Docker 提高镜像拉取速度

原文地址: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 提高镜像拉取速度)