Skip to content

Commit

Permalink
Merge pull request #117603 from lbpdt/fix/docker-tools-layered-image-env
Browse files Browse the repository at this point in the history
dockerTools.streamLayeredImage: resolve duplicate env vars
  • Loading branch information
roberth committed Mar 26, 2021
2 parents 69510c7 + b3f6828 commit 363d7c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions nixos/tests/docker-tools.nix
Expand Up @@ -221,6 +221,21 @@ import ./make-test-python.nix ({ pkgs, ... }: {
assert "FROM_CHILD=true" in env, "envvars from the child should be preserved"
assert "LAST_LAYER=child" in env, "envvars from the child should take priority"
with subtest(
"Ensure inherited environment variables of layered images are correctly resolved"
):
# Read environment variables as stored in image config
config = docker.succeed(
"tar -xOf ${examples.environmentVariablesLayered} manifest.json | ${pkgs.jq}/bin/jq -r .[].Config"
).strip()
out = docker.succeed(
f"tar -xOf ${examples.environmentVariablesLayered} {config} | ${pkgs.jq}/bin/jq -r '.config.Env | .[]'"
)
env = out.splitlines()
assert (
sum(entry.startswith("LAST_LAYER") for entry in env) == 1
), "envvars overridden by child should be unique"
with subtest("Ensure image with only 2 layers can be loaded"):
docker.succeed(
"docker load --input='${examples.two-layered-image}'"
Expand Down
4 changes: 3 additions & 1 deletion pkgs/build-support/docker/stream_layered_image.py
Expand Up @@ -202,7 +202,9 @@ def overlay_base_config(from_image, final_config):
# Preserve environment from base image
final_env = base_config.get("Env", []) + final_config.get("Env", [])
if final_env:
final_config["Env"] = final_env
# Resolve duplicates (last one wins) and format back as list
resolved_env = {entry.split("=", 1)[0]: entry for entry in final_env}
final_config["Env"] = list(resolved_env.values())
return final_config


Expand Down

0 comments on commit 363d7c8

Please sign in to comment.