一尘不染

检查docker hub上是否已存在image:tag组合

docker

作为bash脚本的一部分,我想检查docker hub上是否存在特别的docker image:tag组合。同样,它将是一个私有存储库。

即伪代码将是这样的:

tag = something
if image:tag already exists on docker hub:
    Do nothing
else
    Build and push docker image with that tag

阅读 537

收藏
2020-06-17

共1个答案

一尘不染

请试试这个

function docker_tag_exists() {
    curl --silent -f -lSL https://index.docker.io/v1/repositories/$1/tags/$2 > /dev/null
}

if docker_tag_exists library/nginx 1.7.5; then
    echo exist
else 
    echo not exists
fi

更新:

如果使用Docker Registry
v2(基于):

# set username and password
UNAME="user"
UPASS="password"

function docker_tag_exists() {
    TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
    EXISTS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/$1/tags/?page_size=10000 | jq -r "[.results | .[] | .name == \"$2\"] | any")
    test $EXISTS = true
}

if docker_tag_exists library/nginx 1.7.5; then
    echo exist
else 
    echo not exists
fi
2020-06-17