Created
March 11, 2017 07:23
-
-
Save mohamed-el-habib/26d26dddaf3dcefcc0e6bdd8a15bd681 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 |
Please replace "Docker-Content-Digest:" with "digest:"
@mohamed-el-habib
Kindly check my fork https://gist.github.com/tahazayed/98b7734da6dd23f8a9e167806891613d
I have added arguments and fixed the search issue
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
curl
command that gets the digest is missing-I
option to get the headers, currently it only outputs the response body.