Automating Docker Image Migration Between Private Registries
I needed to move my private Docker registry to a new server, and I decided to take the opportunity to improve my setup. Instead of just rsync
-ing the files, I chose to use an S3-compatible storage backend (MinIO) for managing Docker registry files. I already had MinIO in place, and it made backups much easier to manage.
The challenge? I needed to clone a large number of repositories with multiple tags, and doing it manually would be tedious and time-consuming. To solve this, I created a simple Bash script that uses the Docker Registry API, jq
, curl
, and skopeo
to automate the process.
Here’s the Bash script I used. It fetches repositories and tags from the source registry and migrates them to the target registry using the powerful skopeo
tool.
#!/bin/bash SOURCE_REGISTRY="source-registry.xxxxx.xxx" TARGET_REGISTRY="target--registry.xxxxx.xxx" USERNAME="XXXXXXXX" PASSWORD="XXXXXXX" # Fetch all repositories from the source registry REPOS=$(curl -s -u ${USERNAME}:${PASSWORD} https://${SOURCE_REGISTRY}/v2/_catalog | jq -r '.repositories[]') # Loop through each repository for repo in $REPOS; do echo "Processing repository: $repo" # Fetch all tags for the repository TAGS=$(curl -s -u ${USERNAME}:${PASSWORD} https://${SOURCE_REGISTRY}/v2/${repo}/tags/list | jq -r '.tags[]') # Loop through each tag for tag in $TAGS; do SOURCE_IMAGE="${SOURCE_REGISTRY}/${repo}:${tag}" TARGET_IMAGE="${TARGET_REGISTRY}/${repo}:${tag}" echo "Migrating ${SOURCE_IMAGE} to ${TARGET_IMAGE}" # Copy the image using Skopeo skopeo copy --all \ --src-creds ${USERNAME}:${PASSWORD} \ --dest-creds ${USERNAME}:${PASSWORD} \ docker://${SOURCE_IMAGE} \ docker://${TARGET_IMAGE} if [ $? -ne 0 ]; then echo "Error migrating ${SOURCE_IMAGE}" else echo "Successfully migrated ${SOURCE_IMAGE}" fi done done echo "Migration completed!"How it's works:
- Fetching Repositories: The script uses the Docker Registry HTTP API to list all repositories from the source registry.
- Fetching Tags for Each Repository: It queries all available tags for each repository.
- Using
skopeo
to Copy Images:skopeo
directly transfers images between registries without the need to pull and push them manually. - Error Handling: Logs success and failure for each image, ensuring transparency in the migration process.
Used tools:
- Docker Registry HTTP API: Documentation
- jq: A lightweight and flexible command-line JSON processor for parsing and manipulating API responses. Homepage | GitHub
- skopeo: A powerful tool for managing container images Homepage | Installation Guide