Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save djosip/1c65c95d9a84acf696851451814529dd to your computer and use it in GitHub Desktop.
Save djosip/1c65c95d9a84acf696851451814529dd to your computer and use it in GitHub Desktop.
Clean up untagged manifests in private docker registry
#!/bin/bash
REGISTRY_DIR=/storage/data/docker-registry/docker/registry/v2/repositories
REGISTRY_URL=http://127.0.0.1:5000
# Set CURLOPTS to "--insecure" to disable TLS certificate verification
CURLOPTS=""
if [ -z "$REGISTRY_DIR" -o ! -d "$REGISTRY_DIR" ]; then
echo "$0: error: REGISTRY_DIR not specified or does not exist"
exit 1
fi
export LC_ALL=C
set -o pipefail
# Turn on nullglob
shopt -s nullglob
manifests_without_tags=$(comm -23 <(find ${REGISTRY_DIR}/{*,*/*}/_manifests/revisions/sha256 -type f -name link -printf '%h\n' | \
awk -F "/" '{print $(NF)}' | \
sort) \
<(find ${REGISTRY_DIR}/{*,*/*}/_manifests/tags/*/current -type f -name link -exec sed -e 's/^sha256://g' -e '/.*/a\' {} \; | \
sort))
if [ $? -ne 0 ]; then
echo "$0: error: failed to analyze manifests"
exit 1
fi
total_count=$(echo ${manifests_without_tags} | wc -w)
count=0
REGISTRY_DIR_escaped="$(echo ${REGISTRY_DIR}/ | sed 's/\//\\\//g')"
for manifest in ${manifests_without_tags};
do
repo="$(find ${REGISTRY_DIR}/{*,*/*}/_manifests/revisions/sha256/${manifest} -type f -name link | \
sed -e "s/${REGISTRY_DIR_escaped}//" -e "s/\/_manifests\/revisions\/sha256\/${manifest}\/link//")"
if [ $? -ne 0 ]; then
echo "$0: warning: failed to find the repository name for manifest $manifest"
continue
fi
curl -s -X DELETE ${CURLOPTS} "${REGISTRY_URL}/v2/${repo}/manifests/sha256:${manifest}" > /dev/null
retval=$?
if [ $retval -ne 0 ]; then
echo "$0: warning: failed to delete $repo using curl on $REGISTRY_URL (retval: $retval)"
fi
((count++))
echo "$0: Deleted $count of $total_count manifests, manifest for repo \"$repo\" deleted."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment