Forked from kizbitz/dockerhub-v2-api-organization.sh
Last active
December 6, 2023 09:26
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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