git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #87253 from utdemir/dockertools-preserve-env

Preserve environment variables from the parent image on dockerTools.buildImage

commit98a723e6b3977093ab783836dda46cb6084f547c authorlewo <lewo@abesis.fr> dateFri 15 May 2020, 09:59 (7 years ago) parentc956b59f5a90a7
3 files changed+48−6
nixos/tests/docker-tools.nix+10 pkgs/build-support/docker/default.nix+7−2 pkgs/build-support/docker/examples.nix+31−4
nixos/tests/docker-tools.nix+10
@@ -124,6 +124,16 @@ import ./make-test-python.nix ({ pkgs, ... }: {
124124 f"docker run --rm ${examples.layersOrder.imageName} cat /tmp/layer{index}"
125125 )
126126
127+ with subtest("Ensure environment variables are correctly inherited"):
128+ docker.succeed(
129+ "docker load --input='${examples.environmentVariables}'"
130+ )
131+ out = docker.succeed("docker run --rm ${examples.environmentVariables.imageName} env")
132+ env = out.splitlines()
133+ assert "FROM_PARENT=true" in env, "envvars from the parent should be preserved"
134+ assert "FROM_CHILD=true" in env, "envvars from the child should be preserved"
135+ assert "LAST_LAYER=child" in env, "envvars from the child should take priority"
136+
127137 with subtest("Ensure image with only 2 layers can be loaded"):
128138 docker.succeed(
129139 "docker load --input='${examples.two-layered-image}'"
pkgs/build-support/docker/default.nix+7−2
@@ -773,13 +773,17 @@ rec {
773773
774774 mkdir image
775775 touch baseFiles
776+ baseEnvs='[]'
776777 if [[ -n "$fromImage" ]]; then
777778 echo "Unpacking base image..."
778779 tar -C image -xpf "$fromImage"
779780
781+ # Store the layers and the environment variables from the base image
780782 cat ./image/manifest.json | jq -r '.[0].Layers | .[]' > layer-list
783+ configName="$(cat ./image/manifest.json | jq -r '.[0].Config')"
784+ baseEnvs="$(cat "./image/$configName" | jq '.config.Env // []')"
781785
782− # Do not import the base image configuration and manifest
786+ # Otherwise do not import the base image configuration and manifest
783787 chmod a+w image image/*.json
784788 rm -f image/*.json
785789
@@ -859,7 +863,8 @@ rec {
859863 ) | sponge layer-list
860864
861865 # Create image json and image manifest
862− imageJson=$(cat ${baseJson} | jq ". + {\"rootfs\": {\"diff_ids\": [], \"type\": \"layers\"}}")
866+ imageJson=$(cat ${baseJson} | jq '.config.Env = $baseenv + .config.Env' --argjson baseenv "$baseEnvs")
867+ imageJson=$(echo "$imageJson" | jq ". + {\"rootfs\": {\"diff_ids\": [], \"type\": \"layers\"}}")
863868 manifestJson=$(jq -n "[{\"RepoTags\":[\"$imageName:$imageTag\"]}]")
864869
865870 for layerTar in $(cat ./layer-list); do
pkgs/build-support/docker/examples.nix+31−4
@@ -231,14 +231,41 @@ rec {
231231 '';
232232 };
233233
234− # 14. Create another layered image, for comparing layers with image 10.
234+ # 14. Environment variable inheritance.
235+ # Child image should inherit parents environment variables,
236+ # optionally overriding them.
237+ environmentVariables = let
238+ parent = pkgs.dockerTools.buildImage {
239+ name = "parent";
240+ tag = "latest";
241+ config = {
242+ Env = [
243+ "FROM_PARENT=true"
244+ "LAST_LAYER=parent"
245+ ];
246+ };
247+ };
248+ in pkgs.dockerTools.buildImage {
249+ name = "child";
250+ fromImage = parent;
251+ tag = "latest";
252+ contents = [ pkgs.coreutils ];
253+ config = {
254+ Env = [
255+ "FROM_CHILD=true"
256+ "LAST_LAYER=child"
257+ ];
258+ };
259+ };
260+
261+ # 15. Create another layered image, for comparing layers with image 10.
235262 another-layered-image = pkgs.dockerTools.buildLayeredImage {
236263 name = "another-layered-image";
237264 tag = "latest";
238265 config.Cmd = [ "${pkgs.hello}/bin/hello" ];
239266 };
240267
241− # 15. Create a layered image with only 2 layers
268+ # 16. Create a layered image with only 2 layers
242269 two-layered-image = pkgs.dockerTools.buildLayeredImage {
243270 name = "two-layered-image";
244271 tag = "latest";
@@ -247,7 +274,7 @@ rec {
247274 maxLayers = 2;
248275 };
249276
250− # 16. Create a layered image with more packages than max layers.
277+ # 17. Create a layered image with more packages than max layers.
251278 # coreutils and hello are part of the same layer
252279 bulk-layer = pkgs.dockerTools.buildLayeredImage {
253280 name = "bulk-layer";
@@ -258,7 +285,7 @@ rec {
258285 maxLayers = 2;
259286 };
260287
261− # 17. Create a "layered" image without nix store layers. This is not
288+ # 18. Create a "layered" image without nix store layers. This is not
262289 # recommended, but can be useful for base images in rare cases.
263290 no-store-paths = pkgs.dockerTools.buildLayeredImage {
264291 name = "no-store-paths";