bash 如何检查本地是否存在带有特定标签的 Docker 镜像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30543409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:59:22  来源:igfitidea点击:

How to check if a Docker image with a specific tag exist locally?

bashdocker

提问by Johan

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.

我想知道本地是否存在带有特定标签的 Docker 镜像。如果 Docker 客户端本身无法执行此操作,我可以使用 bash 脚本。

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

只是为了为潜在的 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

回答by VonC

I usually test the result of docker images -q(as in this script):

我通常测试结果docker images -q(如本脚本中所示):

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

But since docker imagesonly takes REPOSITORYas parameter, you would need to grep on tag, without using -q.

由于docker imagesREPOSITORY作为参数,您需要在标签上使用 grep,而不使用-q.

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

docker images现在需要标签(docker 1.8+) [REPOSITORY[:TAG]]

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)

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

As noted by iTaybin the comments:

正如iTayb评论中所指出的:

  • The docker images -qmethod 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 inspectreturns immediately.
  • docker images -q方法在具有大量图像的机器上可能会变得非常慢。在 6,500 张图像的机器上运行需要 44 秒。
  • docker image inspect立即返回。

回答by Travis Reeder

Try docker inspect, for example:

试试docker inspect,例如:

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

But now with an image that exists, you'll get a bunch of information, eg:

但是现在有了存在的图像,您将获得一堆信息,例如:

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

And it's in a nice json format.

它是一个很好的 json 格式。

回答by rubicks

tldr:

域名:

docker image inspect myimage:mytag

By way of demonstration...

通过示范...

success, found image:

成功,找到图片:

$ 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

failure, missing image:

失败,缺少图像:

$ 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

Reference:

参考:

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

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

回答by Luís Bianchin

You can use like the following:

您可以像下面这样使用:

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

Or:

或者:

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

回答by Johan

With the help of Vonc's answerabove I created the following bash script named check.sh:

在上面Vonc 的回答的帮助下,我创建了以下名为的 bash 脚本check.sh

#!/bin/bash
image_and_tag=""
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

Using it for an existing image and tag will print exists, for example:

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

./check.sh rabbitmq:3.4.4

Using it for a non-existing image and tag will print doesn't exist, for example:

将它用于不存在的图像和标签将打印doesn't exist,例如:

./check.sh rabbitmq:3.4.3

回答by Alex Montoya

Using test

使用 test

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

or in one line

或在一行

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

回答by Namik Hajiyev

In bash script I do this to check if image exists by tag :

在 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

Example script above checks if mysql image with 5.6 tag exists. If you want just check if any mysql image exists without specific version then just pass repository name without tag as this :

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

IMAGE_NAME="mysql"

回答by Abhishek J

In case you are trying to search for a docker image from a docker registry, I guess the easiest way to check if a docker image is present is by using the Docker V2 REST API Tagslist service

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

Example:-

例子:-

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

if the above result returns 200Ok with a list of image tags, then we know that image exists

如果上面的结果返回 200Ok 和图像标签列表,那么我们知道图像存在

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

else if you see something like

否则如果你看到类似的东西

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

then you know for sure that image doesn't exist.

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

回答by Artur Mustafin

Just a bit from me to very good readers:

从我到非常好的读者的一点点:

Build

建造

#!/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

Watch

手表

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

Run

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

回答by raVen

for specific tag name

对于特定标签名称

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

for "like clause" tagname:my_image_tag --> start my_ima*

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

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

if you want to someting "the image" for example delete all images tag started "my_ima" try this

如果你想要一些“图像”,例如删除所有图像标签开始“my_ima”试试这个

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