Forked from mohamed-el-habib/clean.docker.registry.sh
Created
February 11, 2018 22:49
-
-
Save joshisa/b87804d03a81bd85a95f5989423d71fd to your computer and use it in GitHub Desktop.
bash script to delete images from docker registry using search keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# usage ./clean.docker.registry.sh registryUrl login filterString | |
# | |
# read the password | |
echo -n Password: | |
read -s password | |
user="$2:${password}" | |
dockerRegistry="$1" | |
imagesFilter="$3" | |
# get the list of images names that match the filter | |
images=$(curl -s -u ${user} ${dockerRegistry}/v2/_catalog | jq -r '.repositories[] | select(. | contains("'${imagesFilter}'")) ') | |
for image in $images ; do | |
# get the list of tags for each image | |
tags=$(curl -s -u ${user} ${dockerRegistry}/v2/${image}/tags/list | jq -r .tags[]) | |
for tag in $tags ; do | |
echo "${image}:${tag}" | |
# get the digest of the image:tag | |
digest=$(curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -v -s -u ${user} "${dockerRegistry}/v2/${image}/manifests/${tag}" 2>&1 | grep -e "Docker-Content-Digest:*" | awk '{ sub(/\r/,"",$3) ; print $3 }') | |
if [ -z $digest ] ; then | |
echo "${image}:${tag} not found" | |
else | |
echo "Deleting ${image}:${tag}:${digest}" | |
curl -XDELETE -w "[%{http_code}]\n" -s -u ${user} ${dockerRegistry}'/v2/'${image}'/manifests/'${digest} | |
echo "..." | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment