List remote tags for public docker images

While you can easily $ docker search for images, it's not so straight forward to find out all available tags of a public docker image. Wouldn't it be nice, if you could just search for tags with $ docker tags redis?

Here's some code (requires the fantastic jq) that you can put into your .zshrc or .bashrc to get the docker tags command until the folks at docker implement it:

docker () {  
  if [[ "${1}" = "tags" ]]; then
    docker_tag_search $2
  else
    command docker $@
  fi
}

docker_tag_search () {  
  # Display help
  if [[ "${1}" == "" ]]; then
    echo "Usage: docker tags repo/image"
    echo "       docker tags image"
    return
  fi

  # Full repo/image was supplied
  if [[ $1 == *"/"* ]]; then
    name=$1

  # Only image was supplied, default to library/image
  else
    name=library/${1}
  fi
  printf "Searching tags for ${name}"

  # Fetch all pages, because the only endpoint supporting pagination params
  # appears to be tags/lists, but that needs authorization
  results=""
  i=0
  has_more=0
  while [ $has_more -eq 0 ]  
  do  
     i=$((i+1))
     result=$(curl "https://registry.hub.docker.com/v2/repositories/${name}/tags/?page=${i}" 2>/dev/null | jq -r '."results"[]["name"]' 2>/dev/null)
     has_more=$?
     results="${results}\n${result}"
     printf "."
  done  
  printf "\n"

  # Sort all tags
  sorted=$(
    for tag in "${results}"; do
      echo $tag
    done | sort
  )

  # Print all tags
  for tag in "${sorted[@]}"; do
    echo $tag
  done
}

This will allow you to search for the images of the standard docker library via $ docker tags imagename and from custom repositories via $ docker tags repo/imagename.

Unfortunately, this script has to paginate until it gets an error, because the image tags endpoint supporting pagination requires authorization.

I'll keep this post updated with your suggestions and improvements. Actually I'm hoping somebody will tell me that all of this is actually not necessary and you can search for image tags with the docker command. Find me on twitter as @jannis.

Jannis

Read more posts by this author.

Subscribe to Jannis Hermanns

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!