侧边栏壁纸
博主头像
玩玩NAS 博主等级

行动起来,活在当下

  • 累计撰写 10 篇文章
  • 累计创建 12 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Docker镜像同步到阿里云/腾讯云 方便国内使用

无名之辈
2025-03-03 / 0 评论 / 0 点赞 / 31 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
本文最后更新于2025-03-19,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Docker 镜像同步到阿里云/腾讯云

在国内使用 Docker 官方镜像仓库(Docker Hub)时,拉取速度通常较慢,甚至会出现超时问题。为了提高下载速度,我们可以将常用的 Docker 镜像同步到 阿里云容器镜像服务(ACR)腾讯云容器镜像服务(TCR),并通过国内的加速节点快速拉取。

写在最前面,以下方法需要本身有能力能下载到镜像,方便的是一次同步,处处可用。你可以用各种加速镜像地址先拉到镜像,或者你本身就有一个国外的服务。

1. 阿里云 ACR 镜像同步

1.1 登录阿里云并创建 ACR 仓库

  1. 进入 阿里云容器镜像服务
    image.png

  2. 创建 命名空间(Namespace)
    image.png

  3. 创建 镜像仓库,支持公有或私有

1.2 配置 Docker 登录阿里云 ACR

image.png

  root@VM-8-5-debian:/home# sudo docker login --username=naspt crpi-pqqbvdf8c8dv7tyr.cn-shanghai.personal.cr.aliyuncs.com
  Password: #输入你的账号密码
  WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  Configure a credential helper to remove this warning. See
  https://docs.docker.com/engine/reference/commandline/login/#credential-stores
  
  Login Succeeded    #提示登录成功

1.3 同步镜像到阿里云

  1. 创建一个registry.json文件存放你的阿里云账号信息。
    {
      "registry":"crpi-pqqbvdf8c8dv7tyr.cn-shanghai.personal.cr.aliyuncs.com/xxxx",  #xxx改成你的一开始设置的命名空间
      "name":"naspt",    #改成你的用户名
      "password": "xxxx"    #xxx上面设置的固定密码
    }
    
  2. 创建一个images.txt文件存放你想要拉取的镜像信息,一行一个,不加tag默认拉取最新的。
    xinjiawei1/emby_unlockd
    
  3. 创建一个py文件,这是一个拉取镜像通过 skopeo直接复制到对应的镜像空间也就是我们刚刚的阿里云空间。
    import subprocess
    import json
    
    with open("registry.json", 'r') as f:
        registry_dict = json.loads(f.read())
    harbor_addr = registry_dict['registry']
    username = registry_dict['name']
    passowrd = registry_dict['password']
    with open('images.txt', 'r') as f:
        for image in f.readlines():
            image_name = image.strip()
            if not image_name:
                continue
            image_path = image_name.split("/")[-1]
            command = [
                '/usr/bin/skopeo', 'copy',
                f'docker://{image_name}',
                f'--dest-creds', f'{username}:{passowrd}',
                f'--dest-tls-verify=false',
                f'docker://{harbor_addr}/{image_path}'
            ]
            print(' '.join(command))
            process = subprocess.Popen(command,
                                       stdout=None,
                                       stderr=None,
                                       )
            process.wait()
            if process.returncode == 0:
                print("\nDownload completed successfully.")
            else:
                print("\nDownload failed with errors.")
    

三个文件在同一个路径下
image-tsht.png

1.4 在国内服务器拉取加速

#执行
python3 syncDocker.py 

1.5 看到下图信息,就代表成功推送到了我们刚刚创建的阿里云仓库

image-wmvj.png

image-enlc.png

2. 腾讯云 TCR 镜像同步

2.1 登录腾讯云并创建 TCR 仓库

  1. 进入 腾讯云容器镜像服务
  2. 创建 命名空间(Namespace)
    image-nuhx.png

2.2 配置 Docker 登录腾讯云 TCR

    # 登录腾讯云 TCR
    root@VM-8-5-debian:/home#  docker login ccr.ccs.tencentyun.com --username=100005757274   #这命令需要自己先创建一个仓库,随便创建,然后点快捷指令,就可以看到了。
    Password: 
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credential-stores
    
    Login Succeeded #看到这个就代表登录成功了

2.3 同步镜像到腾讯云

  1. 创建一个registry.json文件存放你的腾讯云账号信息。
    {
      "registry":"ccr.ccs.tencentyun.com/xxx",  #xxx改成你的一开始设置的命名空间
      "name":"100005757274",    #改成你的用户名
      "password": "xxxx"    #xxx上面设置的固定密码
    }
    
  2. 创建一个images.txt文件存放你想要拉取的镜像信息,一行一个,不加tag默认拉取最新的。
    xinjiawei1/emby_unlockd
    
  3. 创建一个py文件,这是一个拉取镜像通过 skopeo直接复制到对应的镜像空间也就是我们刚刚的腾讯云空间。
    腾讯云的和阿里云的有点不同,需要先手动执行一下登录,然后会自动存储登录信息,然后执行下面的py代码就可以了。
       import subprocess
        
        harbor_addr = 'ccr.ccs.tencentyun.com/naspt'
        
        with open('images.txt', 'r') as f:
            for image in f.readlines():
                image_name = image.strip()
                if not image_name:
                    continue
                image_path = image_name.split("/")[-1]
        
                command = [
                    'skopeo', 'copy',
                    f'docker://{image_name}',
                    f'docker://{harbor_addr}/{image_path}'
                ]
                print(' '.join(command))
        
                process = subprocess.Popen(command, stdout=None, stderr=None)
                process.wait()
        
                if process.returncode == 0:
                    print("\nDownload completed successfully.")
                else:
                    print("\nDownload failed with errors.")
    

三个文件在同一个路径下
image-tsht.png

3. 自动化同步方案

如果你需要定期同步某些镜像,可以使用 crontab 来自动执行同步任务。

3.1 使用 Shell 脚本+crontab

创建 sync_docker_images.sh

crontab -e

定时执行任务(每个小时更新一次,具体频率看自己需求):

0 * * * * /usr/bin/python3 /path/to/sync.py

4. 结论

通过 阿里云 ACR腾讯云 TCR,我们可以大幅提升 Docker 镜像的拉取速度,尤其适用于国内服务器。可以手动同步,也可以利用 Shell 脚本 + crontab
你可以根据自身需求,选择适合的云厂商进行 Docker 镜像同步!

0

评论区