-
-
Save matzegebbe/a2678b227add6bafad9a3a802618b5ad to your computer and use it in GitHub Desktop.
#!/bin/bash | |
REPO_URL="https://repository.xxx.net/repository/" | |
USER="admin" | |
PASSWORD="datpassword" | |
BUCKET="portal-docker" | |
KEEP_IMAGES=10 | |
IMAGES=$(curl --silent -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/_catalog" | jq .repositories | jq -r '.[]' ) | |
echo ${IMAGES} | |
for IMAGE_NAME in ${IMAGES}; do | |
echo ${IMAGE_NAME} | |
TAGS=$(curl --silent -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/tags/list" | jq .tags | jq -r '.[]' ) | |
TAG_COUNT=$(echo $TAGS | wc -w) | |
let TAG_COUNT_DEL=${TAG_COUNT}-${KEEP_IMAGES} | |
COUNTER=0 | |
echo "THERE ARE ${TAG_COUNT} IMAGES FOR ${IMAGE_NAME}" | |
## skip if smaller than keep | |
if [ "${KEEP_IMAGES}" -gt "${TAG_COUNT}" ] | |
then | |
echo "There are only ${TAG_COUNT} Images for ${IMAGE_NAME} - nothing to delete" | |
continue | |
fi | |
for TAG in ${TAGS}; do | |
let COUNTER=COUNTER+1 | |
if [ "${COUNTER}" -gt "${TAG_COUNT_DEL}" ] | |
then | |
break; | |
fi | |
IMAGE_SHA=$(curl --silent -I -X GET -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} "${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/manifests/$TAG" | grep Docker-Content-Digest | cut -d ":" -f3 | tr -d '\r') | |
echo "DELETE ${TAG} ${IMAGE_SHA}"; | |
DEL_URL="${REPO_URL}${BUCKET}/v2/${IMAGE_NAME}/manifests/sha256:${IMAGE_SHA}" | |
RET="$(curl --silent -k -X DELETE -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -u ${USER}:${PASSWORD} $DEL_URL)" | |
done; | |
done; |
Hello, Thanks a lot for this gist, it was exactly what I was looking for !
Anyway I'm facing an issue with the storage on disk because the image is deleted in nexus (not visible in the UI) but the disk space does not decrease Is there a way to really remove the image and layers from disk ? Am I missing a parameter in my Nexus configuration to really remove the images ?
My nexus version is 3.10 and even after running the "Docker - Delete unused manifests and images" task the disk space remains the same.
Thanks for your help.
You may need to consider running a "Compact blob store" task -- https://help.sonatype.com/repomanager3/nexus-repository-administration/repository-management/cleanup-policies?_ga=2.95007263.285540370.1704304621-1985157648.1703700486
How can I add a filter to remove images that are older than two weeks?
Be aware that the ordering of tags might not what you think! So probably 10 old builds are kept and new ones are deleted!
e.g.
https://your-repo.com/repository/docker-repo/v2/image-name/tags/list
{
"name": "image-name",
"tags": [
"0.1.12",
"0.1.14",
"0.1.16",
"0.1.18",
"0.1.20",
"0.1.22",
"0.1.4",
"0.1.5",
"0.1.689",
"0.1.690",
"0.1.691",
"0.1.692",
"0.1.693",
"0.1.7",
"0.1.8",
"0.1.9",
"latest"
]
}
I think this shouldn't be based on tags .. but created datetime. We use commit hashes as versions for example
Install jq on the machine