解决docker使用socks5代理设置
之前的文章写过 linux 使用 v2ray 进行代理上网,但是开启代理后用 docker 拉取镜像时还是会莫名其妙的出现以下连接超时的问题:
"https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
用代理 curl 一下报错的网址发现代理是正常连接的但是返回的是 401 未注册,应该是 docker 未能正常使用代理:
* Trying 127.0.0.1:10808...
* Connected to 127.0.0.1 (127.0.0.1) port 10808
* Host registry-1.docker.io:443 was resolved.
* IPv6: 2a03:2880:f10c:83:face:b00c:0:25de
* IPv4: 202.160.128.238
* SOCKS5 connect to 202.160.128.238:443 (locally resolved)
* SOCKS5 request granted.
* Connected to 127.0.0.1 (127.0.0.1) port 10808
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* CAfile: /home/opt/ssl/cacert.pem
* CApath: none
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 / X25519 / RSASSA-PSS
* ALPN: server did not agree on a protocol. Uses default.
* Server certificate:
* subject: CN=*.docker.com
* start date: Mar 5 00:00:00 2025 GMT
* expire date: Apr 3 23:59:59 2026 GMT
* subjectAltName: host "registry-1.docker.io" matched cert's "*.docker.io"
* issuer: C=US; O=Amazon; CN=Amazon RSA 2048 M03
* SSL certificate verify ok.
* using HTTP/1.x
> GET /v2/ HTTP/1.1
> Host: registry-1.docker.io
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 401 Unauthorized
< content-type: application/json
< docker-distribution-api-version: registry/2.0
< www-authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io"
< date: Wed, 23 Jul 2025 07:40:05 GMT
< content-length: 87
< strict-transport-security: max-age=31536000
<
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}
* Connection #0 to host 127.0.0.1 left intact
解决办法如下,修改 docker 的守护进程文件 /etc/docker/daemon.json,添加以下代理内容然后 systemctl restart docker 即可。
{
"proxies": {
"http-proxy": "socks5://127.0.0.1:7890",
"no-proxy": "localhost,127.0.0.0/8",
"https-proxy": "socks5://127.0.0.1:7890"
}
}
