Skip to content

Instantly share code, notes, and snippets.

@bigglesandginger
Last active December 7, 2016 18:22
Show Gist options
  • Save bigglesandginger/f2ce3f4476c7329de27856f4784fd9ce to your computer and use it in GitHub Desktop.
Save bigglesandginger/f2ce3f4476c7329de27856f4784fd9ce to your computer and use it in GitHub Desktop.
Docker server's garbage collect does not delete old revisions of a tag that are inaccesible. This script will clean up the stranded revisions left behind in a docker registry to allow docker server's garbage collector to perform its task thouroughly
#! /bin/bash
HELP=true
SOURCE=""
REPOSITORY="http://localhost:5000"
SOURCE_FOUND=false
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-r)
REPOSITORY="$2"
shift
;;
-h|--help)
HELP=true
;;
*)
if $SOURCE_FOUND; then
HELP=true
else
HELP=false
fi
SOURCE="$1"
SOURCE_FOUND=true
;;
esac
shift # past argument or value
done
if $HELP; then
echo "Usage: $0 [-r url] SOURCE
SOURCE location of the repositories directory eg:
/mnt/docker/registry/v2/repositories
-r url url to connect to the docker repository
defaults to: http://localhost:5000
"
exit 1
fi
echo "Working on directory: $SOURCE and Docker Server: $REPOSITORY"
for repo in $(curl -s $REPOSITORY/v2/_catalog | jq -r '.repositories[]'); do
echo "Repo: $repo"
for tag in $(curl -s $REPOSITORY/v2/$repo/tags/list | jq -r '.tags[]'); do
echo " tag: $tag"
current=$(cat $SOURCE/$repo/_manifests/tags/$tag/current/link | sed 's/sha256://')
count=0
for sha in $(ls -1 $SOURCE/$repo/_manifests/tags/$tag/index/sha256); do
if [ "$current" = "$sha" ]; then
echo " found current"
else
count=`expr $count + 1`
UNWANTED_REVISION=$SOURCE/$repo/_manifests/revisions/sha256/$sha/link
UNWANTED_TAG=$SOURCE/$repo/_manifests/tags/$tag/index/sha256/$sha
if [ -f $UNWANTED_REVISION ]; then
rm -rf $UNWANTED_REVISION
fi
if [ -d $UNWANTED_TAG ]; then
rm -rf $UNWANTED_TAG
fi
fi
done
echo " removed $count stranded revisions"
done
done
echo "run garbage collection to clean up the docker server
eg: registry garbage-collect /etc/docker/registry/config.yml
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment