Skip to content

Instantly share code, notes, and snippets.

@alexlovelltroy
Created May 7, 2023 04:10
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 alexlovelltroy/bc4e6ca4ca7fc7a9a5934aed67e4e29e to your computer and use it in GitHub Desktop.
Save alexlovelltroy/bc4e6ca4ca7fc7a9a5934aed67e4e29e to your computer and use it in GitHub Desktop.
Mirror a set of packages from public repositories to a new private ECR
#!/bin/bash
# Define the containers you want to mirror from Docker Hub to ECR
containers=(\
"confluentinc/cp-zookeeper:6.1.11"\
"confluentinc/cp-kafka:6.1.11"\
"minio/mc:RELEASE.2023-04-12T02-21-51Z" \
"hashicorp/vault:1.13" \
"postgres:11-alpine" \
"alpine:3.16" \
"alpine:3.17" \
"redis:7-alpine" \
"postgres:11-alpine" \
"postgres:15-alpine" \
"golang:1.20-alpine" \
"golang:1.16-alpine" \
"quay.io/coreos/etcd:v3.5.8"
)
# Set your AWS region and ECR repository
region="us-east-1"
repo_prefix="716215456011.dkr.ecr.us-east-1.amazonaws.com"
for container in "${containers[@]}"
do
# Pull the container from Docker Hub
docker pull "$container"
# Parse the image name and tag
image_name=$(echo "$container" | cut -d':' -f1)
domain=$(echo "$image_name" | cut -d'/' -f1)
# Strip the domain from the image name if present
if [[ "$domain" == *"."* ]]; then
image_name=$(echo "$image_name" | sed -E 's/^[^\/]*\/([^:]+):?.*$/\1/' )
fi
tag=$(echo "$container" | cut -d':' -f2)
# Create the ECR repository if it doesn't exist
aws ecr describe-repositories --region "$region" --repository-names "$image_name" >/dev/null 2>&1 || \
aws ecr create-repository --region "$region" --repository-name "$image_name"
# Tag and push the image to ECR
full_repo_name="$repo_prefix/$image_name"
docker tag "$container" "$full_repo_name:$tag"
docker push "$full_repo_name:${tag/\//\\/}"
# Remove the local copy of the image
docker rmi "$container" "$full_repo_name:$tag"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment