Skip to content

Instantly share code, notes, and snippets.

@omerfsen
Created January 21, 2023 17:20
Show Gist options
  • Save omerfsen/fc2b2b32d4c91dddbaf391aeb385acc9 to your computer and use it in GitHub Desktop.
Save omerfsen/fc2b2b32d4c91dddbaf391aeb385acc9 to your computer and use it in GitHub Desktop.
dockerhub-search-for-image.sh
#!/bin/bash
#
# search dockerhub via APIv2 and print images and tags.
# get token to be able to talk to Docker Hub
TOKEN=dckr_pat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#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)
UNAME=${1}
FILTER=${2}
# get list of namespaces accessible by user (not in use right now)
#NAMESPACES=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]')
# get list of repos for that user account
if [[ ! -z ${UNAME} ]]
then
if [[ -z ${FILTER} ]]
then
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000 | jq -r '.results|.[]|.name')
else
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000 | jq -r '.results|.[]|.name' | grep "${FILTER}")
fi
else
echo "You did not provided a Dockerhub user or Org."
exit 20
fi
# 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=10000 | 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 list of all docker images
for i in ${FULL_IMAGE_LIST}
do
echo ${i}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment