How can I find a Docker image with a specific tag in Docker registry on the Docker command line?

前端 未结 10 1194
小蘑菇
小蘑菇 2020-12-12 18:21

I try to locate one specific tag for a Docker image. How can I do it on the command line? I want to avoid downloading all the images and then removing the unneeded ones.

相关标签:
10条回答
  • 2020-12-12 18:29

    I didn't like any of the solutions above because A) they required external libraries that I didn't have and didn't want to install. B) I didn't get all the pages.

    The Docker API limits you to 100 items per request. This will loop over each "next" item and get them all (for Python it's seven pages; other may be more or less... It depends)

    If you really want to spam yourself, remove | cut -d '-' -f 1 from the last line, and you will see absolutely everything.

    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;
    

    Sample output:

    $ 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
    

    If you want the bash_profile version:

    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;
    }
    

    And simply call it: docker-tags redis

    Sample output:

    $ docker-tags redis
    ...
    2
    2.6
    2.6.17
    2.8
    
    --trunc----
    
    32bit
    alpine
    latest
    nanoserver
    windowsservercore
    
    0 讨论(0)
  • 2020-12-12 18:31

    Add this function to your .zshrc file or run the command manually:

    #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}'
    }
    

    Thanks to this -> How can I list all tags for a Docker image on a remote registry?

    0 讨论(0)
  • 2020-12-12 18:41

    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 it is 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://github.com/denilsonsa/small_scripts/blob/master/docker_remote_tags.sh

    I've also written an improved version (in Python) that aggregates tags that point to the same version: https://github.com/denilsonsa/small_scripts/blob/master/docker_remote_tags.py

    0 讨论(0)
  • 2020-12-12 18:41

    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

    0 讨论(0)
  • 2020-12-12 18:43

    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
    
    0 讨论(0)
  • 2020-12-12 18:45

    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

    0 讨论(0)
提交回复
热议问题