如何使用nix构建docker容器? [英] How to build a docker container with nix?

查看:360
本文介绍了如何使用nix构建docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



具体来说,我想使用Nix作为一个更有代表性的替代方案, code> Dockerfile 有更快(非线性)的图像构建。



我已经找到 dockerTools.buildImage 但是我'd喜欢有一个最小的工作示例,我也想知道最终在Docker容器中的结果。

解决方案

p>以下示例程序包(使用 contents = )将$ code> pkgs.nginx nixpkgs程序包导入Docker容器: p>

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

然后,您可以使用

$ b运行它
$ b

  docker run -it nix-htop 

容器的内容非常小,单个Docker图层:

  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 MB。


I have a Nix package I'd like to bundle up into a docker container.

Specifically, I want to use Nix as a more expressive alternative to a Dockerfile to have faster (non-linear) image builds.

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

解决方案

The following example packages (using contents =) the pkgs.nginx nixpkgs package into 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" ]; }; }')

You can then run it with

docker run -it nix-htop

The contents of the container are pretty minimal, a single 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/

Only htop and its dependencies (glibc, ncurses), 26 MB on my case.

这篇关于如何使用nix构建docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