Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludenus/9c2770ec85676322bd964df75508f3b0 to your computer and use it in GitHub Desktop.
Save ludenus/9c2770ec85676322bd964df75508f3b0 to your computer and use it in GitHub Desktop.
Get the list of images and tags for a Docker Hub organization account (UPD: traverse all pages)
#!/bin/bash
# Example for the Docker Hub V2 API
# Returns all images and tags associated with a Docker Hub organization account.
# Requires 'jq': https://stedolan.github.io/jq/
export PATH=/tmp/bin:$PATH
if [ ! `which jq` ]; then
mkdir -p /tmp/bin
curl -s -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 --output /tmp/bin/jq
chmod 755 /tmp/bin/jq
fi
# set username, password, and organization
export UNAME=${UNAME:-"dockerhub_user"}
export UPASS=${UPASS:-"password"}
export ORG=${ORG:-"organization"}
# -------
set -e
echo
# get token
echo "Retrieving 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
echo "Retrieving repository list ..."
REPO_LIST=""
total=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/?page_size=1 | jq . | jq -r .count)
pages=`echo $(( total / 100 + 1))`
for ((page=1;page<=$pages;page++)); do
repos=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/?page=${page}\&page_size=100 | jq -r '.results|.[]|.name' | sort)
REPO_LIST="${REPO_LIST} ${repos}"
done
# output images & tags
echo
echo "Images and tags for organization: ${ORG}"
echo
for repo in ${REPO_LIST}
do
IMAGE_TAGS=""
total=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/${repo}/tags/?page_size=1 | jq . | jq -r .count)
pages=`echo $(( total / 100 + 1))`
for ((page=1;page<=$pages;page++)); do
tags=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${ORG}/${repo}/tags/?page=${page}\&page_size=100 | jq -r '.results|.[]|.name' | sort)
IMAGE_TAGS="${IMAGE_TAGS} ${tags}"
done
for tag in ${IMAGE_TAGS}
do
echo "${ORG}/${repo}:${tag}"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment