docker pull拉取超時的解決方案

最近docker pull鏡像的時候非常不穩定,速度慢不說還經常超時失敗

Pulling xlxdsj_swagger_ui (swaggerapi/swagger-ui:)...
latest: Pulling from swaggerapi/swagger-ui
ba3557a56b15: Pull complete
1a18b9f93d41: Pull complete
38ceab6c6432: Pull complete
6104f3bd82cc: Pull complete
750e0e12d70c: Pull complete
d7c38a871210: Pull complete
cbd9bbb7c3b7: Pull complete
3d6723371070: Pull complete
5e5b2de4487f: Pull complete
3c84c9c2d567: Pull complete
9e7d57b00bb8: Pull complete
5356d2f5be08: Pull complete
Digest: sha256:2a746f15dacc2f2e381b3449544b82e6c7604567f05606d957ff5b7cf7d29a47
Status: Downloaded newer image for swaggerapi/swagger-ui:latest
Pulling xlxdsj_swagger_editor (swaggerapi/swagger-editor:)...
ERROR: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

大致的解決辦法有三個

1、修改DNS

2、修改鏡像倉庫地址

3、修改hosts

使用socks5代理

以下操作均在CentOS7,Ubuntu與之基本一致,macOS以及windos原理一致

修改DNS

>>> vim /etc/resolv.conf
nameserver 8.8.8.8

修改鏡像倉庫地址

>>> vim /etc/docker/daemon.json
{
 "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn", "https://reg-mirror.qiniu.com"]
}
>>> systemctl daemon-reload
>>> systemctl restart docker

修改host

# 安裝dig
>>> sudo yum install bind-utils
>>> dig @114.114.114.114 registry-1.docker.io
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.3 <<>> @114.114.114.114 registry-1.docker.io
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18733
;; flags: qr rd ra; QUERY: 1, ANSWER: 8, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;registry-1.docker.io. IN A
;; ANSWER SECTION:
registry-1.docker.io. 38 IN A 54.85.56.253
registry-1.docker.io. 38 IN A 35.174.73.84
registry-1.docker.io. 38 IN A 23.22.155.84
registry-1.docker.io. 38 IN A 18.213.137.78
registry-1.docker.io. 38 IN A 107.23.149.57
registry-1.docker.io. 38 IN A 52.5.11.128
registry-1.docker.io. 38 IN A 52.54.232.21
registry-1.docker.io. 38 IN A 52.20.56.50
;; Query time: 8 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: Tue Feb 23 09:03:18 CST 2021
;; MSG SIZE rcvd: 177
>>> sudo vim /etc/hosts
35.174.73.84 registry-1.docker.io

使用socks5代理

如果之前的方法效果都不太好再考慮使用這個

前提是已經裝好ss

>>> vim /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=socks5://127.0.0.1:1080"
>>> systemctl daemon-reload
>>> systemctl restart docker
>>> systemctl show --property=Environment docker
Environment=HTTP_PROXY=socks5://127.0.0.1:1080

補充:解決docker pull鏡像報錯或者超時的方案,將拉取地址改為國內鏡像倉庫

docker默認鏡像拉取地址為國外倉庫下載速度較慢,甚至會報這種錯

Error response from daemon: Get https://docker.elastic.co/v2/: net/http: TLS handshake timeout

解決方法修改 /etc/docker/daemon.json 文件並添加上 registry-mirrors 鍵值。

然後重啟docker就可以瞭

{
 "registry-mirrors": ["https://registry.docker-cn.com"]
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: