簡體   English   中英

如何檢查注冊表中是否存在具有特定標簽的 Docker 圖像?

[英]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

在哪里:

  • my-registry - 注冊表名稱
  • image_name - 我搜索的圖像名稱

結果顯示注冊表中的所有標簽

另一種可能性是使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM