Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the list of images and tags for a Docker Hub user account.
#!/bin/bash
# Example for the Docker Hub V2 API
# Returns all imagas and tags associated with a Docker Hub user account.
# Requires 'jq': https://stedolan.github.io/jq/
# set username and password
UNAME=""
UPASS=""
# -------
set -e
echo
# aquire token
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# get list of repositories for the user account
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/?page_size=100 | jq -r '.results|.[]|.name')
# build a list of all images & tags
for i in ${REPO_LIST}
do
# get tags for repo
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=100 | jq -r '.results|.[]|.name')
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
# add each tag to list
FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
done
done
# output
for i in ${FULL_IMAGE_LIST}
do
echo ${i}
done
@rqc
Copy link

rqc commented Jun 20, 2016

Hello, thanks for this... are there any bindings for these API endpoints in Go?

@alunduil
Copy link

alunduil commented Jul 14, 2016

The URL endpoints appear to be incorrect. I can't get appropriate responses without using the registry subdomain. Is that intended?

@jriguera
Copy link

jriguera commented Apr 4, 2017

Based on this gist, I have created another one to delete images and/or tags from a organization: https://gist.github.com/jriguera/107d594f0d1d0b0dc1d3c7adebdc1f50

@jacobscarter
Copy link

jacobscarter commented Dec 4, 2017

In case anyone here has trouble. In the above example the variable ${i} was saving with a new line \r at the end. So the second curl to get tags was not returning anything. Here is the fix:

# build a list of all images & tags
for i in ${REPO_LIST}
do
  cr=$'\r'
  i="${i%$cr}"
  # get tags for repo
  IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=100 | jq -r '.results|.[]|.name')

@khelkun
Copy link

khelkun commented Jun 14, 2018

👍 Thanks!
I would sort the output this way:

for i in ${FULL_IMAGE_LIST}
do
  echo ${i} >> temp.txt
done

cat temp.txt | sort -n

rm temp.txt

Not perfect but it's a bit more readable for huge repos like library/mongo

@stefaneg
Copy link

stefaneg commented Nov 2, 2018

Anyone have any luck getting image manifests from hub.docker.com API ?

Documented here:
https://docs.docker.com/registry/spec/api/#manifest

Tried this:

https://hub.docker.com/v2/repositories/${UNAME}/${i}/manifests/${tag}

like could be deduced from the working gist above, but unable to get it to work.

@alexanderilyin
Copy link

alexanderilyin commented Nov 15, 2018

I had to use Docker Hub API v2 for private repo in order to get list of all tags, example is here if anyone needs it:

@Jack-qing
Copy link

Jack-qing commented Nov 16, 2018

Environmental Science:python3.7+flask dokcer:Docker version 18.06.1-ce
False hints:line 19: jq: command not found
(23) Failed writing body
problem:ORG="" I don't know what to write.
dock

@jeffguorg
Copy link

jeffguorg commented Dec 5, 2018

Environmental Science:python3.7+flask dokcer:Docker version 18.06.1-ce
False hints:line 19: jq: command not found
(23) Failed writing body
problem:ORG="" I don't know what to write.
dock

@Jack-qing. you need jq. you can install one from brew (brew install jq). let's see if there is any problem after you have jq

@avinash-rafay
Copy link

avinash-rafay commented Jul 3, 2019

Where can I find the official documentation for this docker hub v2 api?

@thatreguy
Copy link

thatreguy commented Nov 8, 2019

Just to add here: This will fetch any user's public repos irrespective of the authorised user.

Copy link

ghost commented Sep 9, 2020

Worked like wonders, for my naive repos <3

@avarne
Copy link

avarne commented Jan 13, 2021

There is a page_size parameter specified in the REST API call which means only 100 images at a time will get listed down. What if I want to list down all images?

@spachecogomez
Copy link

spachecogomez commented Oct 13, 2021

thanks for the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment