summary refs log tree commit diff
path: root/nixos/doc/manual/administration/imperative-containers.xml
blob: 258e1ea948da9c95314050e67c1b94ba74fa8881 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<section  xmlns="http://docbook.org/ns/docbook"
          xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:xi="http://www.w3.org/2001/XInclude"
          version="5.0"
          xml:id="sec-imperative-containers">

<title>Imperative Container Management</title>

<para>We’ll cover imperative container management using
<command>nixos-container</command> first.
Be aware that container management is currently only possible
as <literal>root</literal>.</para>

<para>You create a container with
identifier <literal>foo</literal> as follows:

<screen>
# nixos-container create foo
</screen>

This creates the container’s root directory in
<filename>/var/lib/containers/foo</filename> and a small configuration
file in <filename>/etc/containers/foo.conf</filename>. It also builds
the container’s initial system configuration and stores it in
<filename>/nix/var/nix/profiles/per-container/foo/system</filename>. You
can modify the initial configuration of the container on the command
line. For instance, to create a container that has
<command>sshd</command> running, with the given public key for
<literal>root</literal>:

<screen>
# nixos-container create foo --config 'services.openssh.enable = true; \
  users.extraUsers.root.openssh.authorizedKeys.keys = ["ssh-dss AAAAB3N…"];'
</screen>

</para>

<para>Creating a container does not start it. To start the container,
run:

<screen>
# nixos-container start foo
</screen>

This command will return as soon as the container has booted and has
reached <literal>multi-user.target</literal>. On the host, the
container runs within a systemd unit called
<literal>container@<replaceable>container-name</replaceable>.service</literal>.
Thus, if something went wrong, you can get status info using
<command>systemctl</command>:

<screen>
# systemctl status container@foo
</screen>

</para>

<para>If the container has started succesfully, you can log in as
root using the <command>root-login</command> operation:

<screen>
# nixos-container root-login foo
[root@foo:~]#
</screen>

Note that only root on the host can do this (since there is no
authentication).  You can also get a regular login prompt using the
<command>login</command> operation, which is available to all users on
the host:

<screen>
# nixos-container login foo
foo login: alice
Password: ***
</screen>

With <command>nixos-container run</command>, you can execute arbitrary
commands in the container:

<screen>
# nixos-container run foo -- uname -a
Linux foo 3.4.82 #1-NixOS SMP Thu Mar 20 14:44:05 UTC 2014 x86_64 GNU/Linux
</screen>

</para>

<para>There are several ways to change the configuration of the
container. First, on the host, you can edit
<literal>/var/lib/container/<replaceable>name</replaceable>/etc/nixos/configuration.nix</literal>,
and run

<screen>
# nixos-container update foo
</screen>

This will build and activate the new configuration. You can also
specify a new configuration on the command line:

<screen>
# nixos-container update foo --config 'services.httpd.enable = true; \
  services.httpd.adminAddr = "foo@example.org";'

# curl http://$(nixos-container show-ip foo)/
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">…
</screen>

However, note that this will overwrite the container’s
<filename>/etc/nixos/configuration.nix</filename>.</para>

<para>Alternatively, you can change the configuration from within the
container itself by running <command>nixos-rebuild switch</command>
inside the container. Note that the container by default does not have
a copy of the NixOS channel, so you should run <command>nix-channel
--update</command> first.</para>

<para>Containers can be stopped and started using
<literal>nixos-container stop</literal> and <literal>nixos-container
start</literal>, respectively, or by using
<command>systemctl</command> on the container’s service unit. To
destroy a container, including its file system, do

<screen>
# nixos-container destroy foo
</screen>

</para>

</section>