Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support searching tags in a repository on the command line #17238

Open
tmds opened this issue Oct 21, 2015 · 21 comments
Open

Support searching tags in a repository on the command line #17238

tmds opened this issue Oct 21, 2015 · 21 comments
Labels
kind/enhancement Enhancements are not bugs or new features but can improve usability or performance.

Comments

@tmds
Copy link

tmds commented Oct 21, 2015

Such a command should search the registry for image tags. The command should allow to filter the tags using wildcards.
e.g.

docker search-tags debian:8*
# would list: debian:8.2 and debian:8, indicate they are the same image and list aliases (jessie, latest)
docker search-tags debian
# would list all tags of debian
@jlausuch
Copy link

+1.
I would like to have that function too.

For now, I am using curl to get a response json which I have to parse.As tmds suggests, a new command would save some lines of code.

@thaJeztah
Copy link
Member

I think this would depend on the registry / index supporting this, not docker

@tmds
Copy link
Author

tmds commented Oct 30, 2015

I am not sure what you mean by 'docker'.
I am requesting a feature to be provided by the docker executable. As a user, I don't care whether the filtering happens on the registry server or on my local system.
Doing it on the registry can reduce the data sent to the client.
Doing it on the client can probably work with the existing registry API.

@tmds
Copy link
Author

tmds commented Nov 19, 2015

@thaJeztah can you add the kind/enhancement label please?

@thaJeztah thaJeztah added the kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. label Nov 23, 2015
@ches
Copy link

ches commented May 5, 2016

I wish for this feature in spirit, if maybe not exactly the UX of the proposed command.

I'd mostly like to just be able to get lists of tags for images returned in results of docker search, perhaps having an additional flag for that command. Piping to grep, awk or the like should serve my use cases well enough at this point as far as wildcards go. This seems to come up fairly commonly.

Simple listing seems to be there at least since #9015 (incidentally, tag search is brought up among the discussion there too). If more advanced indexing is desired perhaps docker-archive/docker-registry#687 and wherever its breadcrumbs ultimately end are of interest.

@HariSekhon
Copy link

HariSekhon commented May 25, 2016

I've written an easy to use command line tool in Python to query DockerHub tags for given repos:

It's available as dockerhub_show_tags.py in my PyTools github repo:

https://github.com/harisekhon/pytools

It's simple to use with various command line switches, but most basically:

./dockerhub_show_tags.py repo1 repo2 ...

and there is a ready-to-run docker image on DockerHub:

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 use the output embedded as part of scripts then just 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

@andyxning
Copy link

Friendly ping. Any updates on this?

@andyxning
Copy link

Maybe adding new -t --tag command line argument to docker search is more natural. We need to search all the tags for an image.

@artushin
Copy link

I think even a --tag=1.2.3 or the ability to pass a tag on the image argument would be useful. Specifically for build systems that want to have internal logic to prevent pushed tags being overwritten (a la npm publish not allowing you to republish a version unless the --force flag is provided). A tag list similar to the v1 http API would be great too though.

@pcgeek86
Copy link
Contributor

I remember looking for this a few years ago, when I first started learning about Docker. I still find myself looking for this functionality on a regular basis. There are multiple StackOverflow posts requesting for this functionality, and a plethora of obscure shell scripts that hit the registry directly.

https://stackoverflow.com/questions/28320134/how-to-list-all-tags-for-a-docker-image-on-a-remote-registry
https://stackoverflow.com/questions/24481564/how-can-i-find-docker-image-with-specific-tag-in-docker-registry-in-docker-comma

Any chance this could get bumped up @thaJeztah?

@thaJeztah
Copy link
Member

@pcgeek86 I was discussing this a while back, and docker image search currently uses the v1 (https://github.com/moby/moby/blob/v1.6.0/docs/sources/reference/api/registry_api.md#search) search API of Docker Hub, but (discussing with @lilybguo at the time), there's also a v2 (and v3?) search API that's used internally by the Docker Hub UI. I'm not sure if that API is already publicly accessible, but if it is, then possibly that API could be used to provide more advanced search options.

@thaJeztah
Copy link
Member

So v1 search uses index.docker.io (https://index.docker.io/v1/search?q=ubuntu), but using a /v2 URL works on the Docker Hub domain https://hub.docker.com/v2/search/repositories?query=ubuntu&is_official=true

I'll try to get more information on this

@fragmuffin
Copy link

fragmuffin commented Feb 6, 2018

Workaround
To get a quick and dirty labels list (based on response by @thaJeztah above)

image_name=python
curl -s https://registry.hub.docker.com/v1/repositories/${image_name}/tags | jq --raw-output ".[].name"

Output

latest
2
2-alpine
2-alpine3.4
2-alpine3.6
... (output truncated)
wheezy
windowsservercore
windowsservercore-1709
windowsservercore-ltsc2016

@kingbuzzman
Copy link

I have a much better solution @fragmuffin:

The fundamental issue with your solution is that it will only show you the 10 latest tags... which might cover most peoples use case, but i needed all of them.

I wrote a better description of the problem as a whole over at stackoverflow

function docker-tags () {
  name=$1
  # Initial url
  url=https://registry.hub.docker.com/v2/repositories/library/$name/tags/
  (
    # 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 is 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"]]))')
      # Lets 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
  ) | 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

@fragmuffin
Copy link

@kingbuzzman

The fundamental issue with your solution is that it will only show you the 10 latest tags

That's weird, when I run it in an ubuntu bash shell I get 449 tags for python, and 139 tags for redis.
But different systems can do different things, I'm glad you found a solution that works for you, and thanks for sharing it 👍

@dsifford
Copy link

FYI @fragmuffin if you just pass --raw-output to jq you won't have to then pipe to sed to strip the quotes.

@fragmuffin
Copy link

@dsifford : neat! 👍, I updated my comment with your good advice ;)

@tmds
Copy link
Author

tmds commented Jun 11, 2019

@thaJeztah feel free to close this.

@fragmuffin
Copy link

@tmds is there now a supported way to do this via the docker CLI?

@stephentung-tec
Copy link

I'd also appreciate if there's a native way of doing this through the docker CLI

@liszca
Copy link

liszca commented Jul 23, 2020

@kingbuzzman thanks for your python scripting, here a slightly modified version for a little more flexibility:

#!/bin/bash

function docker-tags () {
  name=$1
  # Initial url
  url=https://registry.hub.docker.com/v2/repositories/$([[ $name =~ "/" ]] && echo $name || echo library/$name)/tags/
  (
    # 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 is 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 | python3 -c 'import sys, json;
data = json.load(sys.stdin); 
print(data.get("next", "") or ""); 
print("\n".join([x["name"] for x in data["results"]]))')
      # Lets 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
  ) | sort --version-sort | uniq;
}

And simply call it: docker-tags redis or docker-tags portainer/portainer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Enhancements are not bugs or new features but can improve usability or performance.
Projects
None yet
Development

No branches or pull requests