05月04, 2026

解决docker代理不生效问题

之前一直没有需求,没用过docker,今天尝试用了一下。

首先,在没有开启代理的时候,报错是这样的:

ERROR: failed to build: failed to solve: xxx/yyy: failed to resolve source metadata for docker.io/xxx/yyy: failed to do request: Head "https://registry-1.docker.io/v2/xxxxx": dial tcp 67.15.100.252:443: connect: connection refused

按常规方式加上代理,新建文件/etc/systemd/system/docker.service.d/proxy.conf,填上代理配置,例如:

[Service]
Environment="HTTP_PROXY=http://192.168.31.2:1080"
Environment="HTTPS_PROXY=http://192.168.31.2:1080"
Environment="NO_PROXY=localhost,127.0.0.1"

然后sudo systemctl daemon-reload && sudo systemctl restart docker

但是发现代理始终不生效,报错:

ERROR: failed to build: failed to solve: failed to fetch anonymous token: Get "https://auth.docker.io/token?scope=repository%3Axxx%3Apull&service=registry.docker.io": dial tcp 128.242.240.117:443: connect: connection refused

尝试半天之后发现,是auth.docker.io这个地址的请求并不走docker的代理配置,所以需要结合export http_proxy=http://192.168.31.2:1080 https_proxy=http://192.168.31.2:1080才行。

附一些常见的docker命令:

docker build -t firekylin .
docker images firekylin
docker run -d -p 8360:8360 firekylin
docker exec -it 2a51518b8efe /bin/sh
docker stop 2a51518b8efe

Comments