检查 docker hub 上是否已经存在 image:tag 组合 [英] Check if image:tag combination already exists on docker hub

查看:43
本文介绍了检查 docker hub 上是否已经存在 image:tag 组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As part of a bash script, I want to check if a particularly docker image:tag combination exists on docker hub. Also, it will be a private repository.

即伪代码如下:

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

推荐答案

请试试这个

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


更新:


Update:

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

In case of usage Docker Registry v2 (based on this):

# 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)
    curl --silent -f --head -lL https://hub.docker.com/v2/repositories/$1/tags/$2/ > /dev/null
}

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

这篇关于检查 docker hub 上是否已经存在 image:tag 组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