Docker 提高镜像拉取速度

本文介绍了解决Docker镜像拉取速度慢的方法,包括使用网络加速服务、设置镜像仓库镜像(Registry Mirror)及增加并行下载数量等,并提供了常见问题及其解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:Docker 提高镜像拉取速度(永久地址,保存网址不迷路 🙃)

问题描述

某些 Docker 镜像,由于网络原因,而出现拉取缓慢的情况。

这需要我们通过网络加速服务或者其他方法进行镜像拉取。

该笔记将记录:在 Docker 中,如何解决镜像拉取慢的问题,以及常见问题处理。

解决方案

目前(01/07/2021),有两种方案解决该问题:
1)使用网络加速服务
2)使用”镜像仓库镜像“(Registry Mirror)

方案一、使用网络加速服务(推荐)

// 添加类似如下配置:

# systemctl edit docker.service
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
Environment="HTTPS_PROXY=https://proxy.example.com:443"
Environment="NO_PROXY=localhost,127.0.0.1"

// 显示配置结果:

# systemctl show --property Environment docker.service

// 重新启动服务

# systemctl restart docker.service

更多细节请参考官方文档:
docker pull/Proxy configuration
Control Docker with systemd/HTTP/HTTPS proxy

方案二、使用 Registry Mirror 服务(废弃)

在配置中,使用 registry-mirrors 地址:

# mkdir -pv /etc/docker

# vim /etc/docker/daemon.json
{
    ...
    "registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com", "https://hub-mirror.c.163.com"],
    ...
}

# systemctl reload docker

关于 registry-mirrors 地址:
1)注册阿里云帐号,在 容器镜像服务 / 镜像加速器 中,查看个人地址(每个人的地址前缀都不同);
2)或者,使用其他 Registry Mirror 地址(比如 https://mirror.ccs.tencentyun.com 地址);

# 03/22/2021 这应该是最好的方案,但是事情永远不会那么简单。前些时间 DockerHub 增加拉取频率限制(Increase Rate Limits):匿名用户根据 IP 地址进行限制的,每六小时只能拉取 100 次;普通帐号更具帐号进行限制,每六小时只能拉取 200 次。我们认为该限制会影响国内的 Registry Mirror 站点,因为我们今天发现,通过 Registry Mirror 拉取的 Nginx 镜像是旧的(我们获取的镜像是两个月前,而最新版本是十天前更新的)。

方法三、增加并行下载数量

镜像拉取(下载)也是 HTTP 下载,因此也应该支持并行下载,而官方也提供 max-concurrent-downloads 参数支持。

通过 max-concurrent-downloads 参数,增加并行下载的数量:

# mkdir -pv /etc/docker

# vim /etc/docker/daemon.json
{
    ...
    "max-concurrent-downloads": 3,
    ...
}

# systemctl reload docker

该方法能够与前面的方法配合使用。

常见问题描述

proxyconnect tcp: net/http: TLS handshake timeout

ssl - Docker not able to pull images behind proxy TLS handshake timeout - Stack Overflow

问题描述:执行 docker pull 命令时产生如下信息:

# docker pull google/cadvisor
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: proxyconnect tcp: net/http: TLS handshake timeout

问题原因:配置错误,我们没有单独的 HTTPS 代理,我们的 HTTP 代理支持 HTTPS 代理。

解决办法:

将配置

Environment="HTTPS_PROXY=https://proxy.example.com:443"

修改为

Environment="HTTPS_PROXY=http://proxy.example.com:443"

Upload failed, retrying: remote error: tls: protocol version not supported

Error response from daemon: Get https://registry-1.docker.io/v2/: remote error: tls: handshake failure · Issue #2922 · docker/for-win

问题描述:在上传镜像(docker push)时,产生如下错误:

...
time="2020-11-20T10:40:54.481118132+08:00" level=info msg="Attempting next endpoint for push after error: remote error: tls: protocol version not supported"
time="2020-11-20T10:43:22.178234212+08:00" level=error msg="Upload failed, retrying: remote error: tls: protocol version not supported"
time="2020-11-20T10:43:53.722306200+08:00" level=error msg="Upload failed, retrying: remote error: tls: protocol version not supported"
time="2020-11-20T10:44:34.796173126+08:00" level=error msg="Upload failed, retrying: remote error: tls: protocol version not supported"
time="2020-11-20T10:45:21.055632062+08:00" level=error msg="Upload failed, retrying: EOF"
time="2020-11-20T10:45:39.645404914+08:00" level=error msg="Not continuing with push after error: context canceled"
...

问题原因:「Starting from version 18.09 docker removed support for older tls ciphers.」,而我们使用的网络加速(HTTP PROXY)使用旧版加密算法来建立 HTTPS 连接,因而导致该问题

解决办法:我们使用 Polipo 进行网络加速(转化 SOCKS 协议),但是它不支持配置 TLS 加密。我们只能更换网络加速工具,比如使用 Squid 服务。但是由于时间成本,我们暂时关闭 docker.service 的代理配置以解决问题 :-)

参考文献

docker pull/Proxy configuration
Control Docker with systemd/HTTP/HTTPS proxy
configuration - How do I override or configure systemd services? - Ask Ubuntu
dockerd | Docker Documentation
Increase Rate Limits | Docker
Download rate limit | Docker Documentation

### Docker 镜像时遇到的超时问题解决方案 当尝试通过 `docker pull` 命令获镜像时,可能会因为网络连接或其他原因而遭遇超时。具体表现为访问 `https://registry-1.docker.io/v2/` 需要认证,并且 `auth.docker.io` 这个域名响应缓慢甚至无响应[^2]。 #### 调整客户端请求超时时间 可以通过修改环境变量来增加默认的超时时间: ```bash export DOCKER_CLIENT_TIMEOUT=300 export COMPOSE_HTTP_TIMEOUT=300 ``` 上述命令分别设置了 Docker CLI 和 Docker Compose 的 HTTP 请求最大等待时间为 300 秒(即五分钟)。这有助于缓解由于短暂性的高延迟所引起的超时现象。 #### 修改 Docker 守护程序配置 如果怀疑是本地 Docker 设置存在问题,则应仔细审查 `/etc/docker/daemon.json` 文件中的各项参数是否有误置之处[^3]。对于某些特定场景下的优化建议如下所示: - **HTTP Proxy**: 如果处于企业内网环境中,可能需要配置代理服务器地址以便于外部资源下载; ```json { "proxies": { "default": { "httpProxy": "http://proxy.example.com:80", "httpsProxy": "https://proxy.example.com:443" } } } ``` - **Registry Mirrors**: 使用国内加速源可以显著提升速度; ```json { "registry-mirrors": ["https://mirror.ccs.tencentyun.com"] } ``` 完成编辑之后记得重启服务使更改生效: ```bash sudo systemctl restart docker ``` #### 测试连通性和性能 为了进一步排查是否为公网链路质量不佳所致,可利用工具如 curl 对目标站点发起探测并观察返回结果: ```bash curl -v https://index.docker.io/v1/ ``` 该操作能够帮助确认是否存在 DNS 解析失败或是 TCP 握手过程异常等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值