如何检查具有特定标记的Docker镜像是否存在于本地?

时间:2015-05-30 07:11:39

标签: bash docker

我想知道本地是否存在具有特定标记的Docker镜像。如果Docker客户端本身不能这样做,我可以使用bash脚本。

只是为潜在的bash脚本提供一些提示,运行docker images命令的结果将返回以下内容:

REPOSITORY                               TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
rabbitmq                                 latest              e8e654c05c91        5 weeks ago         143.5 MB
busybox                                  latest              8c2e06607696        6 weeks ago         2.433 MB
rabbitmq                                 3.4.4               a4fbaad9f996        11 weeks ago        131.5 MB

12 个答案:

答案 0 :(得分:109)

我通常会测试Absolute positioning inside absolute position的结果(如docker images -q中所示):

if [[ "$(docker images -q myimage:mytag 2> /dev/null)" == "" ]]; then
  # do something
fi

但是,因为docker images只需要REPOSITORY作为参数,你需要grep on tag,而不使用-q

this script现在带标签(docker 1.8+)[REPOSITORY[:TAG]]

下面提到的另一种方法是使用docker images 但是对于docker 17+,图像的语法是: docker inspect (在不存在的图像上,docker image inspect

答案 1 :(得分:19)

尝试docker inspect,例如:

$ docker inspect --type=image treeder/hello.rb:nada
Error: No such image: treeder/hello.rb:nada
[]

但是现在有了一个存在的图像,你会得到一堆信息,例如:

$ docker inspect --type=image treeder/hello.rb:latest
[
{
    "Id": "85c5116a2835521de2c52f10ab5dda0ff002a4a12aa476c141aace9bc67f43ad",
    "Parent": "ecf63f5eb5e89e5974875da3998d72abc0d3d0e4ae2354887fffba037b356ad5",
    "Comment": "",
    "Created": "2015-09-23T22:06:38.86684783Z",
    ...
}
]

它采用了不错的json格式。

答案 2 :(得分:13)

您可以使用以下内容:

[ ! -z $(docker images -q someimage:sometag) ] || echo "does not exist"

或者:

[ -z $(docker images -q someimage:sometag) ] || echo "already exists"

答案 3 :(得分:12)

tldr:

docker image inspect myimage:mytag

通过示范......

成功,找到了图片:

$ docker image pull busybox:latest
latest: Pulling from library/busybox
Digest: sha256:32f093055929dbc23dec4d03e09dfe971f5973a9ca5cf059cbfb644c206aa83f
Status: Image is up to date for busybox:latest

$ docker image inspect busybox:latest >/dev/null 2>&1 && echo yes || echo no
yes

失败,缺少图片:

$ docker image rm busybox:latest
Untagged: busybox:latest
Untagged: busybox@sha256:32f093055929dbc23dec4d03e09dfe971f5973a9ca5cf059cbfb644c206aa83f

$ docker image inspect busybox:latest >/dev/null 2>&1 && echo yes || echo no
no

参考:

https://docs.docker.com/engine/reference/commandline/image_inspect/

答案 4 :(得分:3)

在上面Vonc's answer的帮助下,我创建了以下名为check.sh的bash脚本:

#!/bin/bash
image_and_tag="$1"
image_and_tag_array=(${image_and_tag//:/ })
if [[ "$(docker images ${image_and_tag_array[0]} | grep ${image_and_tag_array[1]} 2> /dev/null)" != "" ]]; then
  echo "exists"
else
  echo "doesn't exist"
fi

将其用于现有图像和标记将打印exists,例如:

./check.sh rabbitmq:3.4.4

将其用于不存在的图片和标记将打印doesn't exist,例如:

./check.sh rabbitmq:3.4.3

答案 5 :(得分:2)

使用test

if test ! -z "$(docker images -q <name:tag>)"; then
  echo "Exist"
fi

或一行

test ! -z "$(docker images -q <name:tag>)" &&  echo exist

答案 6 :(得分:2)

在bash脚本中,我这样做是通过标记检查图像是否存在:

IMAGE_NAME="mysql:5.6"

if docker image ls -a "$IMAGE_NAME" | grep -Fq "$IMAGE_NAME" 1>/dev/null; then
echo "could found image $IMAGE_NAME..."
fi

上面的示例脚本检查是否存在带有5.6标签的mysql映像。如果您只想检查是否存在没有特定版本的mysql映像,则只需传递不带标签的存储库名称,如下所示:

IMAGE_NAME="mysql"

答案 7 :(得分:0)

如果您要尝试从docker注册表中搜索docker镜像,我想检查docker镜像是否存在的最简单方法是使用Docker V2 REST API Tags list service

示例:-

curl $CURLOPTS -H "Authorization: Bearer $token" "https://hub.docker.com:4443/v2/your-repo-name/tags/list"

如果以上结果返回200OK并带有图像标签列表,则表明该图像存在

{"name":"your-repo-name","tags":["1.0.0.1533677221","1.0.0.1533740305","1.0.0.1535659921","1.0.0.1535665433","latest"]}

否则,如果您看到类似

{"errors":[{"code":"NAME_UNKNOWN","message":"repository name not known to registry","detail":{"name":"your-repo-name"}}]} 

那么您肯定知道该图像不存在。

答案 8 :(得分:0)

从我这里到非常优秀的读者:

构建

#!/bin/bash -e
docker build -t smpp-gateway smpp
(if  [ $(docker ps -a | grep smpp-gateway | cut -d " " -f1) ]; then \
  echo $(docker rm -f smpp-gateway); \
else \
  echo OK; \
fi;);
docker run --restart always -d --network="host" --name smpp-gateway smpp-gateway:latest

观看

docker logs --tail 50 --follow --timestamps smpp-gateway

运行

sudo docker exec -it \
$(sudo docker ps | grep "smpp-gateway:latest" | cut -d " " -f1) \
/bin/bash

答案 9 :(得分:0)

特定标签名称

$ docker images  --filter reference='<REPOSITORY>:TAG'

对于“ like子句”标记名:my_image_tag->启动my_ima *

$ docker images  --filter reference='<REPOSITORY>:my_ima*'

例如,如果要添加“图像”,请删除所有以“ my_ima”开头的图像标签,请尝试

docker rmi -f $(docker images -q  --filter reference='myreponame:my_ima*')

答案 10 :(得分:0)

我认为应该在converting variables name to string命令内部(使用标志?)实现此功能,以便避免大量的代码重复。

我在称为docker build的包装函数中使用了与accepted answer相同的条件,以便在调用原始docker_build命令之前进行必要的检查。

docker build

免责声明:由于它仅适用于以空格分隔的长格式参数(即# Usage: docker_build <...> (instead of docker build) docker_build() { local arguments=("$@") local index for (( index=0; index<$#; index++ )); do case ${arguments[index]} in --tag) local tag=${arguments[index+1]} if [[ ! -z $(docker images -q "${tag}" 2> /dev/null) ]]; then echo "Image ${tag} already exists." return fi ;; esac done command docker build "$@" } ),因此尚无法投入生产。另外,这仅修改了--tag hello-world:latest命令,所有其他命令保持不变。如果有人有改进,请告诉我。

我想分享一下这段代码,因为在bash函数中包装标准命令的想法(避免代码重复)比编写包装器语句更优雅,更可扩展。

答案 11 :(得分:0)

灵感来自上面@rubicks 的回复。

检查图片是否已经存在

image_name_tag="alpine:3.13.3"

docker image inspect ${image_name_tag} > /dev/null
echo $?

如果图片不存在则拉取

docker image inspect ${image_name_tag} > /dev/null || docker  pull ${image_name_tag}

演示执行

# image_name_tag="alpine:3.13.3"
# 
# docker image inspect ${image_name_tag} > /dev/null
echo $?
Error: No such image: alpine:3.13.3
# echo $?
1
# docker image inspect ${image_name_tag} > /dev/null || docker  pull ${image_name_tag}
Error: No such image: alpine:3.13.3
3.13.3: Pulling from library/alpine
9aae54b2144e: Pull complete 
Digest: sha256:826f70e0ac33e99a72cf20fb0571245a8fee52d68cb26d8bc58e53bfa65dcdfa
Status: Downloaded newer image for alpine:3.13.3
docker.io/library/alpine:3.13.3
# docker image inspect ${image_name_tag} > /dev/null || docker  pull ${image_name_tag}
$