简体   繁体   English

如何检查注册表中是否存在具有特定标签的 Docker 图像?

[英]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.我想检查注册表中是否存在具有特定标签的 docker 图像。

I saw this post: how-to-check-if-a-docker-image-with-a-specific-tag-exist-locally我看到了这篇文章: 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?如何使用docker image inspect (或其他命令)检查远程注册表中是否存在具有特定标签的图像?

I found the way without pulling:我找到了不拉的方式:

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

where:在哪里:

  • my-registry - registry name my-registry - 注册表名称
  • image_name - the image name I search for image_name - 我搜索的图像名称

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.另一种可能性是使用docker pull - 如果退出代码为 1,则它不存在。 If the exit code is 0, it does exist.如果退出代码为 0,则它确实存在。

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.docker search ,但它仅适用于 Docker 集线器。 A universal solution will be a simple shell script with docker pull :一个通用的解决方案将是一个简单的 shell 脚本与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如果您使用 AWS ECR,您可以使用此处提供的解决方案https://gist.github.com/outofcoffee/8f40732aefacfded14cce8a45f6e5eb1

This uses the AWS CLI to query the ECR and will use whatever credentials you have configured.这将使用 AWS CLI 查询 ECR,并将使用您配置的任何凭证。 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.这可以让您更轻松,因为如果您已经将它们用于 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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