0

How do you remove an image from the registry in IBM Cloud Private 2.1.0? In 1.2 you could remove an image from the UI. How is it done in the current version?

Damien Gu
  • 77
  • 8
jimc
  • 41
  • 5

3 Answers3

1

I've written a script to help with removal of images via CLI. It will allow you to remove images within your ICP registry based on jq CLI contains patterns. This script is intended to be run on your ICP master node. Save off the snippet, chmod +x, update the vars for your creds and enjoy!

Example Invocations:

./remove-registry-image.sh label
Result :: Removes all images which include that label in the namespace or image

./remove-registry-image.sh namespace/image-label
Result :: Removes a specific image-label located in namespace

#!/bin/bash
# ----------------------------------------------------------------------------------------------------\\
# Description:
#   A basic script port to delete registry images on an ICP Cluster
#   Fork of https://gist.github.com/mohamed-el-habib/26d26dddaf3dcefcc0e6bdd8a15bd681
#   Reference: https://www.ibm.com/support/knowledgecenter/en/SSBS6K_2.1.0/apis/docker_registry_api.html
#   
#   usage ./remove-registry-image.sh filterString
#   
#   ./remove-registry-image.sh nameSpace/textString
#
# ----------------------------------------------------------------------------------------------------\\

set -e

##########
# Colors##
##########
Green='\x1B[0;32m'
Red='\x1B[0;31m'
Yellow='\x1B[0;33m'
Cyan='\x1B[0;36m'
no_color='\x1B[0m' # No Color
beer='\xF0\x9f\x8d\xba'
delivery='\xF0\x9F\x9A\x9A'
beers='\xF0\x9F\x8D\xBB'
eyes='\xF0\x9F\x91\x80'
cloud='\xE2\x98\x81'
crossbones='\xE2\x98\xA0'
litter='\xF0\x9F\x9A\xAE'
fail='\xE2\x9B\x94'
harpoons='\xE2\x87\x8C'
tools='\xE2\x9A\x92'
present='\xF0\x9F\x8E\x81'
#############

export ICPUSER=admin
export ICPPW=admin

# Get the variables
clear

echo -e "${tools}   Welcome to the delete registry script for an IBM Cloud Private Cluster v2 Docker Registry"

if [ "${OS}" == "rhel" ]; then
  sudo yum install epel-release -y
  sudo yum install jq -y
else
  sudo apt-get -qq install jq -y
fi

dockerRegistry='mycluster.icp'
dockerRegistryPort='8500'
user="$ICPUSER:$ICPPW"
imagesFilter="$1"

# get the list of images names that match the filter
CATALOG_TOKEN=$(curl --cacert $HOME/.kube/kubecfg.crt -u ${user} -ks "https://${dockerRegistry}:8443/image-manager/api/v1/auth/token?service=token-service&scope=registry:catalog:*" | jq -r '.token')
images=$(curl --cacert $HOME/.kube/kubecfg.crt -ks -H "Authorization: Bearer ${CATALOG_TOKEN}" "https://${dockerRegistry}:${dockerRegistryPort}/v2/_catalog" | jq -r '.repositories[]? | select(. | contains("'${imagesFilter}'")) ')

for image in $images ; do

    # get the list of tags for each image    
    TAG_TOKEN=$(curl --cacert $HOME/.kube/kubecfg.crt -u ${user} -ks "https://${dockerRegistry}:8443/image-manager/api/v1/auth/token?service=token-service&scope=repository:${image}:*" | jq -r '.token') 
    tags=$(curl --cacert $HOME/.kube/kubecfg.crt -ks -H "Authorization: Bearer ${TAG_TOKEN}" "https://${dockerRegistry}:${dockerRegistryPort}/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 --cacert $HOME/.kube/kubecfg.crt -ks -H "Authorization: Bearer ${TAG_TOKEN}" "https://${dockerRegistry}:${dockerRegistryPort}/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 -e "${litter}  Deleting ${image}:${tag}:${digest}"
            curl -XDELETE -w "[%{http_code}]\n" --cacert $HOME/.kube/kubecfg.crt -ks -H "Authorization: Bearer ${TAG_TOKEN}" "https://${dockerRegistry}:${dockerRegistryPort}/v2/${image}/manifests/${digest}"
        fi
        echo "...."
    done
done

if [ -n "${images}" ]; then
  echo -e "${fail}   No images matching provided filterString (${imagesFilter}) found"
else
  echo -e "${beers}   Congrats!  All images matching chosen filterString (${imagesFilter}) have been deleted"
fi
Sanjay.Joshi
  • 304
  • 1
  • 12
0

Yes, in 2.1.0 removing an image can't be done by UI. UI will have that feature back in future release. For upcoming release, You would be able to do this from API. Knowledge Center document will have updated information for this.

Anwarul
  • 21
  • 1
-1

from docker CLI directly, list them first:

docker images

remove one of them:

docker rmi -f the-image-id

remove all of them:

docker rmi -f $(docker images -a -q)
SBERENS
  • 68
  • 7