How can I find a Docker image with a specific tag

2019-01-21 18:28发布

I try to locate one specific tag for a Docker image. How can I do it on the command line? I try to avoid to download all and remove unneeded images.

In the official Ubuntu release, https://registry.hub.docker.com/_/ubuntu/, there are several tags (release for it), while when I search it on the command line,

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

Also in the help of command line search https://docs.docker.com/engine/reference/commandline/search/, no clue how it can work?

Is it possible in the docker search command?

If I use a raw command to search via the Docker registry API, then the information can be fetched:

   $ 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"
    }
]

标签: docker
9条回答
Deceive 欺骗
2楼-- · 2019-01-21 19:05

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

查看更多
欢心
3楼-- · 2019-01-21 19:05

For a script that works with Oauth bearer tokens on Docker hub, try this:

Listing the tags of a Docker image on a Docker hub through the HTTP API

查看更多
我命由我不由天
4楼-- · 2019-01-21 19:08

The v2 API seems to use some kind of pagination, so that it does not return all the available tags. This is clearly visible in projects such as python (or library/python). Even after quickly reading the documentation, I could not manage to work with the API correctly (maybe it is the wrong documentation).

Then I rewrote the script using the v1 API, and still using 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

The full script is available at: https://bitbucket.org/denilsonsa/small_scripts/src/default/docker_remote_tags.sh

I've also written (in Python) an improved version that aggregates tags that point to the same version: https://bitbucket.org/denilsonsa/small_scripts/src/default/docker_remote_tags.py

查看更多
混吃等死
5楼-- · 2019-01-21 19:09

I wrote a command line tool to simplify searching DockerHub repo tags, available in my PyTools GitHub repo. It's simple to use with various command line switches, but most basically:

./dockerhub_show_tags.py repo1 repo2

It's even available as a docker image and can take multiple repos:

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

If you want to embed in scripts use -q / --quiet to get just the tags, like normal docker commands:

./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
查看更多
祖国的老花朵
6楼-- · 2019-01-21 19:15

This script (docker-show-repo-tags.sh) should work for any Docker enabled host that has curl, sed, grep, and sort. This was updated to reflect the fact the repository tag URLs changed.

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

This older version no longer works.

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

This is the output for a simple example:

$ 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
查看更多
三岁会撩人
7楼-- · 2019-01-21 19:18

Reimplementation of the previous post, using 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
查看更多
登录 后发表回答