How do I create a docker image containing a given NixOS system?
I know of dockerTools and of the docker-image profile,but this
serverImg= nixos({config,lib,...}:{
imports=[
./configuration.nix
<nixpkgs/nixos/modules/profiles/docker-container.nix>
];
});
dockerImg=with dockerTools;buildLayeredImage{
name="test-server";
contents=[
# "${serverImg.toplevel}/sw"
# (runCommandNoCC "empty"{}"")
coreutils
];
extraCommands=''
#!${pkgs.runtimeShell}
touch /etc/NIXOS
ln -s ${serverImg.toplevel} /run/current-system
${serverImg.toplevel}/bin/switch-to-configuration switch
'';
config={
ExposedPorts."8080"={};
Cmd="manage shell_plus";
};
};
does not build,even through serverImg does.
1 Like
Already available here: Docker Hub
Hmm, isn’t that an Alpine Linux based image with Nix installed?
lewo
January 16, 2020, 10:24am
4
I don’t think you could run switch-to-configuration
as an extraCommand
since IIRC, it requires access to /
for instance. And if you want to run NixOS services, you will also need to run systemd in the container.
This is not exactly what you want to do, but a related project is https://github.com/cloudwatt/nix-container-images#reuse-nixos-modules .
lewo
January 16, 2020, 10:25am
5
Yes, it is a Docker image with Nix installed.
https://github.com/NixOS/nixpkgs/blob/68bcd4af59a96400fffc46432d703ec9dccdf9bd/nixos/modules/virtualisation/docker-image.nix
So,apparently (pkgs.nixos<nixpkgs/nixos/virtualisation/docker-image.nix>).tarball
is,in fact,a docker image,but apparently it needs to run as --privileged
.
Neither in the NixOS manual nor in the NixOS wiki this is even suggested,and I stumbled on this file on accident while loooking for the docker-image.nix
profile.
3 Likes