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



我想检查注册表上是否存在带有特定标签的docker图像

我看到这个帖子:how-to-check-if-a-docker-image-with-a-specific-tag-exist-locally

但是它处理本地系统上的图像。

如何使用docker image inspect(或其他命令)来检查远程注册表上是否存在具有特定标记的图像?

不用拉我就找到路了:

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

地点:

  • my-registry -注册表名
  • image_name -我搜索的图像名称

结果显示了注册表

中的所有标记

另一种可能性是使用docker pull—如果退出代码为1,则它不存在。如果退出码为0,则表示退出码存在。

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

缺点:如果图像实际存在(但不是局部存在),它将拉出整个图像,即使您不想这样做。这取决于你真正想要做什么和实现什么,这可能是一个解决方案,也可能是浪费时间。

docker search,但它只适用于Docker Hub。通用的解决方案是使用docker pull:

编写一个简单的shell脚本:
#!/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 :('

上面的缺点是速度慢,并且需要空闲空间来提取图像。

如果您正在使用AWS ECR,您可以使用这里提供的解决方案https://gist.github.com/outofcoffee/8f40732aefacfded14cce8a45f6e5eb1

这将使用AWS CLI查询ECR,并将使用您已配置的任何凭据。这可以使您更容易,因为如果您已经在AWS上使用凭据,则无需直接担心此请求的凭据。

从这里的要点复制解决方案

#!/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

最新更新