[英]How to check if a Docker image with a specific tag exist on registry?
我想检查注册表中是否存在具有特定标签的 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
在哪里:
结果显示注册表中的所有标签
另一种可能性是使用docker pull
- 如果退出代码为 1,则它不存在。 如果退出代码为 0,则它确实存在。
docker pull <my-registry/my-image:my-tag>
echo $? # print exit code
缺点:如果图像确实存在(但不是本地),它会拉整个图像,即使你不想这样做。 根据您实际想要做和实现的目标,这可能是一个解决方案或浪费时间。
有docker search
,但它仅适用于 Docker 集线器。 一个通用的解决方案将是一个简单的 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 :('
上述方法的缺点是拉动图像的速度慢且需要可用空间。
如果您使用 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.