简体   繁体   中英

How to check if a Docker image with a specific tag exist on registry?

I want to check if docker image with a specific tag exist on registry.

I saw this post: how-to-check-if-a-docker-image-with-a-specific-tag-exist-locally

But it handles images on local system.

How can I use docker image inspect (or other commands) to check if image with specific tag exists on remote registry?

I found the way without pulling:

curl -X GET http://my-registry/v2/image_name/tags/list

where:

  • my-registry - registry name
  • image_name - the image name I search for

The result shows all the tags in the registry

Another possibility is to use docker pull - if the exit code is 1, it doesn't exist. If the exit code is 0, it does exist.

docker pull <my-registry/my-image:my-tag>
echo $?  # print exit code

Disadvantage: If the image actually exists (but not locally), it will pull the whole image, even if you don't want to. Depending on what you actually want to do and achieve, this might be a solution or waste of time.

There is docker search but it only works with Docker Hub. A universal solution will be a simple shell script with docker pull :

#!/bin/bash

function check_image() {
    # pull image
    docker pull $1 >/dev/null 2>&1
    # save exit code
    exit_code=$?
    if [ $exit_code = 0 ]; then
        # remove pulled image
        docker rmi $1 >/dev/null 2>&1
        echo Image $1 exists!
    else
        echo "Image $1 does not exist :("
    fi
}
check_image hello-world:latest
# will print 'Image hello-world:latest exists!'
check_image hello-world:nonexistent
# will print 'Image hello-world:nonexistent does not exist :('

The downsides of the above are slow speed and free space requirement to pull an image.

If you are using AWS ECR you can use the solution provided here https://gist.github.com/outofcoffee/8f40732aefacfded14cce8a45f6e5eb1

This uses the AWS CLI to query the ECR and will use whatever credentials you have configured. This could make it easier for you as you will not need to directly worry about credentials for this request if you are already using them for AWS.

Copied the solution from the gist here

#!/usr/bin/env bash
# Example:
#    ./find-ecr-image.sh foo/bar mytag

if [[ $# -lt 2 ]]; then
    echo "Usage: $( basename $0 ) <repository-name> <image-tag>"
    exit 1
fi

IMAGE_META="$( aws ecr describe-images --repository-name=$1 --image-ids=imageTag=$2 2> /dev/null )"

if [[ $? == 0 ]]; then
    IMAGE_TAGS="$( echo ${IMAGE_META} | jq '.imageDetails[0].imageTags[0]' -r )"
    echo "$1:$2 found"
else
    echo "$1:$2 not found"
    exit 1
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM