=======================
== I like turtles!!1 ==
=======================

List remote tags for public docker images

bash docker snippets zsh

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?

docker search tags

Here’s some code 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 | docker run -i jannis/jq -r '."results"[]["name"]' 2>/dev/null)
     has_more=$?
     if [[ ! -z "${result// }" ]]; then results="${results}\n${result}"; fi
     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.

Edit:

2018/02/14: Removed the jq requirement so you can just paste the snippet and you’re good to go.

Thanks to Bernard Spragg for the lovely container photo.