Last active
December 14, 2022 21:22
-
-
Save charlieoleary/05c1dab2e19703f73a041e585e060dc9 to your computer and use it in GitHub Desktop.
Migrate your Docker Hub organization to ECR.
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 | |
# | |
# A simple bash script to migrate your Docker Hub organization to ECR with pretty minimal effort. | |
# Adapted from kizbitz's original script -- https://gist.github.com/kizbitz/e59f95f7557b4bbb8bf2 | |
# | |
# In order for this to work you must run this from a system that is logged in to both the ECR | |
# repository you are migrating TO and the Docker Hub organization you are migrating FROM. | |
# | |
# Due to how Docker works (pulling / pushing images), this script can be run over and over | |
# without any issues. Note that ECR authentication times out after 24 hours, so if you are | |
# exporting a significant number of images (or large images in general) your authentication | |
# may time out. | |
# | |
# 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. | |
# ECR_REGION - The AWS region your ECR repositories resides within. | |
# ECR_REGISTRY - The ECR registry. | |
# | |
# OPTIONAL ENVIRONMENT VARIABLES: | |
# DH_MAX_TAGS - The number of tags to migrate from Docker Hub to ECR (default: 10). | |
# | |
set -e | |
# check to make sure required environment variables are set | |
if [ -z $DH_USERNAME ] || [ -z $DH_PASSWORD ] || [ -z $DH_ORG ] || [ -z $ECR_REGION ] || [ -z $ECR_REGISTRY ]; then | |
echo "DH_USERNAME, DH_PASSWORD, DH_ORG, ECR_REGION, and ECR_REGISTRY must all be set." | |
exit 2 | |
fi | |
# the number of docker images to migrate from dockerhub to ecr | |
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 | |
# first, create a repository in ecr. it may already exist, so simply ignore the error if it does. | |
aws ecr create-repository --region ${ECR_REGION} --repository-name ${DH_ORG}/${repo} || true | |
# 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} | |
# pull the tagged image from dockerhub | |
echo "-> Pulling ${IMAGE_NAME}..." | |
docker pull ${IMAGE_NAME} && \ | |
# tag it with the ecr repository | |
echo " Tagging ${ECR_REGISTRY}/${IMAGE_NAME}..." && \ | |
docker tag ${IMAGE_NAME} ${ECR_REGISTRY}/${IMAGE_NAME} && \ | |
# and finally, push it to ecr | |
echo " Pushing ${ECR_REGISTRY}/${IMAGE_NAME}..." && \ | |
docker push ${ECR_REGISTRY}/${IMAGE_NAME} | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment