How to build a docker container with nix?

I have a Nix package that I would like to add to the docker container.

In particular, I want to use Nix as a more expressive alternative Dockerfilefor faster (non-linear) image assembly.

I found the documentation on dockerTools.buildImage, but I would like to have a minimal working example, and I would also like to know what ends up in what is in the docker container.

+6
source share
1 answer

The following sample packages (using contents =) the pkgs.nginxnixpkgs package in a docker container:

docker load --input $(nix-build -E 'with import <nixpkgs> {}; pkgs.dockerTools.buildImage { name = "nix-htop"; contents = pkgs.htop; config = { Cmd = [ "/bin/htop" ]; }; }')

Then you can run it with

docker run -it nix-htop

The contents of the container are pretty minimal, one Docker layer:

docker save nix-htop | tar x --to-stdout --wildcards '*/layer.tar' | tar t --exclude="*/*/*/*"
./
./bin/
./bin/htop
./share/
./share/applications/
./share/man/
./share/pixmaps/
nix/
nix/store/
nix/store/gi5vvbjawzw1bakiksazbd50bvfmpmmc-ncurses-6.0/
nix/store/pa5nkrpd5hg5qp1dc4gmbd2vdhn1y3x2-htop-2.0.2/
nix/store/vn6fkjnfps37wa82ri4mwszwvnnan6sk-glibc-2.25/

htop (glibc, ncurses), 26 .

+9

Source: https://habr.com/ru/post/1016500/


All Articles