Implementation ideas for docker registry image synchronization

Implementation ideas for docker registry image synchronization

Intro

Previously, our docker images were stored in Azure's Container Registry. Recently, we built our own docker registry. We want to synchronize the docker images previously saved in Azure's Container Registry to our own docker registry.

Implementation ideas

Our method is relatively simple and low-level, but it can basically meet the requirements.

Our approach is

  • First, get the list of all images in the source registry
  • Then get the image tags one by one
  • Then traverse and pull the corresponding images to the local computer, then docker tag them and name them as the new registry image name
  • Then push the docker image to the new registry
  • Delete the image downloaded to the local computer and the image pushed to the new registry

Later, I suddenly remembered that Alibaba Cloud seems to have an image synchronization tool, https://github.com/AliyunContainerService/image-syncer image-syncer is a docker image synchronization tool that can be used for many-to-many image repository synchronization. It supports most of the current mainstream docker image repository services. It is great to see the introduction. If you need to synchronize images between registries, you can try this tool. According to the introduction, this tool will not pull to the local disk. After getting the image data from the source registry, it will be directly pushed to the new registry, which will be much more efficient.

Docker-Registry API

Docker registry has a set of specifications, you can refer to https://docs.docker.com/registry/spec/api/ for more information

Get all images

Docker registry v2 adds a new _catalog API to get all images. v1 can be replaced by _search

The syntax is as follows:

GET /v2/_catalog

By default, a maximum of 100 records are returned. If the number of records is greater than 100, you can specify the number of records to return by using the parameter n . If you want to use paging, you can specify another parameter last to specify the last image to return after the previous page. For example: http://example.com/v2/_catalog?n=20&last=b

Get the image tag

To get the tag list of the docker image, you can use GET /v2/<repository-name>/tags/list to get it. You can also page it, similar to getting the image list above. You can use n and last to achieve paged loading.

Operation Example

A test docker registry is deployed locally for demonstration. I use httpie for testing.

Get the mirror list:

Call the _catalog interface to get the image list

http://5000/v2/_catalog

Get the tag list of the image

Call tags/list interface to obtain the image tag

http://5000/v2/busybox/tags/list
http:5000/v2/redis/tags/list

PowerShell script

Any operation and maintenance that is not automated is a hooligan. It is very likely that there will be similar needs in the future. It is better to write a script to run it automatically.

The following script is a bit simplified because there are not many images on our azure container registry, only about 50 or 60 images, and the image only has the latest tag, no other tags, so the above steps are simplified, and not all images are obtained by paging, nor all tags are obtained. If you actually use it, please modify it yourself before using it

# variables
$srcRegUser = "xxx"
$srcRegPwd = "111111"
$srcRegHost = "xxx.azurecr.cn"
$destRegUser = "yyy"
$destRegPwd = "222"
$destRegHost = "registry.xxx.com"

# get repositories from source registry
# httpie
$response = (http -b -a "${srcRegUser}:${srcRegPwd}" "https://${srcRegHost}/v2/_catalog") | ConvertFrom-Json
# curl
#$response = (curl -u "${srcRegUser}:${srcRegPwd}" "https://${srcRegHost}/v2/_catalog") | ConvertFrom-Json
# repository
$repositories = $response.repositories

#
Write-Host $repositories

# login source registry
docker login $srcRegHost -u $srcRegUser -p $srcRegPwd
# login dest registry
docker login $destRegHost -u $destRegUser -p $destRegPwd

# sync
foreach($repo in $repositories)
{
  Write-Host "sync $repo begin"

  $srcTag = "${srcRegHost}/${repo}:latest"
  $destTag = "${destRegHost}/${repo}:latest"

  Write-Host "source image tag: $srcTag"
  Write-Host "dest image tag $destTag"

  Write-Host "docker pull $srcTag begin"

  docker pull $srcTag

  Write-Host "docker pull $srcTag completed"

  Write-Host "docker tag $srcTag $destTag ing"

  docker tag $srcTag $destTag

  Write-Host "docker push $destTag begin"

  docker push $destTag

  Write-Host "docker push $destTag completed"
  
  Write-Host "docker rmi $srcTag $destTag begin"

  docker rmi $srcTag $destTag

  Write-Host "docker rmi $srcTag $destTag end"

  Write-Host "sync $repo completed"
}

Write-Host "Completed..."

More

If you need to synchronize many images, consider using Alibaba Cloud's image synchronization tool to synchronize them.

Reference

https://stackoverflow.com/questions/31251356/how-to-get-a-list-of-images-on-docker-registry-v2

https://github.com/AliyunContainerService/image-syncer

https://docs.docker.com/registry/spec/api/

Summarize

This is the end of this article about docker registry image synchronization. For more related docker registry image content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to implement Docker Registry to build a private image warehouse
  • How to query or obtain images in a private registry
  • Detailed explanation of Docker domestic image pulling and image acceleration registry-mirrors configuration modification
  • How to create a private repository using a Docker registry image
  • Detailed explanation of Docker Registry image deletion and garbage collection

<<:  Briefly understand the MYSQL database optimization stage

>>:  A complete guide to CSS style attributes css() and width() in jQuery

Recommend

Why does MySQL paging become slower and slower when using limit?

Table of contents 1. Test experiment 2. Performan...

AsyncHooks asynchronous life cycle in Node8

Async Hooks is a new feature of Node8. It provide...

JavaScript removes unnecessary properties of an object

Table of contents Example Method 1: delete Method...

Weird and interesting Docker commands you may not know

Intro Introduces and collects some simple and pra...

Some slightly more complex usage example codes in mysql

Preface I believe that the syntax of MySQL is not...

How to collect Nginx logs using Filebeat

Nginx logs can be used to analyze user address lo...

Detailed explanation of the use of MySQL DML statements

Preface: In the previous article, we mainly intro...

Pure CSS to achieve cloudy weather icon effect

Effect The effect is as follows ​ Implementation ...

uniapp Sample code for implementing global sharing of WeChat mini-programs

Table of contents Create a global shared content ...

Web designers also need to learn web coding

Often, after a web design is completed, the desig...

The pitfalls and solutions caused by the default value of sql_mode in MySQL 5.7

During normal project development, if the MySQL v...

Summary and examples of vue3 component communication methods

The communication modes of vue3 components are as...

Detailed graphic tutorial on how to enable remote secure access with Docker

1. Edit the docker.service file vi /usr/lib/syste...