1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-11-24 22:50:49 +00:00

Merge pull request #87154 from utdemir/buildimage-optimizations

Some performance optimizations to dockerTools.build{,Layered}Image
This commit is contained in:
lewo 2020-05-19 15:39:25 +02:00 committed by GitHub
commit a498da343a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 39 deletions

View file

@ -392,14 +392,10 @@ rec {
(cd layer; eval "$extraCommands") (cd layer; eval "$extraCommands")
fi fi
# Tar up the layer and throw it into 'layer.tar'. # Tar up the layer and throw it into 'layer.tar', while calculating its checksum.
echo "Packing layer..." echo "Packing layer..."
mkdir $out mkdir $out
tar --transform='s|^\./||' -C layer --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=${toString uid} --group=${toString gid} -cf $out/layer.tar . tarhash=$(tar --transform='s|^\./||' -C layer --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=${toString uid} --group=${toString gid} -cf - . | tee $out/layer.tar | tarsum)
# Compute a checksum of the tarball.
echo "Computing layer checksum..."
tarhash=$(tarsum < $out/layer.tar)
# Add a 'checksum' field to the JSON, with the value set to the # Add a 'checksum' field to the JSON, with the value set to the
# checksum of the tarball. # checksum of the tarball.
@ -449,11 +445,7 @@ rec {
# Tar up the layer and throw it into 'layer.tar'. # Tar up the layer and throw it into 'layer.tar'.
echo "Packing layer..." echo "Packing layer..."
mkdir $out mkdir $out
tar -C layer --hard-dereference --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=${toString uid} --group=${toString gid} -cf $out/layer.tar . tarhash=$(tar -C layer --hard-dereference --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=${toString uid} --group=${toString gid} -cf - . | tee $out/layer.tar | tarsum)
# Compute a checksum of the tarball.
echo "Computing layer checksum..."
tarhash=$(tarsum < $out/layer.tar)
# Add a 'checksum' field to the JSON, with the value set to the # Add a 'checksum' field to the JSON, with the value set to the
# checksum of the tarball. # checksum of the tarball.
@ -537,11 +529,10 @@ rec {
echo "Packing layer..." echo "Packing layer..."
mkdir -p $out mkdir -p $out
tar -C layer --hard-dereference --sort=name --mtime="@$SOURCE_DATE_EPOCH" -cf $out/layer.tar . tarhash=$(tar -C layer --hard-dereference --sort=name --mtime="@$SOURCE_DATE_EPOCH" -cf - . |
tee $out/layer.tar |
${tarsum}/bin/tarsum)
# Compute the tar checksum and add it to the output json.
echo "Computing checksum..."
tarhash=$(${tarsum}/bin/tarsum < $out/layer.tar)
cat ${baseJson} | jshon -s "$tarhash" -i checksum > $out/json cat ${baseJson} | jshon -s "$tarhash" -i checksum > $out/json
# Indicate to docker that we're using schema version 1.0. # Indicate to docker that we're using schema version 1.0.
echo -n "1.0" > $out/VERSION echo -n "1.0" > $out/VERSION

View file

@ -11,39 +11,35 @@ echo "Creating layer #$layerNumber for $@"
mkdir -p "$layerPath" mkdir -p "$layerPath"
# Make sure /nix and /nix/store appear first in the archive. # Make sure /nix and /nix/store appear first in the archive.
#
# We create the directories here and use them because # We create the directories here and use them because
# when there are other things being added to the # when there are other things being added to the
# nix store, tar could fail, saying, # nix store, tar could fail, saying,
# "tar: /nix/store: file changed as we read it" # "tar: /nix/store: file changed as we read it"
mkdir -p nix/store mkdir -p nix/store
tar -cf "$layerPath/layer.tar" \
--mtime="@$SOURCE_DATE_EPOCH" \
--owner=0 --group=0 \
--transform='s,nix,/nix,' \
nix
# We change into the /nix/store in order to avoid a similar # Then we change into the /nix/store in order to
# "file changed as we read it" error as above. Namely, # avoid a similar "file changed as we read it" error
# if we use the absolute path of /nix/store/123-pkg # as above. Namely, if we use the absolute path of
# and something new is added to the nix store while tar # /nix/store/123-pkg and something new is added to the nix
# is running, it will detect a change to /nix/store and # store while tar is running, it will detect a change to
# fail. Instead, if we cd into the nix store and copy # /nix/store and fail. Instead, if we cd into the nix store
# the relative nix store path, tar will ignore changes # and copy the relative nix store path, tar will ignore
# to /nix/store. In order to create the correct structure # changes to /nix/store. In order to create the correct
# in the tar file, we transform the relative nix store # structure in the tar file, we transform the relative nix
# path to the absolute store path. # store path to the absolute store path.
for storePath in "$@"; do tarhash=$(
n=$(basename "$storePath") basename -a "$@" |
tar -C /nix/store -rpf "$layerPath/layer.tar" \ tar -cp nix \
-C /nix/store --verbatim-files-from --files-from - \
--hard-dereference --sort=name \ --hard-dereference --sort=name \
--mtime="@$SOURCE_DATE_EPOCH" \ --mtime="@$SOURCE_DATE_EPOCH" \
--owner=0 --group=0 \ --owner=0 --group=0 \
--transform="s,$n,/nix/store/$n," \ --transform 's,^nix(/|$),/nix/,' \
$n --transform 's,^[^/],/nix/store/\0,rS' |
done tee "$layerPath/layer.tar" |
tarsum
# Compute a checksum of the tarball. )
tarhash=$(tarsum < $layerPath/layer.tar)
# Add a 'checksum' field to the JSON, with the value set to the # Add a 'checksum' field to the JSON, with the value set to the
# checksum of the tarball. # checksum of the tarball.