当前位置: 首页 > 知识库问答 >
问题:

如何在Docker命令行的Docker注册表中找到具有特定标记的Docker图像?

颛孙建业
2023-03-14

我尝试为Docker图像定位一个特定的标记。如何在命令行上执行此操作?我希望避免下载所有图像,然后删除不需要的图像。

在官方的Ubuntu版本中,https://registry.hub.docker.com/_/ubuntu/,有几个标签(为它发布),而当我在命令行上搜索它时,

user@ubuntu:~$ docker search ubuntu | grep ^ubuntu
ubuntu              Official Ubuntu base image                          354
ubuntu-upstart      Upstart is an event-based replacement for ...   7
ubuntufan/ping                                                0
ubuntu-debootstrap                                                   0

也可以在命令行的帮助下进行搜索https://docs.docker.com/engine/reference/commandline/search/,不知道它是如何工作的?

是否可以在docker search(docker搜索)命令中执行此操作?

如果我使用原始命令通过Docker注册表API进行搜索,则可以获取信息:

   $ curl https://registry.hub.docker.com//v1/repositories/ubuntu/tags | python -mjson.tool
   [
    {
        "layer": "ef83896b",
        "name": "latest"
    },
    .....
    {
        "layer": "463ff6be",
        "name": "raring"
    },
    {
        "layer": "195eb90b",
        "name": "saucy"
    },
    {
        "layer": "ef83896b",
        "name": "trusty"
    }
]

共有3个答案

越飞翮
2023-03-14

据我所知,CLI不允许在存储库中搜索/列出标记。

但是如果您知道您想要哪个标签,您可以通过添加冒号和图像名称来显式提取它:docker拉ubuntu: saucy

宋弘壮
2023-03-14

我不喜欢上面的任何解决方案,因为A)它们需要我没有也不想安装的外部库。B)我没有得到所有的页面。

Docker API将每个请求限制为100个项目。这将循环遍历每个“下一个”项目,并将它们全部获取(对于Python,它有七页;其他可能或多或少……这取决于)

如果你真的想给自己发垃圾邮件,请从最后一行中删除| cut-d“-”f 1,你将看到所有内容。

url=https://registry.hub.docker.com/v2/repositories/library/redis/tags/?page_size=100 `# Initial url` ; \
( \
  while [ ! -z $url ]; do `# Keep looping until the variable url is empty` \
    >&2 echo -n "." `# Every iteration of the loop prints out a single dot to show progress as it got through all the pages (this is inline dot)` ; \
    content=$(curl -s $url | python -c 'import sys, json; data = json.load(sys.stdin); print(data.get("next", "") or ""); print("\n".join([x["name"] for x in data["results"]]))') `# Curl the URL and pipe the output to Python. Python will parse the JSON and print the very first line as the next URL (it will leave it blank if there are no more pages) then continue to loop over the results extracting only the name; all will be stored in a variable called content` ; \
    url=$(echo "$content" | head -n 1) `# Let's get the first line of content which contains the next URL for the loop to continue` ; \
    echo "$content" | tail -n +2 `# Print the content without the first line (yes +2 is counter intuitive)` ; \
  done; \
  >&2 echo `# Finally break the line of dots` ; \
) | cut -d '-' -f 1 | sort --version-sort | uniq;

样本输出:

$ url=https://registry.hub.docker.com/v2/repositories/library/redis/tags/?page_size=100 `#initial url` ; \
> ( \
>   while [ ! -z $url ]; do `#Keep looping until the variable url is empty` \
>     >&2 echo -n "." `#Every iteration of the loop prints out a single dot to show progress as it got through all the pages (this is inline dot)` ; \
>     content=$(curl -s $url | python -c 'import sys, json; data = json.load(sys.stdin); print(data.get("next", "") or ""); print("\n".join([x["name"] for x in data["results"]]))') `# Curl the URL and pipe the JSON to Python. Python will parse the JSON and print the very first line as the next URL (it will leave it blank if there are no more pages) then continue to loop over the results extracting only the name; all will be store in a variable called content` ; \
>     url=$(echo "$content" | head -n 1) `#Let's get the first line of content which contains the next URL for the loop to continue` ; \
>     echo "$content" | tail -n +2 `#Print the content with out the first line (yes +2 is counter intuitive)` ; \
>   done; \
>   >&2 echo `#Finally break the line of dots` ; \
> ) | cut -d '-' -f 1 | sort --version-sort | uniq;
...
2
2.6
2.6.17
2.8
2.8.6
2.8.7
2.8.8
2.8.9
2.8.10
2.8.11
2.8.12
2.8.13
2.8.14
2.8.15
2.8.16
2.8.17
2.8.18
2.8.19
2.8.20
2.8.21
2.8.22
2.8.23
3
3.0
3.0.0
3.0.1
3.0.2
3.0.3
3.0.4
3.0.5
3.0.6
3.0.7
3.0.504
3.2
3.2.0
3.2.1
3.2.2
3.2.3
3.2.4
3.2.5
3.2.6
3.2.7
3.2.8
3.2.9
3.2.10
3.2.11
3.2.100
4
4.0
4.0.0
4.0.1
4.0.2
4.0.4
4.0.5
4.0.6
4.0.7
4.0.8
32bit
alpine
latest
nanoserver
windowsservercore

