Skip to content

Instantly share code, notes, and snippets.

@rblaine95
Last active July 23, 2018 10:54
Show Gist options
  • Save rblaine95/2632338723f596acffc9f96900e90586 to your computer and use it in GitHub Desktop.
Save rblaine95/2632338723f596acffc9f96900e90586 to your computer and use it in GitHub Desktop.
Query DockerHub for a list of all images and image tags associated with a user account, then pull all of them, tag them with a new registry, push them, and then delete them from the localhost.
#!/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=""
REGISTRY_URL="localhost:5000"
# -------
which jq > /dev/null
if [[ $? -ne 0 ]]; then
echo "Install jq"
exit 1
fi
which curl > /dev/null
if [[ $? -ne 0 ]]; then
echo "Install curl"
exit 1
fi
# -------
#set -e
# aquire token
getToken(){
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
getImageList() {
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
getTagList(){ # not needed because of --all-tags flag
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
}
batch() {
ptr=0
image_list_arr=($FULL_IMAGE_LIST)
# pull 10 images at a time
while [[ $ptr -lt ${#image_list_arr[@]} ]]; do
for x in {0..9}; do
docker pull ${image_list_arr[$ptr]} &
pids[${x}]=$!
ptr=$((ptr + 1))
done
for pid in ${pids[*]}; do
wait $pid
done
done
# tag 10 images at a time
ptr=0
while [[ $ptr -lt ${#image_list_arr[@]} ]]; do
for x in {0..9}; do
docker tag ${image_list_arr[$ptr]} ${REGISTRY_URL}/${image_list_arr[$ptr]} &
pids[${x}]=$!
ptr=$((ptr + 1))
done
for pid in ${pids[*]}; do
wait $pid
done
done
# push 10 new tags at a time
ptr=0
while [[ $ptr -lt ${#image_list_arr[@]} ]]; do
for x in {0..9}; do
docker push ${REGISTRY_URL}/${image_list_arr[$ptr]} &
pids[${x}]=$!
ptr=$((ptr + 1))
done
for pid in ${pids[*]}; do
wait $pid
done
done
# delete all images in list
for i in ${FULL_IMAGE_LIST}; do
docker rmi -f docker.io/${i}
docker rmi -f ${REGISTRY_URL}/${i}
done
}
stream() { # one image at a time
for i in ${FULL_IMAGE_LIST}; do # for each image in list
docker pull ${i} # pull that image
docker tag ${i} ${REGISTRY_URL}/${i} # tag that image
docker push ${REGISTRY_URL}/${i} # push new tag of that image
docker rmi -f docker.io/${i} # delete old tag of that image
docker rmi -f ${REGISTRY_URL}/${i} # delete new tag of that image
done
}
main() {
getToken
getImageList
echo "[========================]"
echo "Batch or Stream migration?"
echo "[========================]"
read -p "Enter: " bs
case $bs in
[bB]* )
getTagList
batch;;
[sS]* )
getTagList
stream;;
* ) exit 1;;
esac
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment