Docker Tip #81: Searching the Docker Hub on the Command Line
If you're looking for an image or its tags from the Docker Hub, you can search and filter them using the Docker CLI tool.
If you’re new’ish to Docker, you’ll often find yourself browsing the Docker Hub looking to see if any images exist to solve your problem (preferably official images).
There’s nothing wrong with that, but you can also do this from the comfort of the command line without resorting to a terminal based browser like Lynx.
Search for an image with the Docker CLI:
docker search python
That will return back:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec... 4029 [OK]
django Django is a free web application framework, ... 806 [OK]
pypy PyPy is a fast, compliant alternative implem... 175 [OK]
kaggle/python Docker image for Python scripts run on Kaggle 114 [OK]
+ 21 other results (ordered by stars)
You can even type in something like docker search pyt
if you only know part
of the name.
Limiting it to official images only:
docker search ruby --filter="is-official=true"
That will return back:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ruby Ruby is a dynamic, reflective, object-orient... 1643 [OK]
redmine Redmine is a flexible project management web... 716 [OK]
jruby JRuby (http://www.jruby.org) is an implement... 82 [OK]
I’ve been using Docker for almost 5 years and I only learned that this existed today.
Although, having the image name alone isn’t too useful, you can also get a
list of tags for each image. Unfortunately this isn’t built into the docker search
command but you can do it with a little bit of shell scripting.
Bash function / alias to get a list of Docker image tags:
Update: In September 2022 Docker deprecated v1 of their API. Here’s the updated v2 version. Their API’s response is quite a bit more detailed which now involves using jq to parse the JSON reponse:
# Working v2 API solution:
dtags () {
local image="${1}"
curl --silent \
"https://registry.hub.docker.com/v2/repositories/library/${image}/tags?page_size=1000" \
| jq -r ".results[].name" | sort --version-sort
}
# Original deprecated v1 API solution (I'm keeping this here for historic purposes):
dtags () {
local image="${1}"
curl --silent https://registry.hub.docker.com/v1/repositories/"${image}"/tags \
| tr -d '[]" ' | tr '}' '\n' | awk -F: '{print $3}' | sort --version-sort
}
Now you can run dtags debian
to get a full list of Debian tags or any image
you want! If you’re curious, you can find my
dotfiles on GitHub
which has this function and many more Docker related aliases set up.
There’s also this video tutorial that explains the v1 API alias in more detail and an updated video tutorial going over the v2 API alias.