如果您想要bash_profile版本:

function docker-tags () {
  name=$1
  # Initial URL
  url=https://registry.hub.docker.com/v2/repositories/library/$name/tags/?page_size=100
  (
    # Keep looping until the variable URL is empty
    while [ ! -z $url ]; do
      # Every iteration of the loop prints out a single dot to show progress as it got through all the pages (this is inline dot)
      >&2 echo -n "."
      # Curl the URL and pipe the output to Python. Python will parse the JSON and print the very first line as the next URL (it will leave it blank if there are no more pages)
      # then continue to loop over the results extracting only the name; all will be stored in a variable called content
      content=$(curl -s $url | python -c 'import sys, json; data = json.load(sys.stdin); print(data.get("next", "") or ""); print("\n".join([x["name"] for x in data["results"]]))')
      # Let's get the first line of content which contains the next URL for the loop to continue
      url=$(echo "$content" | head -n 1)
      # Print the content without the first line (yes +2 is counter intuitive)
      echo "$content" | tail -n +2
    done;
    # Finally break the line of dots
    >&2 echo
  ) | cut -d '-' -f 1 | sort --version-sort | uniq;
}

并简单地将其称为:docker-tags redis

样本输出:

$ docker-tags redis
...
2
2.6
2.6.17
2.8

--trunc----

32bit
alpine
latest
nanoserver
windowsservercore
萧越泽
2023-03-14

使用CoreOS时,jq可用于解析JSON数据。

就像您之前所做的那样,查看库/centos

$ curl -s -S 'https://registry.hub.docker.com/v2/repositories/library/centos/tags/' | jq '."results"[]["name"]' |sort
"6"
"6.7"
"centos5"
"centos5.11"
"centos6"
"centos6.6"
"centos6.7"
"centos7.0.1406"
"centos7.1.1503"
"latest"

更简洁的v2 API现在可用,这就是我在示例中使用的。我将构建一个简单的脚本docker_remote_tags

#!/usr/bin/bash
curl -s -S "https://registry.hub.docker.com/v2/repositories/library/$@/tags/" | jq '."results"[]["name"]' |sort

使能够:

$ ./docker_remote_tags library/centos
"6"
"6.7"
"centos5"
"centos5.11"
"centos6"
"centos6.6"
"centos6.7"
"centos7.0.1406"
"centos7.1.1503"
"latest"

参考号:

https://stedolan.github.io/jq/安装jq

 类似资料:
  • 问题内容: 我尝试为Docker映像找到一个特定的标签。如何在命令行上执行此操作?我尽量避免全部下载并删除不需要的图像。 在正式的Ubuntu版本https://registry.hub.docker.com/_/ubuntu/中,有多个标签(针对该版本),而当我在命令行上搜索它时, 同样在命令行 https://docs.docker.com/engine/reference/commandli

  • 如何使用CLI(首选)或curl列出远程Docker注册表上Docker图像的所有标记? 最好不从远程注册表中提取所有版本。我只想列出标签。

  • 我安装了一个docker镜像,我想检查它的CMD命令是什么。有没有cli命令可以这样做?例如,我想让它告诉我这个docker镜像CMD是["rails","server"]

  • 我尝试在本地使用kubernetes。因此我安装了minikube。当我创建部署对象时,pods无法在minikube docker注册表中找到图像,并且pods的状态显示为< code>ErrImageNeverPull。为了解决这个问题,我遵循了以下步骤: < li> < Li > < code > eval $(minikube docker-env) < li >我在deployment.

  • 我的的第一行如下: 这个映像是601MB,我在每次调用构建时都必须从docker hub中提取它。 有什么方法可以将图像存储在项目的docker存储库中吗?以便在第一次运行构建时自动将其放置在那里,然后在后续调用构建时从那里检索?

  • 我想通过docker compose构建图像,并为其设置特定标记。文件说: Comment将构建并用生成的名称标记它,然后使用该映像。 但是我找不到指定标签的方法,对于构建的图像,我总是看到“最新”标签。