65

我尝试为 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

同样在命令行search https://docs.docker.com/engine/reference/commandline/search/的帮助下,不知道它是如何工作的?

是否可以在docker search命令中?

如果我使用原始命令通过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"
    }
]
4

11 回答 11

42

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

所以就像你以前做的那样,看着library/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"

参考:

jq: https://stedolan.github.io/jq/ |apt-get install jq

于 2015-09-17T03:27:21.213 回答
19

我不喜欢上面的任何解决方案,因为 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
于 2018-02-22T15:36:21.073 回答
12

As far as I know, the CLI does not allow searching/listing tags in a repository.

But if you know which tag you want, you can pull that explicitly by adding a colon and the image name: docker pull ubuntu:saucy

于 2014-06-30T08:49:51.020 回答
9

此脚本 (docker-show-repo-tags.sh) 应该适用于任何启用了 Docker 且具有 curl、sed、grep 和 sort 的主机。这已更新以反映存储库标记 URL 更改的事实。

#!/bin/sh
#
# Simple script that will display Docker repository tags
# using basic tools: curl, sed, grep, and sort.
#
# Usage:
#   $ docker-show-repo-tags.sh ubuntu centos
for Repo in $* ; do
    curl -sS "https://hub.docker.com/r/library/$Repo/tags/" | \
        sed -e $'s/"tags":/\\\n"tags":/g' -e $'s/\]/\\\n\]/g' | \
        grep '^"tags"' | \
        grep '"library"' | \
        sed -e $'s/,/,\\\n/g' -e 's/,//g' -e 's/"//g' | \
        grep -v 'library:' | \
        sort -fu | \
        sed -e "s/^/${Repo}:/"
done

这个旧版本不再有效。

#!/bin/sh
# WARNING: This no long works!
# Simple script that will display Docker repository tags.
#
# Usage:
#   $ docker-show-repo-tags.sh ubuntu centos
for Repo in $* ; do
  curl -s -S "https://registry.hub.docker.com/v2/repositories/library/$Repo/tags/" | \
    sed -e $'s/,/,\\\n/g' -e $'s/\[/\\\[\n/g' | \
    grep '"name"' | \
    awk -F\" '{print $4;}' | \
    sort -fu | \
    sed -e "s/^/${Repo}:/"
done

这是一个简单示例的输出:

$ docker-show-repo-tags.sh centos | cat -n
     1    centos:5
     2    centos:5.11
     3    centos:6
     4    centos:6.10
     5    centos:6.6
     6    centos:6.7
     7    centos:6.8
     8    centos:6.9
     9    centos:7.0.1406
    10    centos:7.1.1503
    11    centos:7.2.1511
    12    centos:7.3.1611
    13    centos:7.4.1708
    14    centos:7.5.1804
    15    centos:centos5
    16    centos:centos5.11
    17    centos:centos6
    18    centos:centos6.10
    19    centos:centos6.6
    20    centos:centos6.7
    21    centos:centos6.8
    22    centos:centos6.9
    23    centos:centos7
    24    centos:centos7.0.1406
    25    centos:centos7.1.1503
    26    centos:centos7.2.1511
    27    centos:centos7.3.1611
    28    centos:centos7.4.1708
    29    centos:centos7.5.1804
    30    centos:latest
于 2015-12-02T23:13:38.407 回答
5

我编写了一个命令行工具来简化对 Docker Hub 存储库标签的搜索,该工具可在我的PyTools GitHub 存储库中找到。与各种命令行开关一起使用很简单,但最基本的是:

./dockerhub_show_tags.py repo1 repo2

它甚至可以作为 Docker 映像使用,并且可以使用多个存储库:

docker run harisekhon/pytools dockerhub_show_tags.py centos ubuntu

DockerHub

repo: centos
tags: 5.11
      6.6
      6.7
      7.0.1406
      7.1.1503
      centos5.11
      centos6.6
      centos6.7
      centos7.0.1406
      centos7.1.1503

