Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save locvfx/ae9e364bfa981b5328a3a0832f8f6555 to your computer and use it in GitHub Desktop.
Save locvfx/ae9e364bfa981b5328a3a0832f8f6555 to your computer and use it in GitHub Desktop.
One liner for deleting images from a v2 docker registry
#!/usr/bin/env bash
# Credits (based on): J. Elliot Taylor https://gist.github.com/jaytaylor/86d5efaddda926a25fa68c263830dac1
# Changes:
# - iterate over tag list instead of using only first one
# - optional: basic auth
set -e
set -o pipefail
readonly VER=1.0.0
REGISTRY_URL='http://localhost:5000'
REGISTRY_CONTAINER=registry
# Remove the longest `*/` prefix
readonly SCRIPT_FULL_NAME="${0##*/}"
usage() {
cat <<HEREDOC
NAME
$SCRIPT_FULL_NAME -- remove the image(s) from the local Docker Registry
SYNOPSIS
$SCRIPT_FULL_NAME [-r registry_url] [-c container] [-u user:password] image_name [tag 1] [tag 2] ...
$SCRIPT_FULL_NAME -h: display this help
DESCRIPTION
Removes the tagged image(s) from the local Docker Registry.
The following options are available:
image_name image name in the Registry
tag1 ... (optional) remove specific tagged image. If not specified, remove **all** tagged images.
-r registry_url (optional) Registry URL, defaults to $REGISTRY_URL
-c container (optional) Registry container name or id, defaults to \`$REGISTRY_CONTAINER\`
-u (optional) Registry credentials
ENVIRONMENT
* Docker Registry container running locally.
EXAMPLES
$ $SCRIPT_FULL_NAME my_app 1.4.1
v$VER
HEREDOC
exit 1
}
# If a character is followed by a colon, the option is expected to have an argument
while getopts r:c:u:h OPT; do
case "$OPT" in
r)
readonly REGISTRY_URL="$OPTARG"
;;
c)
readonly REGISTRY_CONTAINER=$OPTARG
;;
u)
readonly CRED_OPT=(--user "$OPTARG")
;;
*) # -h or `?`: an unknown option
usage
;;
esac
done
echo -e "\n[$(date +'%T %Z') v$VER] ${USER:-${USERNAME:-${LOGNAME:-UID #$UID}}}@${HOSTNAME} ${PWD}> $0${*+ }$*\n"
shift $((OPTIND - 1))
# Process positional parameters
readonly IMAGE=$1
if [[ ! $IMAGE ]]; then
usage
fi
shift
while (($# > 0)); do
tags+=("$1")
shift
done
if ((${#tags[@]} == 0)); then
# Get tag list
tag_list=$(curl "${CRED_OPT[@]}" --silent --show-error "${REGISTRY_URL}/v2/${IMAGE}/tags/list" | jq -r '.tags[]?')
readarray -t tags <<<"$tag_list"
fi
# check for empty tag list, e.g. when already cleaned up
if ((${#tags[@]} == 0)); then
echo "No $IMAGE images found"
exit
fi
deleted=false
for tag in "${tags[@]}"; do
image_digest=$(curl --head --header "Accept: application/vnd.docker.distribution.manifest.v2+json" "${CRED_OPT[@]}" \
--silent --show-error "${REGISTRY_URL}/v2/${IMAGE}/manifests/${tag}" \
| awk '$1 == "Docker-Content-Digest:" { print $2 }' \
| tr -d $'\r'
)
if [[ $image_digest ]]; then
echo "DELETING $IMAGE:$tag"
curl --request DELETE "${CRED_OPT[@]}" --silent --show-error "${REGISTRY_URL}/v2/${IMAGE}/manifests/${image_digest}"
deleted=true
echo "DELETED $IMAGE:$tag"
else
echo "No $IMAGE:$tag image found"
fi
done
if [[ $deleted == true ]]; then
echo -e "\nCleaning up after deletion\n"
# -m delete manifests that are not currently referenced via tag
docker exec -it "$REGISTRY_CONTAINER" bin/registry garbage-collect /etc/docker/registry/config.yml -m
echo -e "\nRestarting Registry"
docker restart "$REGISTRY_CONTAINER"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment