Skip to content

Instantly share code, notes, and snippets.

@mickaelbaron
Created March 6, 2024 15:12
Show Gist options
  • Save mickaelbaron/976992b9f3f684391b7e7c2e6e3ae8de to your computer and use it in GitHub Desktop.
Save mickaelbaron/976992b9f3f684391b7e7c2e6e3ae8de to your computer and use it in GitHub Desktop.
How to delete Docker image:tag from a Docker Registry?
#!/bin/bash
# Check parameters
if [ $# -eq 0 ]; then
echo "Login and password are missing (user:password)."
exit 1
fi
if [ $# -eq 1 ]; then
echo "Image name is missing."
exit 1
fi
if [ $# -eq 2 ]; then
echo "Image tag is missing."
exit 1
fi
userPassword=$1
imagename=$2
tag=$3
token() {
registryURL="https://PRIVATE_REGISTRY_URL"
realm="$registryURL/dockerauth/auth"
service="Authentication"
scope=$1
authURL="$realm?service=$service&scope=$scope"
token=$(curl -ks -H "Authorization: Basic $(echo -n $userPassword | base64)" "$authURL")
token=$(echo $token | jq .token | tr -d '"')
}
token "repository:$imagename:*"
tokenRepository=$token
dockerContentDigest=$(curl -sSL -I -H "Authorization: Bearer $tokenRepository" -H "Accept: application/vnd.docker.distribution.manifest.v2+json" "${registryURL}/v2/$imagename/manifests/$tag" | awk '$1 == "docker-content-digest:" { print $2 }' | tr -d $'\r')
curl -sSL -H "Authorization: Bearer $tokenRepository" -X DELETE "${registryURL}/v2/$imagename/manifests/${dockerContentDigest}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment