Skip to content

Instantly share code, notes, and snippets.

@anadimisra
Last active January 12, 2021 06:16
Show Gist options
  • Save anadimisra/efb197849fbf16dff8653acf1441fc9d to your computer and use it in GitHub Desktop.
Save anadimisra/efb197849fbf16dff8653acf1441fc9d to your computer and use it in GitHub Desktop.
#!/bin/bash
srcreg="my.privateregistry.com"
tgtreg="awsacctnum.dkr.ecr.awsregion.amazonaws.com"
repos=`curl -s http://$srcreg/v2/_catalog?n=2048 | jq '.repositories[]' | tr -d '"'`
for repo in $repos; do
echo -e "\n===WORKING ON REPOSITORY" $repo"==="
#check for existing repo at ECR and create if not exist
awsrepo=`aws ecr describe-repositories | grep -o \"$repo\" | tr -d '"'`
if [ "$awsrepo" != "$repo" ]; then aws ecr create-repository --repository-name $repo; fi
tags=`curl -s http://$srcreg/v2/$repo/tags/list?n=2048 | jq '.tags[]' | tr -d '"'`
#check for existing tags at ECR and push if not exist
for tag in $tags; do
awstag=`aws ecr list-images --repository-name $repo | grep -o \"$tag\" | tr -d '"'`
echo -e "\n===WORKING ON" $repo:$tag"==="
if [ "$awstag" != "$tag" ]; then
echo -e "\n===PULLING==="
docker pull $srcreg/$repo:$tag
echo -e "\n===RETAGGING==="
docker tag $srcreg/$repo:$tag $tgtreg/$repo:$tag
echo $srcreg/$repo:$tag "retagged to" $tgtreg/$repo:$tag
echo -e "\n===PUSHING==="
docker push $tgtreg/$repo:$tag
fi
done
#check local images and cleanup if necessary
cleanup=`docker images -a | grep "$repo "`
if [ "$cleanup" != "" ]; then
echo -e "\n===CLEANING UP==="
#add -f at end of the command below to force cleanup of matching repository images on local disk.
#this may result in extra network traffic and longer migration times if you have inter-repo dependencies.
#especially useful if disk space is a factor - only the working repo is retained on disk.
docker images -a | grep $repo | awk '{print $3}' | xargs docker rmi
fi
done
#!/bin/bash
repo=$1
srcreg="my.privateregistry.com"
tgtreg="awsacctnum.dkr.ecr.awsregion.amazonaws.com"
echo -e "\n===WORKING ON REPOSITORY" $repo"==="
#check for existing repo at ECR and create if not exist
awsrepo=`aws ecr describe-repositories | grep -o \"$repo\" | tr -d '"'`
if [ "$awsrepo" != "$repo" ]; then aws ecr create-repository --repository-name $repo; fi
tags=`curl -s http://$srcreg/v2/$repo/tags/list?n=2048 | jq '.tags[]' | tr -d '"'`
#check for existing tags at ECR and push if not exist
for tag in $tags; do
awstag=`aws ecr list-images --repository-name $repo | grep -o \"$tag\" | tr -d '"'`
echo -e "\n===WORKING ON" $repo:$tag"==="
if [ "$awstag" != "$tag" ]; then
echo -e "\n===PULLING==="
docker pull $srcreg/$repo:$tag
echo -e "\n===RETAGGING==="
docker tag $srcreg/$repo:$tag $tgtreg/$repo:$tag
echo $srcreg/$repo:$tag "retagged to" $tgtreg/$repo:$tag
echo -e "\n===PUSHING==="
docker push $tgtreg/$repo:$tag
fi
done
#check local images and cleanup if necessary
cleanup=`docker images -a | grep "$repo "`
if [ "$cleanup" != "" ]; then
echo -e "\n===CLEANING UP==="
#add -f at end of the command below to force cleanup of matching repository images on local disk.
#this may result in extra network traffic and longer migration times if you have inter-repo dependencies.
#especially useful if disk space is a factor - only the working repo is retained on disk.
docker images -a | grep $repo | awk '{print $3}' | xargs docker rmi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment