git.iko.soy/typeable/nixpkgs
on master help sign in
dockerTools.buildLayeredImage: image names with registry/ prefix

When using `buildLayeredImage`, it is not possible to specify an image name of the form `<registry>/my/image`, although it is a valid name. This is due to derivations under `buildLayeredImage` using that image name as their derivation name, but slashes are not permitted in that context. A while ago, #13099 fixed that exact same problem in `buildImage` by using `baseNameOf name` in derivation names instead of `name`. This change does the same thing for `buildLayeredImage`.

commit419a4fa596577667271fcfc057bc9063ddbffe47 authorLouis Blin <45168934+lbpdt@users.noreply.github.com> dateTue 9 Mar 2021, 18:32 (6 years ago) parent0124e87
3 files changed+37−5
nixos/tests/docker-tools.nix+16 pkgs/build-support/docker/default.nix+7−5 pkgs/build-support/docker/examples.nix+14
nixos/tests/docker-tools.nix+16
@@ -254,5 +254,21 @@ import ./make-test-python.nix ({ pkgs, ... }: {
254254 "docker run --rm ${examples.layeredStoreSymlink.imageName} bash -c 'test -L ${examples.layeredStoreSymlink.passthru.symlink}'",
255255 "docker rmi ${examples.layeredStoreSymlink.imageName}",
256256 )
257+
258+ with subtest("buildImage supports registry/ prefix in image name"):
259+ docker.succeed(
260+ "docker load --input='${examples.prefixedImage}'"
261+ )
262+ docker.succeed(
263+ "docker images --format '{{.Repository}}' | grep -F '${examples.prefixedImage.imageName}'"
264+ )
265+
266+ with subtest("buildLayeredImage supports registry/ prefix in image name"):
267+ docker.succeed(
268+ "docker load --input='${examples.prefixedLayeredImage}'"
269+ )
270+ docker.succeed(
271+ "docker images --format '{{.Repository}}' | grep -F '${examples.prefixedLayeredImage.imageName}'"
272+ )
257273 '';
258274})
pkgs/build-support/docker/default.nix+7−5
@@ -447,7 +447,7 @@ rec {
447447 let
448448 stream = streamLayeredImage args;
449449 in
450− runCommand "${name}.tar.gz" {
450+ runCommand "${baseNameOf name}.tar.gz" {
451451 inherit (stream) imageName;
452452 passthru = { inherit (stream) imageTag; };
453453 nativeBuildInputs = [ pigz ];
@@ -746,8 +746,10 @@ rec {
746746 (lib.assertMsg (maxLayers > 1)
747747 "the maxLayers argument of dockerTools.buildLayeredImage function must be greather than 1 (current value: ${toString maxLayers})");
748748 let
749+ baseName = baseNameOf name;
750+
749751 streamScript = writePython3 "stream" {} ./stream_layered_image.py;
750− baseJson = writeText "${name}-base.json" (builtins.toJSON {
752+ baseJson = writeText "${baseName}-base.json" (builtins.toJSON {
751753 inherit config;
752754 architecture = defaultArch;
753755 os = "linux";
@@ -759,7 +761,7 @@ rec {
759761 # things like permissions set on 'extraCommands' are not overriden
760762 # by Nix. Then we precompute the sha256 for performance.
761763 customisationLayer = symlinkJoin {
762− name = "${name}-customisation-layer";
764+ name = "${baseName}-customisation-layer";
763765 paths = contentsList;
764766 inherit extraCommands;
765767 postBuild = ''
@@ -788,7 +790,7 @@ rec {
788790 # so they'll be excluded from the created images.
789791 unnecessaryDrvs = [ baseJson overallClosure ];
790792
791− conf = runCommand "${name}-conf.json" {
793+ conf = runCommand "${baseName}-conf.json" {
792794 inherit maxLayers created;
793795 imageName = lib.toLower name;
794796 passthru.imageTag =
@@ -852,7 +854,7 @@ rec {
852854 --arg created "$created" |
853855 tee $out
854856 '';
855− result = runCommand "stream-${name}" {
857+ result = runCommand "stream-${baseName}" {
856858 inherit (conf) imageName;
857859 passthru = {
858860 inherit (conf) imageTag;
pkgs/build-support/docker/examples.nix+14
@@ -427,4 +427,18 @@ rec {
427427 tag = "latest";
428428 contents = [ pkgs.bash symlink ];
429429 } // { passthru = { inherit symlink; }; };
430+
431+ # image with registry/ prefix
432+ prefixedImage = pkgs.dockerTools.buildImage {
433+ name = "registry-1.docker.io/image";
434+ tag = "latest";
435+ config.Cmd = [ "${pkgs.hello}/bin/hello" ];
436+ };
437+
438+ # layered image with registry/ prefix
439+ prefixedLayeredImage = pkgs.dockerTools.buildLayeredImage {
440+ name = "registry-1.docker.io/layered-image";
441+ tag = "latest";
442+ config.Cmd = [ "${pkgs.hello}/bin/hello" ];
443+ };
430444}