2014-08-24 19:18:18 +02:00
|
|
|
|
<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">
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<title>Imperative Container Management</title>
|
2014-08-24 19:18:18 +02:00
|
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<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>
|
2014-08-24 19:18:18 +02:00
|
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<para>
|
|
|
|
|
You create a container with identifier <literal>foo</literal> as follows:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# nixos-container create foo
|
2014-08-24 19:18:18 +02:00
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
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>:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2017-04-24 17:31:02 +02:00
|
|
|
|
# nixos-container create foo --config '
|
2018-04-05 10:43:56 +02:00
|
|
|
|
<xref linkend="opt-services.openssh.enable"/> = true;
|
2018-06-28 23:48:38 +02:00
|
|
|
|
<link linkend="opt-users.users._name__.openssh.authorizedKeys.keys">users.users.root.openssh.authorizedKeys.keys</link> = ["ssh-dss AAAAB3N…"];
|
2017-04-24 17:31:02 +02:00
|
|
|
|
'
|
2014-08-24 19:18:18 +02:00
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
</para>
|
2014-08-24 19:18:18 +02:00
|
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<para>
|
|
|
|
|
Creating a container does not start it. To start the container, run:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# nixos-container start foo
|
2014-08-24 19:18:18 +02:00
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
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>:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# systemctl status container@foo
|
2014-08-24 19:18:18 +02:00
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
</para>
|
2014-08-24 19:18:18 +02:00
|
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<para>
|
|
|
|
|
If the container has started successfully, you can log in as root using the
|
|
|
|
|
<command>root-login</command> operation:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# nixos-container root-login foo
|
2014-08-24 19:18:18 +02:00
|
|
|
|
[root@foo:~]#
|
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
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:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# nixos-container login foo
|
2014-08-24 19:18:18 +02:00
|
|
|
|
foo login: alice
|
|
|
|
|
Password: ***
|
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
With <command>nixos-container run</command>, you can execute arbitrary
|
|
|
|
|
commands in the container:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# nixos-container run foo -- uname -a
|
2014-08-24 19:18:18 +02:00
|
|
|
|
Linux foo 3.4.82 #1-NixOS SMP Thu Mar 20 14:44:05 UTC 2014 x86_64 GNU/Linux
|
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
</para>
|
2014-08-24 19:18:18 +02:00
|
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<para>
|
2018-09-30 02:37:11 +02:00
|
|
|
|
To change the configuration of the container, you can edit
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<literal>/var/lib/container/<replaceable>name</replaceable>/etc/nixos/configuration.nix</literal>,
|
|
|
|
|
and run
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# nixos-container update foo
|
2014-08-24 19:18:18 +02:00
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
This will build and activate the new configuration. You can also specify a
|
|
|
|
|
new configuration on the command line:
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2017-04-24 17:31:02 +02:00
|
|
|
|
# nixos-container update foo --config '
|
2018-04-05 10:43:56 +02:00
|
|
|
|
<xref linkend="opt-services.httpd.enable"/> = true;
|
|
|
|
|
<xref linkend="opt-services.httpd.adminAddr"/> = "foo@example.org";
|
|
|
|
|
<xref linkend="opt-networking.firewall.allowedTCPPorts"/> = [ 80 ];
|
2018-09-30 02:37:11 +02:00
|
|
|
|
'
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# curl http://$(nixos-container show-ip foo)/
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">…
|
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
However, note that this will overwrite the container’s
|
|
|
|
|
<filename>/etc/nixos/configuration.nix</filename>.
|
|
|
|
|
</para>
|
|
|
|
|
|
2018-09-30 02:18:58 +02:00
|
|
|
|
<para>
|
2018-09-30 02:37:11 +02:00
|
|
|
|
Note that in previous versions of NixOS (17.09 and earlier) one could also
|
|
|
|
|
use all nix-related commands (like <command>nixos-rebuild switch</command>)
|
|
|
|
|
from inside the container. However, since the release of Nix 2.0 this is not
|
|
|
|
|
supported anymore. Supporting Nix commands inside the container might be
|
|
|
|
|
possible again in future versions. See
|
|
|
|
|
<link xlink:href="https://github.com/NixOS/nixpkgs/issues/40355">the github
|
|
|
|
|
issue</link> for tracking progress on this issue.
|
2018-09-30 02:18:58 +02:00
|
|
|
|
</para>
|
|
|
|
|
|
2018-05-02 01:57:09 +02:00
|
|
|
|
<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
|
2014-08-24 19:18:18 +02:00
|
|
|
|
<screen>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
# nixos-container destroy foo
|
2014-08-24 19:18:18 +02:00
|
|
|
|
</screen>
|
2018-05-02 01:57:09 +02:00
|
|
|
|
</para>
|
2016-06-01 16:23:32 +02:00
|
|
|
|
</section>
|