Docker 镜像同步到阿里云/腾讯云
在国内使用 Docker 官方镜像仓库(Docker Hub)时,拉取速度通常较慢,甚至会出现超时问题。为了提高下载速度,我们可以将常用的 Docker 镜像同步到 阿里云容器镜像服务(ACR) 或 腾讯云容器镜像服务(TCR),并通过国内的加速节点快速拉取。
写在最前面,以下方法需要本身有能力能下载到镜像,方便的是一次同步,处处可用。你可以用各种加速镜像地址先拉到镜像,或者你本身就有一个国外的服务。
1. 阿里云 ACR 镜像同步
1.1 登录阿里云并创建 ACR 仓库
-
进入 阿里云容器镜像服务
-
创建 命名空间(Namespace)
-
创建 镜像仓库,支持公有或私有
1.2 配置 Docker 登录阿里云 ACR
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 同步镜像到阿里云
- 创建一个registry.json文件存放你的阿里云账号信息。
{ "registry":"crpi-pqqbvdf8c8dv7tyr.cn-shanghai.personal.cr.aliyuncs.com/xxxx", #xxx改成你的一开始设置的命名空间 "name":"naspt", #改成你的用户名 "password": "xxxx" #xxx上面设置的固定密码 }
- 创建一个images.txt文件存放你想要拉取的镜像信息,一行一个,不加tag默认拉取最新的。
xinjiawei1/emby_unlockd
- 创建一个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.")
三个文件在同一个路径下
1.4 在国内服务器拉取加速
#执行
python3 syncDocker.py
1.5 看到下图信息,就代表成功推送到了我们刚刚创建的阿里云仓库
2. 腾讯云 TCR 镜像同步
2.1 登录腾讯云并创建 TCR 仓库
- 进入 腾讯云容器镜像服务
- 创建 命名空间(Namespace)
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 同步镜像到腾讯云
- 创建一个registry.json文件存放你的腾讯云账号信息。
{ "registry":"ccr.ccs.tencentyun.com/xxx", #xxx改成你的一开始设置的命名空间 "name":"100005757274", #改成你的用户名 "password": "xxxx" #xxx上面设置的固定密码 }
- 创建一个images.txt文件存放你想要拉取的镜像信息,一行一个,不加tag默认拉取最新的。
xinjiawei1/emby_unlockd
- 创建一个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.")
三个文件在同一个路径下
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 镜像同步!
评论区