Skip to content

Instantly share code, notes, and snippets.

@immanuelpotter
Last active April 25, 2025 11:03
Show Gist options
  • Save immanuelpotter/ca369ca45ec80609303c8124e5a1f33d to your computer and use it in GitHub Desktop.
Save immanuelpotter/ca369ca45ec80609303c8124e5a1f33d to your computer and use it in GitHub Desktop.
Mirror container images from public image repo to ECR
#!/usr/bin/env bash
profile="$AWS_PROFILE"
region="$AWS_REGION"
public_image="$1"
usage(){
echo "Usage: AWS_PROFILE=blah AWS_REGION=blah $0 [public_image_repo/image_name:tag]"
echo "Example: AWS_PROFILE=hello AWS_REGION=eu-west-2 ./push-image-to-ecr.sh redis/redis-stack:v2.2.0"
echo "The original dockerhub image will then be available at AWS_ACCOUNT_ID.dkr.ecr.eu-west-2.amazonaws.com/redis/redis-stack:v2.2.0"
exit 3
}
get_account_id(){
account_id=$(aws sts get-caller-identity --query Account --output text --profile $profile --region $region)
if [ $? -ne 0 ]; then
echo "Error: Unable to get AWS account ID"
exit 1
fi
echo "$account_id"
return $account_id
}
main(){
if [ -z "$profile" ] || [ -z "$region" ] || [ -z $public_image ]; then
usage
fi
account_id=$(get_account_id)
docker pull $public_image
aws ecr get-login-password --region $region | docker login --username AWS --password-stdin ${account_id}.dkr.ecr.${region}.amazonaws.com
docker tag $public_image ${account_id}.dkr.ecr.${region}.amazonaws.com/${public_image}
echo docker push ${account_id}.dkr.ecr.${region}.amazonaws.com/${public_image}
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment