1 # this test creates a simple GNU image with docker tools and sees if it executes
3 import ./make-test-python.nix ({ pkgs, ... }:
5 # nixpkgs#214434: dockerTools.buildImage fails to unpack base images
6 # containing duplicate layers when those duplicate tarballs
7 # appear under the manifest's 'Layers'. Docker can generate images
8 # like this even though dockerTools does not.
9 repeatedLayerTestImage =
11 # Rootfs diffs for layers 1 and 2 are identical (and empty)
12 layer1 = pkgs.dockerTools.buildImage { name = "empty"; };
13 layer2 = layer1.overrideAttrs (_: { fromImage = layer1; });
14 repeatedRootfsDiffs = pkgs.runCommand "image-with-links.tar" {
15 nativeBuildInputs = [pkgs.jq];
18 tar -xf "${layer2}" -C contents
20 first_rootfs=$(jq -r '.[0].Layers[0]' manifest.json)
21 second_rootfs=$(jq -r '.[0].Layers[1]' manifest.json)
22 target_rootfs=$(sha256sum "$first_rootfs" | cut -d' ' -f 1).tar
24 # Replace duplicated rootfs diffs with symlinks to one tarball
26 mv "$first_rootfs" "$target_rootfs"
28 ln -s "../$target_rootfs" "$first_rootfs"
29 ln -s "../$target_rootfs" "$second_rootfs"
31 # Update manifest's layers to use the symlinks' target
33 jq ".[0].Layers[0] = \"$target_rootfs\"" |
34 jq ".[0].Layers[1] = \"$target_rootfs\"" > manifest.json.new
35 mv manifest.json.new manifest.json
37 tar --sort=name --hard-dereference -cf $out .
39 in pkgs.dockerTools.buildImage {
40 fromImage = repeatedRootfsDiffs;
41 name = "repeated-layer-test";
43 copyToRoot = pkgs.bash;
44 # A runAsRoot script is required to force previous layers to be unpacked
46 echo 'runAsRoot has run.'
51 pkgs.dockerTools.streamLayeredImage {
54 enableFakechroot = true;
57 chown 12345:12345 /testfile
59 config.Cmd = [ "${pkgs.coreutils}/bin/stat" "-c" "%u:%g" "/testfile" ];
63 pkgs.dockerTools.streamLayeredImage {
64 name = "non-root-test";
72 Cmd = [ "${pkgs.coreutils}/bin/stat" "-c" "%u:%g" "${pkgs.coreutils}/bin/stat" ];
76 name = "docker-tools";
77 meta = with pkgs.lib.maintainers; {
78 maintainers = [ lnl7 roberth ];
90 testScript = with pkgs.dockerTools; ''
91 unix_time_second1 = "1970-01-01T00:00:01Z"
93 docker.wait_for_unit("sockets.target")
95 with subtest("includeStorePath"):
96 with subtest("assumption"):
97 docker.succeed("${examples.helloOnRoot} | docker load")
98 docker.succeed("docker run --rm hello | grep -i hello")
99 docker.succeed("docker image rm hello:latest")
101 with subtest("includeStorePath = false; breaks example"):
102 docker.succeed("${examples.helloOnRootNoStore} | docker load")
103 docker.fail("docker run --rm hello | grep -i hello")
104 docker.succeed("docker image rm hello:latest")
105 with subtest("includeStorePath = false; breaks example (fakechroot)"):
106 docker.succeed("${examples.helloOnRootNoStoreFakechroot} | docker load")
107 docker.fail("docker run --rm hello | grep -i hello")
108 docker.succeed("docker image rm hello:latest")
110 with subtest("Ensure ZERO paths are added to the store"):
111 docker.fail("${examples.helloOnRootNoStore} | ${pkgs.crane}/bin/crane export - - | tar t | grep 'nix/store/'")
112 with subtest("Ensure ZERO paths are added to the store (fakechroot)"):
113 docker.fail("${examples.helloOnRootNoStoreFakechroot} | ${pkgs.crane}/bin/crane export - - | tar t | grep 'nix/store/'")
115 with subtest("includeStorePath = false; works with mounted store"):
116 docker.succeed("${examples.helloOnRootNoStore} | docker load")
117 docker.succeed("docker run --rm --volume ${builtins.storeDir}:${builtins.storeDir}:ro hello | grep -i hello")
118 docker.succeed("docker image rm hello:latest")
119 with subtest("includeStorePath = false; works with mounted store (fakechroot)"):
120 docker.succeed("${examples.helloOnRootNoStoreFakechroot} | docker load")
121 docker.succeed("docker run --rm --volume ${builtins.storeDir}:${builtins.storeDir}:ro hello | grep -i hello")
122 docker.succeed("docker image rm hello:latest")
124 with subtest("Ensure Docker images use a stable date by default"):
126 "docker load --input='${examples.bash}'"
128 assert unix_time_second1 in docker.succeed(
129 "docker inspect ${examples.bash.imageName} "
130 + "| ${pkgs.jq}/bin/jq -r .[].Created",
133 docker.succeed("docker run --rm ${examples.bash.imageName} bash --version")
134 # Check imageTag attribute matches image
135 docker.succeed("docker images --format '{{.Tag}}' | grep -F '${examples.bash.imageTag}'")
136 docker.succeed("docker rmi ${examples.bash.imageName}")
138 # The remaining combinations
139 with subtest("Ensure imageTag attribute matches image"):
141 "docker load --input='${examples.bashNoTag}'"
144 "docker images --format '{{.Tag}}' | grep -F '${examples.bashNoTag.imageTag}'"
146 docker.succeed("docker rmi ${examples.bashNoTag.imageName}:${examples.bashNoTag.imageTag}")
149 "docker load --input='${examples.bashNoTagLayered}'"
152 "docker images --format '{{.Tag}}' | grep -F '${examples.bashNoTagLayered.imageTag}'"
154 docker.succeed("docker rmi ${examples.bashNoTagLayered.imageName}:${examples.bashNoTagLayered.imageTag}")
157 "${examples.bashNoTagStreamLayered} | docker load"
160 "docker images --format '{{.Tag}}' | grep -F '${examples.bashNoTagStreamLayered.imageTag}'"
163 "docker rmi ${examples.bashNoTagStreamLayered.imageName}:${examples.bashNoTagStreamLayered.imageTag}"
167 "docker load --input='${examples.nixLayered}'"
169 docker.succeed("docker images --format '{{.Tag}}' | grep -F '${examples.nixLayered.imageTag}'")
170 docker.succeed("docker rmi ${examples.nixLayered.imageName}")
172 with subtest("Check that images with alternative compression schemas load"):
174 "docker load --input='${examples.bashZstdCompressed}'",
175 "docker rmi ${examples.bashZstdCompressed.imageName}",
178 "docker load --input='${examples.bashUncompressed}'",
179 "docker rmi ${examples.bashUncompressed.imageName}",
182 "docker load --input='${examples.bashLayeredUncompressed}'",
183 "docker rmi ${examples.bashLayeredUncompressed.imageName}",
186 "docker load --input='${examples.bashLayeredZstdCompressed}'",
187 "docker rmi ${examples.bashLayeredZstdCompressed.imageName}",
191 "Check if the nix store is correctly initialized by listing "
192 "dependencies of the installed Nix binary"
195 "docker load --input='${examples.nix}'",
196 "docker run --rm ${examples.nix.imageName} nix-store -qR ${pkgs.nix}",
197 "docker rmi ${examples.nix.imageName}",
201 "Ensure (layered) nix store has correct permissions "
202 "and that the container starts when its process does not have uid 0"
205 "docker load --input='${examples.bashLayeredWithUser}'",
206 "docker run -u somebody --rm ${examples.bashLayeredWithUser.imageName} ${pkgs.bash}/bin/bash -c 'test 755 == $(stat --format=%a /nix) && test 755 == $(stat --format=%a /nix/store)'",
207 "docker rmi ${examples.bashLayeredWithUser.imageName}",
210 with subtest("The nix binary symlinks are intact"):
212 "docker load --input='${examples.nix}'",
213 "docker run --rm ${examples.nix.imageName} ${pkgs.bash}/bin/bash -c 'test nix == $(readlink ${pkgs.nix}/bin/nix-daemon)'",
214 "docker rmi ${examples.nix.imageName}",
217 with subtest("The nix binary symlinks are intact when the image is layered"):
219 "docker load --input='${examples.nixLayered}'",
220 "docker run --rm ${examples.nixLayered.imageName} ${pkgs.bash}/bin/bash -c 'test nix == $(readlink ${pkgs.nix}/bin/nix-daemon)'",
221 "docker rmi ${examples.nixLayered.imageName}",
224 with subtest("The pullImage tool works"):
226 "docker load --input='${examples.testNixFromDockerHub}'",
227 "docker run --rm nix:2.2.1 nix-store --version",
228 "docker rmi nix:2.2.1",
231 with subtest("runAsRoot and entry point work"):
233 "docker load --input='${examples.nginx}'",
234 "docker run --name nginx -d -p 8000:80 ${examples.nginx.imageName}",
236 docker.wait_until_succeeds("curl -f http://localhost:8000/")
238 "docker rm --force nginx",
239 "docker rmi '${examples.nginx.imageName}'",
242 with subtest("A pulled image can be used as base image"):
244 "docker load --input='${examples.onTopOfPulledImage}'",
245 "docker run --rm ontopofpulledimage hello",
246 "docker rmi ontopofpulledimage",
249 with subtest("Regression test for issue #34779"):
251 "docker load --input='${examples.runAsRootExtraCommands}'",
252 "docker run --rm runasrootextracommands cat extraCommands",
253 "docker run --rm runasrootextracommands cat runAsRoot",
254 "docker rmi '${examples.runAsRootExtraCommands.imageName}'",
257 with subtest("Ensure Docker images can use an unstable date"):
259 "docker load --input='${examples.unstableDate}'"
261 assert unix_time_second1 not in docker.succeed(
262 "docker inspect ${examples.unstableDate.imageName} "
263 + "| ${pkgs.jq}/bin/jq -r .[].Created"
266 with subtest("Ensure Layered Docker images can use an unstable date"):
268 "docker load --input='${examples.unstableDateLayered}'"
270 assert unix_time_second1 not in docker.succeed(
271 "docker inspect ${examples.unstableDateLayered.imageName} "
272 + "| ${pkgs.jq}/bin/jq -r .[].Created"
275 with subtest("Ensure Layered Docker images work"):
277 "docker load --input='${examples.layered-image}'",
278 "docker run --rm ${examples.layered-image.imageName}",
279 "docker run --rm ${examples.layered-image.imageName} cat extraCommands",
282 with subtest("Ensure images built on top of layered Docker images work"):
284 "docker load --input='${examples.layered-on-top}'",
285 "docker run --rm ${examples.layered-on-top.imageName}",
288 with subtest("Ensure layered images built on top of layered Docker images work"):
290 "docker load --input='${examples.layered-on-top-layered}'",
291 "docker run --rm ${examples.layered-on-top-layered.imageName}",
295 def set_of_layers(image_name):
298 f"docker inspect {image_name} "
299 + "| ${pkgs.jq}/bin/jq -r '.[] | .RootFS.Layers | .[]'"
304 with subtest("Ensure layers are shared between images"):
306 "docker load --input='${examples.another-layered-image}'"
308 layers1 = set_of_layers("${examples.layered-image.imageName}")
309 layers2 = set_of_layers("${examples.another-layered-image.imageName}")
310 assert bool(layers1 & layers2)
312 with subtest("Ensure order of layers is correct"):
314 "docker load --input='${examples.layersOrder}'"
317 for index in 1, 2, 3:
318 assert f"layer{index}" in docker.succeed(
319 f"docker run --rm ${examples.layersOrder.imageName} cat /tmp/layer{index}"
322 with subtest("Ensure layers unpacked in correct order before runAsRoot runs"):
323 assert "abc" in docker.succeed(
324 "docker load --input='${examples.layersUnpackOrder}'",
325 "docker run --rm ${examples.layersUnpackOrder.imageName} cat /layer-order"
328 with subtest("Ensure repeated base layers handled by buildImage"):
330 "docker load --input='${repeatedLayerTestImage}'",
331 "docker run --rm ${repeatedLayerTestImage.imageName} /bin/bash -c 'exit 0'"
334 with subtest("Ensure environment variables are correctly inherited"):
336 "docker load --input='${examples.environmentVariables}'"
338 out = docker.succeed("docker run --rm ${examples.environmentVariables.imageName} env")
339 env = out.splitlines()
340 assert "FROM_PARENT=true" in env, "envvars from the parent should be preserved"
341 assert "FROM_CHILD=true" in env, "envvars from the child should be preserved"
342 assert "LAST_LAYER=child" in env, "envvars from the child should take priority"
344 with subtest("Ensure environment variables of layered images are correctly inherited"):
346 "docker load --input='${examples.environmentVariablesLayered}'"
348 out = docker.succeed("docker run --rm ${examples.environmentVariablesLayered.imageName} env")
349 env = out.splitlines()
350 assert "FROM_PARENT=true" in env, "envvars from the parent should be preserved"
351 assert "FROM_CHILD=true" in env, "envvars from the child should be preserved"
352 assert "LAST_LAYER=child" in env, "envvars from the child should take priority"
355 "Ensure inherited environment variables of layered images are correctly resolved"
357 # Read environment variables as stored in image config
358 config = docker.succeed(
359 "tar -xOf ${examples.environmentVariablesLayered} manifest.json | ${pkgs.jq}/bin/jq -r .[].Config"
361 out = docker.succeed(
362 f"tar -xOf ${examples.environmentVariablesLayered} {config} | ${pkgs.jq}/bin/jq -r '.config.Env | .[]'"
364 env = out.splitlines()
366 sum(entry.startswith("LAST_LAYER") for entry in env) == 1
367 ), "envvars overridden by child should be unique"
369 with subtest("Ensure image with only 2 layers can be loaded"):
371 "docker load --input='${examples.two-layered-image}'"
375 "Ensure the bulk layer doesn't miss store paths (regression test for #78744)"
378 "docker load --input='${pkgs.dockerTools.examples.bulk-layer}'",
379 # Ensure the two output paths (ls and hello) are in the layer
380 "docker run bulk-layer ls /bin/hello",
384 "Ensure the bulk layer with a base image respects the number of maxLayers"
387 "docker load --input='${pkgs.dockerTools.examples.layered-bulk-layer}'",
388 # Ensure the image runs correctly
389 "docker run layered-bulk-layer ls /bin/hello",
392 # Ensure the image has the correct number of layers
393 assert len(set_of_layers("layered-bulk-layer")) == 4
395 with subtest("Ensure only minimal paths are added to the store"):
396 # TODO: make an example that has no store paths, for example by making
397 # busybox non-self-referential.
399 # This check tests that buildLayeredImage can build images that don't need a store.
401 "docker load --input='${pkgs.dockerTools.examples.no-store-paths}'"
404 docker.succeed("docker run --rm no-store-paths ls / >/dev/console")
406 # If busybox isn't self-referential, we need this line
407 # docker.fail("docker run --rm no-store-paths ls /nix/store >/dev/console")
408 # However, it currently is self-referential, so we check that it is the
410 docker.succeed("diff <(docker run --rm no-store-paths ls /nix/store) <(basename ${pkgs.pkgsStatic.busybox}) >/dev/console")
412 with subtest("Ensure buildLayeredImage does not change store path contents."):
414 "docker load --input='${pkgs.dockerTools.examples.filesInStore}'",
415 "docker run --rm file-in-store nix-store --verify --check-contents",
416 "docker run --rm file-in-store |& grep 'some data'",
419 with subtest("Ensure cross compiled image can be loaded and has correct arch."):
421 "docker load --input='${pkgs.dockerTools.examples.cross}'",
425 "docker inspect ${pkgs.dockerTools.examples.cross.imageName} "
426 + "| ${pkgs.jq}/bin/jq -r .[].Architecture"
428 == "${if pkgs.stdenv.hostPlatform.system == "aarch64-linux" then "amd64" else "arm64"}"
431 with subtest("buildLayeredImage doesn't dereference /nix/store symlink layers"):
433 "docker load --input='${examples.layeredStoreSymlink}'",
434 "docker run --rm ${examples.layeredStoreSymlink.imageName} bash -c 'test -L ${examples.layeredStoreSymlink.passthru.symlink}'",
435 "docker rmi ${examples.layeredStoreSymlink.imageName}",
438 with subtest("buildImage supports registry/ prefix in image name"):
440 "docker load --input='${examples.prefixedImage}'"
443 "docker images --format '{{.Repository}}' | grep -F '${examples.prefixedImage.imageName}'"
446 with subtest("buildLayeredImage supports registry/ prefix in image name"):
448 "docker load --input='${examples.prefixedLayeredImage}'"
451 "docker images --format '{{.Repository}}' | grep -F '${examples.prefixedLayeredImage.imageName}'"
454 with subtest("buildLayeredImage supports running chown with fakeRootCommands"):
456 "docker load --input='${examples.layeredImageWithFakeRootCommands}'"
459 "docker run --rm ${examples.layeredImageWithFakeRootCommands.imageName} sh -c 'stat -c '%u' /home/alice | grep -E ^1000$'"
462 with subtest("Ensure docker load on merged images loads all of the constituent images"):
464 "docker load --input='${examples.mergedBashAndRedis}'"
467 "docker images --format '{{.Repository}}-{{.Tag}}' | grep -F '${examples.bash.imageName}-${examples.bash.imageTag}'"
470 "docker images --format '{{.Repository}}-{{.Tag}}' | grep -F '${examples.redis.imageName}-${examples.redis.imageTag}'"
472 docker.succeed("docker run --rm ${examples.bash.imageName} bash --version")
473 docker.succeed("docker run --rm ${examples.redis.imageName} redis-cli --version")
474 docker.succeed("docker rmi ${examples.bash.imageName}")
475 docker.succeed("docker rmi ${examples.redis.imageName}")
478 "Ensure docker load on merged images loads all of the constituent images (missing tags)"
481 "docker load --input='${examples.mergedBashNoTagAndRedis}'"
484 "docker images --format '{{.Repository}}-{{.Tag}}' | grep -F '${examples.bashNoTag.imageName}-${examples.bashNoTag.imageTag}'"
487 "docker images --format '{{.Repository}}-{{.Tag}}' | grep -F '${examples.redis.imageName}-${examples.redis.imageTag}'"
489 # we need to explicitly specify the generated tag here
491 "docker run --rm ${examples.bashNoTag.imageName}:${examples.bashNoTag.imageTag} bash --version"
493 docker.succeed("docker run --rm ${examples.redis.imageName} redis-cli --version")
494 docker.succeed("docker rmi ${examples.bashNoTag.imageName}:${examples.bashNoTag.imageTag}")
495 docker.succeed("docker rmi ${examples.redis.imageName}")
497 with subtest("mergeImages preserves owners of the original images"):
499 "docker load --input='${examples.mergedBashFakeRoot}'"
502 "docker run --rm ${examples.layeredImageWithFakeRootCommands.imageName} sh -c 'stat -c '%u' /home/alice | grep -E ^1000$'"
505 with subtest("The image contains store paths referenced by the fakeRootCommands output"):
507 "docker run --rm ${examples.layeredImageWithFakeRootCommands.imageName} /hello/bin/layeredImageWithFakeRootCommands-hello"
510 with subtest("mergeImage correctly deals with varying compression schemas in inputs"):
511 docker.succeed("docker load --input='${examples.mergeVaryingCompressor}'")
513 for sub_image, tag in [
514 ("${examples.redis.imageName}", "${examples.redis.imageTag}"),
515 ("${examples.bashUncompressed.imageName}", "${examples.bashUncompressed.imageTag}"),
516 ("${examples.bashZstdCompressed.imageName}", "${examples.bashZstdCompressed.imageTag}"),
518 docker.succeed(f"docker images --format '{{{{.Repository}}}}-{{{{.Tag}}}}' | grep -F '{sub_image}-{tag}'")
519 docker.succeed(f"docker rmi {sub_image}")
522 with subtest("exportImage produces a valid tarball"):
524 "tar -tf ${examples.exportBash} | grep '\./bin/bash' > /dev/null"
527 with subtest("layered image fakeRootCommands with fakechroot works"):
528 docker.succeed("${examples.imageViaFakeChroot} | docker load")
529 docker.succeed("docker run --rm image-via-fake-chroot | grep -i hello")
530 docker.succeed("docker image rm image-via-fake-chroot:latest")
532 with subtest("Ensure bare paths in contents are loaded correctly"):
534 "docker load --input='${examples.build-image-with-path}'",
535 "docker run --rm build-image-with-path bash -c '[[ -e /hello.txt ]]'",
536 "docker rmi build-image-with-path",
539 "${examples.layered-image-with-path} | docker load",
540 "docker run --rm layered-image-with-path bash -c '[[ -e /hello.txt ]]'",
541 "docker rmi layered-image-with-path",
544 with subtest("Ensure correct architecture is present in manifests."):
546 docker load --input='${examples.build-image-with-architecture}'
547 docker inspect build-image-with-architecture \
548 | ${pkgs.jq}/bin/jq -er '.[] | select(.Architecture=="arm64").Architecture'
549 docker rmi build-image-with-architecture
552 ${examples.layered-image-with-architecture} | docker load
553 docker inspect layered-image-with-architecture \
554 | ${pkgs.jq}/bin/jq -er '.[] | select(.Architecture=="arm64").Architecture'
555 docker rmi layered-image-with-architecture
559 docker.succeed("${examples.etc} | docker load")
560 docker.succeed("docker run --rm etc | grep localhost")
561 docker.succeed("docker image rm etc:latest")
563 with subtest("image-with-certs"):
564 docker.succeed("<${examples.image-with-certs} docker load")
565 docker.succeed("docker run --rm image-with-certs:latest test -r /etc/ssl/certs/ca-bundle.crt")
566 docker.succeed("docker run --rm image-with-certs:latest test -r /etc/ssl/certs/ca-certificates.crt")
567 docker.succeed("docker run --rm image-with-certs:latest test -r /etc/pki/tls/certs/ca-bundle.crt")
568 docker.succeed("docker image rm image-with-certs:latest")
570 with subtest("streamLayeredImage: chown is persistent in fakeRootCommands"):
572 "${chownTestImage} | docker load",
573 "docker run --rm ${chownTestImage.imageName} | diff /dev/stdin <(echo 12345:12345)"
576 with subtest("streamLayeredImage: with non-root user"):
578 "${nonRootTestImage} | docker load",
579 "docker run --rm ${chownTestImage.imageName} | diff /dev/stdin <(echo 12345:12345)"