Skip to content

Instantly share code, notes, and snippets.

@Raboo
Forked from charlieoleary/migrate-to-ecr.sh
Last active December 14, 2022 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raboo/ff3b287852ddd53cb4a122dfe5c9ebfc to your computer and use it in GitHub Desktop.
Save Raboo/ff3b287852ddd53cb4a122dfe5c9ebfc to your computer and use it in GitHub Desktop.
Migrate your Docker Hub organization(docker.io) to Github Container Registry(ghcr.io).
#!/bin/bash
#
# A simple bash script to migrate your Docker Hub organization to Github Container Registry.
# forked from https://gist.github.com/charlieoleary/05c1dab2e19703f73a041e585e060dc9
#
# In order for this to work you must run this from a system that is logged in to both the GHCR
# repository you are migrating to and the Docker Hub organization you are migrating FROM with
# podman.
#
# Due to how Docker works (pulling / pushing images), this script can be run over and over
# without any issues.
#
# Finally, it's important to note that you will be downloading these images to the machine you
# run this script on. Be aware that it can consume a considerable amount of disk space if you
# are exporting a large number of images.
#
# REQUIRED ENVIRONMENT VARAIBLES:
# DH_USERNAME - The Docker Hub username with permissions to the organization repositories.
# DH_PASSWORD - The password for the Docker Hub user.
# DH_ORG - The name of the Docker Hub organization you are migrating.
#
# OPTIONAL ENVIRONMENT VARIABLES:
# DH_MAX_TAGS - The number of tags to migrate from Docker Hub to GHCR (default: 10).
#
set -e
# check to make sure required environment variables are set
if [ -z "$DH_USERNAME" ] || [ -z "$DH_PASSWORD" ] || [ -z "$DH_ORG" ]; then
echo "Variables DH_USERNAME, DH_PASSWORD, DH_ORG must all be set."
exit 2
fi
# the number of docker images to migrate from dockerhub to ecr
DH_MAX_TAGS=${DH_MAX_TAGS:-10}
# get token to be able to talk to dockerhub
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${DH_USERNAME}'", "password": "'${DH_PASSWORD}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# get list of repos for the organization
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${DH_ORG}/?page_size=100 | jq -r '.results|.[]|.name')
# start the migration
for repo in ${REPO_LIST}; do
# next, get the specified number of tags in the repository
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${DH_ORG}/${repo}/tags/?page_size=${DH_MAX_TAGS}" | jq -r '.results|.[]|.name')
# build a list of images from tags
for tag in ${IMAGE_TAGS}; do
IMAGE_NAME=${DH_ORG}/${repo}:${tag}
echo Pulling docker.io/"${IMAGE_NAME}"
podman pull docker.io/"${IMAGE_NAME}"
echo Pushing ghcr.io/"${IMAGE_NAME}"
podman push docker.io/"${IMAGE_NAME}" ghcr.io/"${IMAGE_NAME}"
echo adding docker.io/"${IMAGE_NAME}" and ghcr.io/"${IMAGE_NAME}" to delete list "(delete.sh)"
echo podman rmi docker.io/"${IMAGE_NAME}" >> delete.sh
echo podman rmi ghcr.io/"${IMAGE_NAME}" >> delete.sh
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment