如何检查本地是否存在带有特定标签的Docker映像? [英] How to check if a Docker image with a specific tag exist locally?

查看:670
本文介绍了如何检查本地是否存在带有特定标签的Docker映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定本地是否存在带有特定标签的Docker映像.如果Docker客户端无法原生执行此操作,我可以使用bash脚本.

I'd like to find out if a Docker image with a specific tag exists locally. I'm fine by using a bash script if the Docker client cannot do this natively.

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

Just to provide some hints for a potential bash script the result of running the docker images command returns the following:

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

推荐答案

我通常会测试此脚本):

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

但是由于docker images仅将REPOSITORY作为参数,因此您需要对标签进行grep,而无需使用-q .

But since docker images only takes REPOSITORY as parameter, you would need to grep on tag, without using -q.

docker images 现在获取标签(docker 1.8+)

docker images takes tags now (docker 1.8+) [REPOSITORY[:TAG]]

下面提到的另一种方法是使用 docker inspect .
但是对于docker 17+,图像的语法为: docker image inspect (在不存在的图像上,退出状态将为非0 )

The other approach mentioned below is to use docker inspect.
But with docker 17+, the syntax for images is: docker image inspect (on an non-existent image, the exit status will be non-0)

iTayb 所述-检查是否有特定标签的docker-image-local-local/30543453?noredirect = 1#comment108525095_30543453>评论:

As noted by iTayb in the comments:

  • docker images -q方法在具有大量图像的计算机上会变得非常慢.在6,500张图像的计算机上运行需要44秒.
  • docker image inspect立即返回.
  • The docker images -q method can get really slow on a machine with lots of images. It takes 44s to run on a 6,500 images machine.
  • The docker image inspect returns immediately.

这篇关于如何检查本地是否存在带有特定标签的Docker映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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