From 3b0d5b5ed3815d3bf5cf1c85a6c8e17091ba7258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Thu, 6 Jun 2019 10:39:50 +0200 Subject: [PATCH] nixos/tests/docker-tools: check layer sharing with buildLayeredImage Adapted from grahamc's blog post on layered Docker images in Nix: https://grahamc.com/blog/nix-and-layered-docker-images --- nixos/tests/docker-tools.nix | 6 ++++++ pkgs/build-support/docker/examples.nix | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/nixos/tests/docker-tools.nix b/nixos/tests/docker-tools.nix index f91121077ea..9ab1a71f331 100644 --- a/nixos/tests/docker-tools.nix +++ b/nixos/tests/docker-tools.nix @@ -68,6 +68,12 @@ import ./make-test.nix ({ pkgs, ... }: { $docker->succeed("docker load --input='${pkgs.dockerTools.examples.layered-on-top}'"); $docker->succeed("docker run --rm ${pkgs.dockerTools.examples.layered-on-top.imageName}"); + # Ensure layers are shared between images + $docker->succeed("docker load --input='${pkgs.dockerTools.examples.another-layered-image}'"); + $docker->succeed("docker inspect ${pkgs.dockerTools.examples.layered-image.imageName} | ${pkgs.jq}/bin/jq -r '.[] | .RootFS.Layers | .[]' | sort > layers1.sha256"); + $docker->succeed("docker inspect ${pkgs.dockerTools.examples.another-layered-image.imageName} | ${pkgs.jq}/bin/jq -r '.[] | .RootFS.Layers | .[]' | sort > layers2.sha256"); + $docker->succeed('[ $(comm -1 -2 layers1.sha256 layers2.sha256 | wc -l) -ne 0 ]'); + # Ensure order of layers is correct $docker->succeed("docker load --input='${pkgs.dockerTools.examples.layersOrder}'"); $docker->succeed("docker run --rm ${pkgs.dockerTools.examples.layersOrder.imageName} cat /tmp/layer1 | grep -q layer1"); diff --git a/pkgs/build-support/docker/examples.nix b/pkgs/build-support/docker/examples.nix index ac21be907b8..29eea33a7e1 100644 --- a/pkgs/build-support/docker/examples.nix +++ b/pkgs/build-support/docker/examples.nix @@ -226,4 +226,12 @@ rec { ''; }; + # 14. Create another layered image, for comparing layers with image 10. + another-layered-image = pkgs.dockerTools.buildLayeredImage { + name = "another-layered-image"; + tag = "latest"; + config.Cmd = [ "${pkgs.hello}/bin/hello" ]; + contents = [ pkgs.hello ]; + }; + }