1<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-imperative-containers">
2 <title>Imperative Container Management</title>
3 <para>
4 We’ll cover imperative container management using
5 <literal>nixos-container</literal> first. Be aware that container
6 management is currently only possible as <literal>root</literal>.
7 </para>
8 <para>
9 You create a container with identifier <literal>foo</literal> as
10 follows:
11 </para>
12 <programlisting>
13# nixos-container create foo
14</programlisting>
15 <para>
16 This creates the container’s root directory in
17 <literal>/var/lib/nixos-containers/foo</literal> and a small
18 configuration file in
19 <literal>/etc/nixos-containers/foo.conf</literal>. It also builds
20 the container’s initial system configuration and stores it in
21 <literal>/nix/var/nix/profiles/per-container/foo/system</literal>.
22 You can modify the initial configuration of the container on the
23 command line. For instance, to create a container that has
24 <literal>sshd</literal> running, with the given public key for
25 <literal>root</literal>:
26 </para>
27 <programlisting>
28# nixos-container create foo --config '
29 services.openssh.enable = true;
30 users.users.root.openssh.authorizedKeys.keys = ["ssh-dss AAAAB3N…"];
31'
32</programlisting>
33 <para>
34 By default the next free address in the
35 <literal>10.233.0.0/16</literal> subnet will be chosen as container
36 IP. This behavior can be altered by setting
37 <literal>--host-address</literal> and
38 <literal>--local-address</literal>:
39 </para>
40 <programlisting>
41# nixos-container create test --config-file test-container.nix \
42 --local-address 10.235.1.2 --host-address 10.235.1.1
43</programlisting>
44 <para>
45 Creating a container does not start it. To start the container, run:
46 </para>
47 <programlisting>
48# nixos-container start foo
49</programlisting>
50 <para>
51 This command will return as soon as the container has booted and has
52 reached <literal>multi-user.target</literal>. On the host, the
53 container runs within a systemd unit called
54 <literal>container@container-name.service</literal>. Thus, if
55 something went wrong, you can get status info using
56 <literal>systemctl</literal>:
57 </para>
58 <programlisting>
59# systemctl status container@foo
60</programlisting>
61 <para>
62 If the container has started successfully, you can log in as root
63 using the <literal>root-login</literal> operation:
64 </para>
65 <programlisting>
66# nixos-container root-login foo
67[root@foo:~]#
68</programlisting>
69 <para>
70 Note that only root on the host can do this (since there is no
71 authentication). You can also get a regular login prompt using the
72 <literal>login</literal> operation, which is available to all users
73 on the host:
74 </para>
75 <programlisting>
76# nixos-container login foo
77foo login: alice
78Password: ***
79</programlisting>
80 <para>
81 With <literal>nixos-container run</literal>, you can execute
82 arbitrary commands in the container:
83 </para>
84 <programlisting>
85# nixos-container run foo -- uname -a
86Linux foo 3.4.82 #1-NixOS SMP Thu Mar 20 14:44:05 UTC 2014 x86_64 GNU/Linux
87</programlisting>
88 <para>
89 There are several ways to change the configuration of the container.
90 First, on the host, you can edit
91 <literal>/var/lib/container/name/etc/nixos/configuration.nix</literal>,
92 and run
93 </para>
94 <programlisting>
95# nixos-container update foo
96</programlisting>
97 <para>
98 This will build and activate the new configuration. You can also
99 specify a new configuration on the command line:
100 </para>
101 <programlisting>
102# nixos-container update foo --config '
103 services.httpd.enable = true;
104 services.httpd.adminAddr = "foo@example.org";
105 networking.firewall.allowedTCPPorts = [ 80 ];
106'
107
108# curl http://$(nixos-container show-ip foo)/
109<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">…
110</programlisting>
111 <para>
112 However, note that this will overwrite the container’s
113 <literal>/etc/nixos/configuration.nix</literal>.
114 </para>
115 <para>
116 Alternatively, you can change the configuration from within the
117 container itself by running <literal>nixos-rebuild switch</literal>
118 inside the container. Note that the container by default does not
119 have a copy of the NixOS channel, so you should run
120 <literal>nix-channel --update</literal> first.
121 </para>
122 <para>
123 Containers can be stopped and started using
124 <literal>nixos-container stop</literal> and
125 <literal>nixos-container start</literal>, respectively, or by using
126 <literal>systemctl</literal> on the container’s service unit. To
127 destroy a container, including its file system, do
128 </para>
129 <programlisting>
130# nixos-container destroy foo
131</programlisting>
132</section>