-
-
Save qoomon/7c7f16939630cafafceeb83d254194e4 to your computer and use it in GitHub Desktop.
DEPRECATED in favour of `docker image prune` https://docs.docker.com/config/pruning/
This file contains hidden or 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/" -mindepth 2 -type d -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
I mean not paying attention about the first line of the script and try to run it with a default bash shell.
@qoomon Right ! but still doing the job if someone want to customize the operation. Thx !