-
-
Save B-Stefan/6cecaab4793db9655b8f4bfe26644c74 to your computer and use it in GitHub Desktop.
Docker Registry v2 / Remove orphan layers left by the "file" storage backend of the docker registry, heavily inspired by https://gist.github.com/shepmaster/53939af82a51e3aa0cd6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
base_dir=/var/lib/registry/docker/registry/v2 | |
repository_dir=$base_dir/repositories | |
image_dir=$base_dir/blobs | |
output_dir=$(mktemp -d) | |
all_images=$output_dir/all | |
used_images=$output_dir/used | |
unused_images=$output_dir/unused | |
find "$image_dir/sha256/" -type d -mindepth 2 -exec basename {} \; > "$all_images" | |
echo "Collecting orphan images">&2 | |
for library in $repository_dir/*; do | |
echo "Library $(basename "$library")" >&2 | |
for repo in $library/*; do | |
echo " Repo $(basename "$repo")" >&2 | |
for tag in $repo/_manifests/tags/*; do | |
echo " Tag $(basename "$tag")" >&2 | |
image_hash=$(basename $tag/index/sha256/*) | |
echo " Image $image_hash" >&2 | |
echo "$image_hash" | |
jq -r '.config.digest' "$image_dir/sha256/${image_hash:0:2}/$image_hash/data" | sed 's|sha256:||' | |
jq -r '.layers[].digest' "$image_dir/sha256/${image_hash:0:2}/$image_hash/data" | sed 's|sha256:||' | |
done | |
done | |
done | sort | uniq > "$used_images" | |
grep -v -F -f "$used_images" "$all_images" > "$unused_images" | |
all_image_count=$(wc -l < "$all_images") | |
used_image_count=$(wc -l < "$used_images") | |
unused_image_count=$(wc -l < "$unused_images") | |
unused_image_size='0' | |
echo "${all_image_count} images, ${used_image_count} used, ${unused_image_count} unused" | |
if [ "$unused_image_count" -gt 0 ]; then | |
unused_image_folders="" | |
for image_hash in $(cat "$unused_images"); do | |
unused_image_folders="$unused_image_folders $image_dir/sha256/${image_hash:0:2}/$image_hash" | |
done | |
unused_image_size=$(du -hc $unused_image_folders | tail -n1 | cut -f1) | |
echo "Unused images consume ${unused_image_size}" | |
echo "Delete unused Images? [y/n]" | |
read confirmation | |
if [ "$confirmation" == "y" ]; then | |
echo "Cleanup images" | |
for image_hash in $(cat "$unused_images"); do | |
rm -rf "$image_dir/sha256/${image_hash:0:2}/$image_hash" | |
done | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment