当我使用dockerTools.buildImage
$ docker run -it lnl7/nix:latest env
PATH=/root/.nix-profile/bin:/run/current-system/sw/bin
HOSTNAME=52be36e10f96
TERM=xterm
MANPATH=/root/.nix-profile/share/man:/run/current-system/sw/share/man
NIX_PATH=nixpkgs=/nix/store/d7r0qlms9w7k0m5kmvinp52abxbncafv-nixpkgs-unstable-2018-01-13
GIT_SSL_CAINFO=/nix/store/dblcx21khkizv3h9n0haqbkmzig3n3wd-nss-cacert-3.33/etc/ssl/certs/ca-bundle.crt
SSL_CERT_FILE=/nix/store/dblcx21khkizv3h9n0haqbkmzig3n3wd-nss-cacert-3.33/etc/ssl/certs/ca-bundle.crt
HOME=/root
但是使用这个
$ cat runner/nixops_runner.nix
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
workdir = "/app";
sshConfig = writeText "ssh_config" ''
Host *
StrictHostKeyChecking=no
UserKnownHostsFile /dev/null
IdentityFile /ssh/id_rsa
'';
nixFromDockerHub = dockerTools.pullImage {
imageName = "lnl7/nix";
imageTag = "latest";
# this hash will need change if the tag is updated at docker hub
sha256 = "031xba6yi72v87v560wqbcsi80hqbwx498qz5b5jxigj36yb3k36";
};
in
dockerTools.buildImage {
name = "nixops_runner";
fromImage = nixFromDockerHub;
}
我明白了
$ docker load --input $(nix-build runner/nixops_runner.nix --no-out-link) && docker run -it nixops_runner env
Loaded image: nixops_runner:latest
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=9d673e788746
TERM=xterm
HOME=/root
如何保留图像的环境变量?
答案 0 :(得分:1)
这些函数在低抽象级别(几乎没有)工作。模拟docker构建语义似乎不是一个目标。另请注意API将发生变化的警告。
如果您知道图片环境中的内容,可以手动为buildImage
指定。不支持从图层继承环境变量(截至2018年5月)。
pkgs.dockerTools.buildImage {
name = "nixops_runner";
config = {
Env = [ "PATH=/bin" "TZDIR=${pkgs.tzdata}/share/zoneinfo" ];
Cmd = "/some/path/bin/my-script";
};
}