repo: ubuntu
tags: latest
      14.04
      15.10
      16.04
      trusty
      trusty-20160503.1
      wily
      wily-20160503
      xenial
      xenial-20160503

如果要将其嵌入脚本中,请使用-q/--quiet来获取标签,就像普通的 Docker 命令一样:

./dockerhub_show_tags.py centos -q
5.11
6.6
6.7
7.0.1406
7.1.1503
centos5.11
centos6.6
centos6.7
centos7.0.1406
centos7.1.1503
于 2016-05-25T10:22:57.803 回答
4

v2 API 似乎使用了某种分页,因此它不会返回所有可用的标签。这在python(或library/python)等项目中清晰可见。即使在快速阅读文档之后,我也无法正确使用 API(可能是错误的文档)。

然后我使用 v1 API 重写了脚本,它仍在使用jq

#!/bin/bash

repo="$1"

if [[ "${repo}" != */* ]]; then
    repo="library/${repo}"
fi

url="https://registry.hub.docker.com/v1/repositories/${repo}/tags"
curl -s -S "${url}" | jq '.[]["name"]' | sed 's/^"\(.*\)"$/\1/' | sort

完整的脚本位于:https ://github.com/denilsonsa/small_scripts/blob/master/docker_remote_tags.sh

我还编写了一个改进的版本(在 Python 中),它聚合了指向同一版本的标签:https ://github.com/denilsonsa/small_scripts/blob/master/docker_remote_tags.py

于 2016-03-19T01:17:46.643 回答
2

将此函数添加到您的 .zshrc 文件或手动运行命令:

#usage list-dh-tags <repo>
#example: list-dh-tags node
function list-dh-tags(){
    wget -q https://registry.hub.docker.com/v1/repositories/$1/tags -O -  | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n'  | awk -F: '{print $3}'
}

多亏了这个->如何在远程注册表上列出 Docker 映像的所有标签?

于 2018-07-19T11:05:55.307 回答
1

对于现代偶然发现此问题的任何人,您可以使用Skopeo从 Docker 注册表中检索图像的标签:

$ skopeo list-tags docker://jenkins/jenkins \
| jq -r '.Tags[] | select(. | contains("lts-alpine"))' \
| sort --version-sort --reverse

lts-alpine
2.277.3-lts-alpine
2.277.2-lts-alpine
2.277.1-lts-alpine
2.263.4-lts-alpine
2.263.3-lts-alpine
2.263.2-lts-alpine
2.263.1-lts-alpine
2.249.3-lts-alpine
2.249.2-lts-alpine
2.249.1-lts-alpine
2.235.5-lts-alpine
2.235.4-lts-alpine
2.235.3-lts-alpine
2.235.2-lts-alpine
2.235.1-lts-alpine
2.222.4-lts-alpine
于 2021-04-21T21:17:02.257 回答
0

对于在 Docker Hub 上使用 OAuth 不记名令牌的脚本,请尝试以下操作:

通过 HTTP API 在 Docker 集线器上列出 Docker 映像的标签

于 2016-07-09T02:30:49.333 回答
0

您可以使用Visual Studio Code为可用的 Docker 图像和标签提供自动完成功能。但是,这需要您键入标签的第一个字母才能看到自动完成建议。

例如,在编写时FROM ubuntu它会提供自动完成建议,如ubuntu,ubuntu-debootstrapubuntu-upstart. 在编写FROM ubuntu:a时,它会提供自动完成建议,例如ubuntu:artfulubuntu:artful-20170511.1

于 2019-03-28T10:31:05.773 回答
0

使用 Python over sed/AWK 重新实现上一篇文章:

for Repo in $* ; do
    tags=$(curl -s -S "https://registry.hub.docker.com/v2/repositories/library/$Repo/tags/")
    python - <<EOF

import json

tags = [t['name'] for t in json.loads('''$tags''')['results']]
tags.sort()
for tag in tags:
    print "{}:{}".format('$Repo', tag)
EOF
done
于 2016-02-19T12:25:21.423 回答