summary refs log tree commit diff
path: root/pkgs/build-support/docker/store-path-to-layer.sh
diff options
context:
space:
mode:
authorGraham Christensen <graham.christensen@target.com>2018-09-25 10:53:42 -0400
committerGraham Christensen <graham.christensen@target.com>2018-09-26 17:54:14 -0400
commit4fe900619080f0b1a804abbccaab500d819ead10 (patch)
tree2313ef2722f9d6d10edf3bb3f4bc8b2c08e3107c /pkgs/build-support/docker/store-path-to-layer.sh
parentfd045173cef84e65a8cb133ded28c99167cb0901 (diff)
downloadnixpkgs-4fe900619080f0b1a804abbccaab500d819ead10.tar
nixpkgs-4fe900619080f0b1a804abbccaab500d819ead10.tar.gz
nixpkgs-4fe900619080f0b1a804abbccaab500d819ead10.tar.bz2
nixpkgs-4fe900619080f0b1a804abbccaab500d819ead10.tar.lz
nixpkgs-4fe900619080f0b1a804abbccaab500d819ead10.tar.xz
nixpkgs-4fe900619080f0b1a804abbccaab500d819ead10.tar.zst
nixpkgs-4fe900619080f0b1a804abbccaab500d819ead10.zip
dockerTools.buildLayeredImage: init
Create a many-layered Docker Image.

Implements much less than buildImage:

 - Doesn't support specific uids/gids
 - Doesn't support runninng commands after building
 - Doesn't require qemu
 - Doesn't create mutable copies of the files in the path
 - Doesn't support parent images

If you want those feature, I recommend using buildLayeredImage as an
input to buildImage.

Notably, it does support:

 - Caching low level, common paths based on a graph traversial
   algorithm, see referencesByPopularity in
   0a80233487993256e811f566b1c80a40394c03d6
 - Configurable number of layers. If you're not using AUFS or not
   extending the image, you can specify a larger number of layers at
   build time:

       pkgs.dockerTools.buildLayeredImage {
         name = "hello";
         maxLayers = 128;
         config.Cmd = [ "${pkgs.gitFull}/bin/git" ];
       };

 - Parallelized creation of the layers, improving build speed.
 - The contents of the image includes the closure of the configuration,
   so you don't have to specify paths in contents and config.

   With buildImage, paths referred to by the config were not included
   automatically in the image. Thus, if you wanted to call Git, you
   had to specify it twice:

       pkgs.dockerTools.buildImage {
         name = "hello";
         contents = [ pkgs.gitFull ];
         config.Cmd = [ "${pkgs.gitFull}/bin/git" ];
       };

   buildLayeredImage on the other hand includes the runtime closure of
   the config when calculating the contents of the image:

       pkgs.dockerTools.buildImage {
         name = "hello";
         config.Cmd = [ "${pkgs.gitFull}/bin/git" ];
       };

Minor Problems

 - If any of the store paths change, every layer will be rebuilt in
   the nix-build. However, beacuse the layers are bit-for-bit
   reproducable, when these images are loaded in to Docker they will
   match existing layers and not be imported or uploaded twice.

Common Questions

 - Aren't Docker layers ordered?

   No. People who have used a Dockerfile before assume Docker's
   Layers are inherently ordered. However, this is not true -- Docker
   layers are content-addressable and are not explicitly layered until
   they are composed in to an Image.

 - What happens if I have more than maxLayers of store paths?

   The first (maxLayers-2) most "popular" paths will have their own
   individual layers, then layer #(maxLayers-1) will contain all the
   remaining "unpopular" paths, and finally layer #(maxLayers) will
   contain the Image configuration.
Diffstat (limited to 'pkgs/build-support/docker/store-path-to-layer.sh')
-rwxr-xr-xpkgs/build-support/docker/store-path-to-layer.sh24
1 files changed, 24 insertions, 0 deletions
diff --git a/pkgs/build-support/docker/store-path-to-layer.sh b/pkgs/build-support/docker/store-path-to-layer.sh
new file mode 100755
index 00000000000..ff814c1f613
--- /dev/null
+++ b/pkgs/build-support/docker/store-path-to-layer.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+set -eu
+
+layerNumber=$1
+shift
+
+layerPath="./layers/$layerNumber"
+echo "Creating layer #$layerNumber for $@"
+
+mkdir -p "$layerPath"
+tar -rpf "$layerPath/layer.tar" --hard-dereference --sort=name \
+    --mtime="@$SOURCE_DATE_EPOCH" \
+    --owner=0 --group=0 "$@"
+
+# Compute a checksum of the tarball.
+tarhash=$(tarsum < $layerPath/layer.tar)
+
+# Add a 'checksum' field to the JSON, with the value set to the
+# checksum of the tarball.
+cat ./generic.json | jshon -s "$tarhash" -i checksum > $layerPath/json
+
+# Indicate to docker that we're using schema version 1.0.
+echo -n "1.0" > $layerPath/VERSION