Forked from mohamed-el-habib/clean.docker.registry.sh
Last active
December 12, 2022 08:53
-
-
Save tahazayed/98b7734da6dd23f8a9e167806891613d to your computer and use it in GitHub Desktop.
bash script to delete images from docker registry using search keyword
This file contains hidden or 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 | |
# Please follow this artical to allow registry deleation https://azizunsal.github.io/blog/post/delete-images-from-private-docker-registry/#override-the-registry-configuration | |
# Usage ./clean.docker.registry.sh -r registryUrl -u login -f stringFilter -t tagToKeep | |
SHORT=r:,u:,f:,k:,h | |
LONG=dockerRegistry:,user:,imagesFilter:,keepTag:,help | |
OPTS=$(getopt -a -n clean.docker.regisrty.sh --options $SHORT --longoptions $LONG -- "$@") | |
eval set -- "$OPTS" | |
while : | |
do | |
case $1 in | |
-r | --dockerRegistry ) | |
dockerRegistry="$2" | |
shift 2 | |
;; | |
-u | --user ) | |
user="$2" | |
shift 2 | |
;; | |
-f | --imagesFilter ) | |
imagesFilter="$2" | |
shift 2 | |
;; | |
-k | --keepTag ) | |
keepTag="$2" | |
shift 2 | |
;; | |
-h | --help) | |
"#usage ./clean.docker.registry.sh -r registryUrl -u login -f stringFilter -t tagToKeep" | |
exit 2 | |
;; | |
--) | |
shift; | |
break | |
;; | |
*) | |
echo "Unexpected option: $1" | |
;; | |
esac | |
done | |
echo -e "\nDocker Registry: ${dockerRegistry}" | |
echo -e "User: ${user}" | |
echo -e "ImagesFilter: ${imagesFilter}" | |
# read the password | |
echo -n Password: | |
read -s password | |
AUTH=$(echo -ne "${user}:${password}" | base64 --wrap 0) | |
# get the list of images names that match the filter | |
images=$(curl -H "Authorization: Basic ${AUTH}" -s --insecure ${dockerRegistry}/v2/_catalog | jq -r '.repositories[] | select(. | contains("'${imagesFilter}'"))') | |
for image in $images ; do | |
# get the list of tags for each image | |
tags=$(curl -H "Authorization: Basic ${AUTH}" -s --insecure ${dockerRegistry}/v2/${image}/tags/list | jq -r .tags[] | sort -r) | |
for tag in $tags ; do | |
if [[ "${tag}" != "${keepTag}" ]]; then | |
echo "${image}:${tag}" | |
# get the digest of the image:tag | |
digest=$(curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -H "Authorization: Basic ${AUTH}" -v -s --insecure "${dockerRegistry}/v2/${image}/manifests/${tag}" 2>&1 | grep -e "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" -H "Authorization: Basic ${AUTH}" --insecure ${dockerRegistry}'/v2/'${image}'/manifests/'${digest} | |
echo "..." | |
fi | |
else | |
echo "Keep ${image}:${tag}" | |
fi | |
done | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment