Skip to content

Instantly share code, notes, and snippets.

@gustavoapolinario
Created May 12, 2017 13:56
Show Gist options
  • Save gustavoapolinario/80119176bd00abd47f37428fb0ca6f66 to your computer and use it in GitHub Desktop.
Save gustavoapolinario/80119176bd00abd47f37428fb0ca6f66 to your computer and use it in GitHub Desktop.
SH to list all repositories in docker registry, list all tags of repositorie and remove all images from the tag repositorie
#!/bin/bash
#
# SH to list all repositories in docker registry, list all tags of repositorie and remove all images from the tag repositorie
#
# Created by: Gustavo Apolinario
# 2017-05-12
#
# curl -X GET localhost:5000/v2/_catalog
# curl -X GET localhost:5000/v2/socsite_apache/tags/list
# curl -X GET localhost:5000/v2/socsite_apache/manifests/latest
# curl -X DELETE localhost:5000/v2/socsite_apache/manifests/sha:...
# curl -X DELETE localhost:5000/v2/socsite_apache/blobs/sha:...
# Registry Container name
CONTAINER_REGISTRY='registry'
# IP from your docker registry
SERVIDOR='localhost'
# port of your registry
PORT=5000
_help ()
{
echo "Please execute this way"
echo "$0 --images"
echo "$0 --tags <IMAGE>"
echo "$0 --delete <IMAGE> <TAG>"
exit
}
_get_registry_images ()
{
CATALOG=$(curl --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET $SERVIDOR:$PORT/v2/_catalog)
#for i in ; do
echo "$CATALOG"
#done
}
_get_registry_image_tags ()
{
image=$1
CATALOG=$(curl --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET $SERVIDOR:$PORT/v2/$image/tags/list)
#for i in ; do
echo "$CATALOG"
#done
}
_delete_image ()
{
image=$1
tag=$2
#echo "curl -k -v --silent -H \"Accept: application/vnd.docker.distribution.manifest.v2+json\" -X GET localhost:5000/v2/$image/manifests/$tag 2>&1 | grep Docker-Content-Digest" | awk '{print ($3)}')
DOCKER_PRIMARY_DIGEST=$(curl -k -v --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET $SERVIDOR:$PORT/v2/$image/manifests/$tag 2>&1 | grep Docker-Content-Digest | awk '{print ($3)}')
# Remove digests, but the garbage collector will do it
DOCKER_DIGESTS=$(curl -k -v --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET $SERVIDOR:$PORT/v2/$image/manifests/$tag 2>&1 | grep digest | awk '{print ($2)}')
# Remove digests, but the garbage collector will do it
IFS=$'\n'
for line in $DOCKER_DIGESTS; do
DOCKER_IMAGE=$(echo $line | cut -d'"' -f 2)
if [[ "$DOCKER_IMAGE" != "" ]];then
echo "Deleting Docker digest: $DOCKER_IMAGE"
curl -X DELETE $SERVIDOR:$PORT/v2/$image/blobs/$DOCKER_IMAGE
#curl -X DELETE $SERVIDOR:$PORT/v2/$image/manifests/$DOCKER_IMAGE
fi
done
echo "Deleting Docker primary digest: $DOCKER_PRIMARY_DIGEST"
#curl -X DELETE $SERVIDOR:$PORT/v2/$image/blobs/$DOCKER_PRIMARY_DIGEST
curl -X DELETE $SERVIDOR:$PORT/v2/$image/manifests/$DOCKER_PRIMARY_DIGEST
echo $'\n\n'
echo "Running Garbage Collector to cleanup registry"; echo $'\n'
GARBAGE_COLLECT=""
if [[ "$(expr substr $(uname -s) 1 5)" == "MINGW" ]];then
#windows:
GARBAGE_COLLECT=$(docker exec $CONTAINER_REGISTRY bin/registry garbage-collect --dry-run //etc/docker/registry/config.yml)
else
#linux:
GARBAGE_COLLECT=$(docker exec $CONTAINER_REGISTRY bin/registry garbage-collect --dry-run /etc/docker/registry/config.yml)
fi
echo "$GARBAGE_COLLECT"
# Force Remove not used digests, but the garbage collector should have do it
# echo "
#
#
#"
#
# IFS=$'\n'
# for line in $GARBAGE_COLLECT; do
# DOCKER_IMAGE=$(echo $line | grep 'deletion:' | awk '{print ($5)}')
# if [[ "$DOCKER_IMAGE" != "" ]];then
# echo "Deleting not used Docker image: $DOCKER_IMAGE\n"
# curl -X DELETE $SERVIDOR:$PORT/v2/$image/manifests/$DOCKER_IMAGE
# curl -X DELETE $SERVIDOR:$PORT/v2/$image/blobs/$DOCKER_IMAGE
# fi
# done
#DOCKER_IMAGES=$(echo $GARBAGE_COLLECT | grep 'deletion:')
#echo "\n\n\n $DOCKER_IMAGES"
}
if [ "$1" == "--images" ]; then
if [ "$#" -ne 1 ]; then
_help
else
_get_registry_images
fi
elif [ "$1" == "--tags" ]; then
if [ "$#" -ne 2 ]; then
_help
else
_get_registry_image_tags $2
fi
elif [ "$1" == "--delete" ]; then
if [ "$#" -ne 3 ]; then
_help
else
_delete_image $2 $3
fi
else
_help
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment