Docker Registry Maintenance

One
Diarpi
December, 2017

Made this "clean-up" script after almost running out of disk space on the Docker registry.
Simply had too many snapshot / CI made / test versions!

The script (named delete.sh) will accept three parameters; registry URL, repository name and docker tag, and issue a DELETE request using curl.
Script should output "202 Accepted" on success.

The script was created and tested on Ubuntu 16.04 system.

#!/bin/bash
# usage ./delete.sh <registry> <repository> <tag>
# eg.: ./delete.sh https://myregistry.example.com:5000 myrepo 1.0.42-SNAPSHOT

REGISTRY=$1
REPO=$2
TAG=$3

if [[ -z $REGISTRY ]] || [[ -z $REPO ]] || [[ -z $TAG ]];
then
  echo "Missing parameter(s) REGISTRY/REPOSITORY/TAG"
  echo ""
  echo "usage ./delete.sh <registry> <repository> <tag>"
  echo " eg.: ./delete.sh https://myregistry.example.com:5000 myrepo 1.0.42-SNAPSHOT"
  exit 1
fi

# Get docker image hash
HASH=$(curl -sI -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X GET ${REGISTRY}/v2/${REPO}/manifests/${TAG} | grep "Docker-Content-Digest" | awk '{ print $2 }')

# Remove any \r from the delete URL, else curl would fail
DELETE_URL=$(echo "${REGISTRY}/v2/${REPO}/manifests/${HASH}" |sed -e "s/\r//")

# Delete docker image
curl -sI -X DELETE ${DELETE_URL} |grep "HTTP"

Now run the garbage collect.
It will mark and then delete unnecessary blobs from the docker repository:

docker exec my_registry bin/registry garbage-collect /etc/docker/registry/config.yml

Assumed that the registry container is named "my_registry" and the registry config file location is "/etc/docker/registry/config.yml"

The disk disk space should now be free !