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

5

6

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 ?

Desmund answered 21/1, 2021 at 9:57 Comment(2)
Does this answer your question? Check if image:tag combination already exists on docker hubVanir
See #69085488Peonir
D
5

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

Desmund answered 21/1, 2021 at 11:34 Comment(4)
A good one! How did you pass authentication with this method?Cambell
It didn't asked meDesmund
For anyone that stumbles into this some day; I discovered how to auth registries that restrict access here; if you get denied there's a WWW-Authenticate response; if it's basic then you need to pass basic auth (probably your username, access token) to access this endpoint. If it's bearer, you likely need to perform a GET to the URL referenced in that header with basic auth attached there. That GET responds with a token then you put into "Authorization: Bearer <token>"Hombre
Found this by finding the "spec tests" for OCI here which led me to this code to understand the difference between bearer basic scheme for OCI (Open Container standard)Hombre
A
2

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.

Alvie answered 5/5, 2022 at 23:0 Comment(0)
C
1

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.

Cambell answered 21/1, 2021 at 10:51 Comment(0)
N
0

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
Navigable answered 2/9, 2022 at 8:44 Comment(0)
D
0

When using Azure Container Registry you can use the Azure CLI to probe image tags without having to pull.

$ az acr repository show -n myregistry --image path/to/image:some-tag
Danialdaniala answered 17/10 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.