Skip to content

Instantly share code, notes, and snippets.

@mateuszdrab
Created April 8, 2025 11:34
Show Gist options
  • Save mateuszdrab/a93615e484fba56665fb61512402a622 to your computer and use it in GitHub Desktop.
Save mateuszdrab/a93615e484fba56665fb61512402a622 to your computer and use it in GitHub Desktop.
Simple script to migrate all images from ACR to an alternative registry
#!/bin/bash
# This script migrates images from one Azure Container Registry (ACR) to another registry.
# Run this script in a bash shell with the Azure CLI installed and authenticated.
# Make sure containerd is installed and running.
# Usage: ./migrate-acr-registry.sh <acr_name> <target_registry_name> <target_registry_username> <target_registry_password>
# Primary intention is to migrate images from ACR to another registry including multiplatform images and all tags.
set -ex
# Take registry name from prompt or parameter
acr_name=$1
if [ -z "$acr_name" ]; then
read -p "Enter the name of the ACR registry: " acr_name
fi
# Check if the ACR registry exists
if ! az acr show --name "$acr_name" &> /dev/null; then
echo "ACR registry $acr_name does not exist."
exit 1
fi
# get target registry name from prompt or parameter
target_registry_name=$2
if [ -z "$target_registry_name" ]; then
read -p "Enter the name of the target registry: " target_registry_name
fi
# get target registry username and password from prompt or parameter
target_registry_username=$3
if [ -z "$target_registry_username" ]; then
read -p "Enter the username for the target registry: " target_registry_username
fi
target_registry_password=$4
if [ -z "$target_registry_password" ]; then
read -sp "Enter the password for the target registry: " target_registry_password
echo
fi
# Get fill ACR FQDN
acr_fqdn=$(az acr show --name "$acr_name" --query "loginServer" -o tsv)
# Print the ACR FQDN
echo "ACR FQDN: $acr_fqdn"
# Get ACR refresh token
acr_refresh_token=$(az acr login --name "$acr_name" --expose-token --output tsv --query accessToken)
# Get the list of repositories in the ACR
repositories=$(az acr repository list --name "$acr_name" --output tsv)
# Print the list of repositories
echo "Repositories in ACR $acr_name:"
for repo in $repositories; do
echo "- $repo"
done
# Process tags for each repository
for repo in $repositories; do
echo "Tags for repository $repo:"
tags=$(az acr repository show-tags --name "$acr_name" --repository "$repo" --output tsv)
for tag in $tags; do
echo " - $tag"
# Pull the image from ACR using ctr all platforms
echo "Pulling image $acr_fqdn/$repo:$tag"
ctr image pull --all-platforms --all-metadata --refresh "$acr_refresh_token" "$acr_fqdn/$repo:$tag" || {
echo "Failed to pull image $acr_fqdn/$repo:$tag"
continue
}
# Tag the image for the target registry
echo "Tagging image $acr_fqdn/$repo:$tag to $target_registry_name/$repo:$tag"
ctr image tag --force "$acr_fqdn/$repo:$tag" "$target_registry_name/$repo:$tag" || {
echo "Failed to tag image $acr_fqdn/$repo:$tag to $target_registry_name/$repo:$tag"
continue
}
# Push the image to the target registry
echo "Pushing image $target_registry_name/$repo:$tag"
ctr image push --user "$target_registry_username:$target_registry_password" "$target_registry_name/$repo:$tag" || {
echo "Failed to push image $target_registry_name/$repo:$tag"
continue
}
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment