Linux · 2 分钟阅读
12. Docker Hub 加速器
国内访问 Docker Hub 慢是常态。镜像加速器只是过渡方案,长期应该走阿里云 ACR、Harbor 或自建 mirror。
常见加速器
| 服务 | 地址 | 备注 |
|---|---|---|
| 阿里云 | https://<your-id>.mirror.aliyuncs.com |
需要阿里云账号拿个人 ID |
| 腾讯云 | https://mirror.ccs.tencentyun.com |
内网环境好用 |
| 网易 | https://hub-mirror.c.163.com |
公网可用,2024 起偶发不稳 |
| 百度云 | https://mirror.baidubce.com |
偶发延迟 |
阿里云的地址是账号相关的,每个人的 ID 都不同。
通过 daemon.json
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": [
"https://mirror.ccs.tencentyun.com",
"https://hub-mirror.c.163.com"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
验证
docker info | grep -A3 "Registry Mirrors"
time docker pull nginx:1.27
注意事项
daemon.json是 JSON,最后一行不能加逗号- 修改后必须
daemon-reload+restart,否则不生效 - 多个加速器按顺序使用,第一个挂掉才会用第二个
- 内网环境用内网 mirror 比公网快一截
latest不命中缓存时仍然要打外网,固定 tag 才能真正受益
不开加速器的选项
- 用
docker save/load在内网同步镜像 - 自建 mirror 镜像(
--registry启动参数) - 全部业务镜像走私有 registry(Harbor / Quay / ACR),只对外部基础镜像走加速
排错
# 启动日志
journalctl -u docker -n 200
# 拉镜像失败具体错误
docker pull nginx:1.27
常见错误:
connection refused— 加速器地址写错x509: certificate is not yet valid— 系统时间不对i/o timeout— DNS 解析失败或公司防火墙挡了 443