Skip to content

Instantly share code, notes, and snippets.

@agross
Created January 31, 2022 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agross/270ce4b5070e378289624566ed192d5f to your computer and use it in GitHub Desktop.
Save agross/270ce4b5070e378289624566ed192d5f to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
set -e
repositories=/var/lib/registry/docker/registry/v2/repositories/
printf 'Running Docker Registry garbage collection\n'
if ! docker container \
exec \
docker-registry_registry_1 \
stat "$repositories" 2> /dev/null; then
printf 'No repositories at %s\n' "$repositories"
exit
fi
docker container \
exec \
docker-registry_registry_1 \
registry \
garbage-collect \
/etc/docker/registry/config.yml
printf 'Deleting empty repositories\n'
# Delete directories where their _manifests/tags subdirectory is empty.
# Busybox find does not support -empty, emulate it using find and wc.
# After finding repos to delete we need to delete them in a second pass.
# The second pass is required because find of the first pass would try to
# traverse the directories we just deleted.
docker container \
exec \
docker-registry_registry_1 \
find "$repositories" \
-depth \
-mindepth 1 \
-type d \
-path '*/_manifests/tags' \
-exec sh -ec '[ $(find "$1" -maxdepth 1 -type d | wc -l) = 1 ] && printf "%s\0" "${1%_manifests/tags}"' - {} \; |
xargs -0 \
-r \
-I{} \
docker container \
exec \
docker-registry_registry_1 \
rm -rf {}
# Delete empty directories.
docker container \
exec \
docker-registry_registry_1 \
find "$repositories" \
-depth \
-mindepth 1 \
-type d \
-exec rmdir {} 2>/dev/null \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment