Skip to content

Instantly share code, notes, and snippets.

@sdvcrx
Last active December 28, 2021 02:54
Show Gist options
  • Save sdvcrx/af579f5c68fc782c65d8b7f9de6b1aa8 to your computer and use it in GitHub Desktop.
Save sdvcrx/af579f5c68fc782c65d8b7f9de6b1aa8 to your computer and use it in GitHub Desktop.
Sync dockerhub images to ghcr.io or other registry
"""
Sync dockerhub images to ghcr.io or other registry
Usage:
1. create tag list file `tags.txt` that syncing to ghcr.io
```
git tag > tags.txt
# or get from dockerhub API
curl -s https://registry.hub.docker.com/v1/repositories/[IMAGE_NAME]/tags | jq -r 'map(.name) | .[]' > tags.json
```
2. Run script with `--dry-run` options to test
```
python ./migrate.py --name [IMAGE_NAME] --dry-run
```
3. Actually run script
```
python ./migrate.py --name [IMAGE_NAME]
```
Other options:
```
python ./migrate.py --help
```
"""
import argparse
import subprocess
from typing import List
parser = argparse.ArgumentParser(description='Sync docker image to other registry')
parser.add_argument('--dry-run', action='store_true', help='Dry run')
parser.add_argument('--registry', default='ghcr.io', help="Registry name, default is ghcr.io")
parser.add_argument("--name", type=str, required=True, help="Image name")
args = parser.parse_args()
image_name = args.name
PREFIX = args.registry
def exec(cmd: str):
print(cmd)
if args.dry_run:
return
subprocess.run(cmd, shell=True, capture_output=True)
def get_tags() -> List[str]:
with open('./tags.txt') as f:
return [l.strip() for l in f.readlines()]
def convert_and_push(tag: str):
image = f'{image_name}:{tag}'
ghcr_image_name = f'{PREFIX}/{image}'
exec(f'docker pull {image}')
exec(f'docker image tag {image} {ghcr_image_name}')
exec(f'docker push {ghcr_image_name}')
exec(f'docker image rm {image} {ghcr_image_name}')
if __name__ == '__main__':
tags = get_tags()
if args.dry_run:
print(args.name)
convert_and_push(tags[0])
exit(0)
for tag in tags:
convert_and_push(tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment