为docker设置代理

当我们在终端输入docker pull xxx时,docker服务会去DockerHub拉取镜像

但因为国家政策的相关原因,DockerHub是被禁止访问的

因此你大概会得到以下报错

1
2
3
Error response from daemon: Get "https://registry-1.docker.io/v2/": 
net/http: request canceled while waiting for connection
(Client.Timeout exceeded while awaiting headers)

 

我们如果想要拉取镜像就需要为docker配置代理

但是docker是C/S架构的,你直接

1
https_proxy="xxxx" docker pull xxx

是无法成功的,这只是为客户端设置了代理,并没有为服务器设置代理

 

正确的做法如下

前往docker的配置目录,ubuntu下是/etc/docker

新建一个daemon.json文件,里面填写你的代理地址

1
2
3
4
5
6
7
{
"proxies": {
"http-proxy": "http://proxy.example.com:3128",
"https-proxy": "https://proxy.example.com:3129",
"no-proxy": "*.test.example.com,.example.org,127.0.0.0/8"
}
}

然后重启docker服务

1
sudo systemctl restart docker

此时再docker pull xxx就能成功了

 

更多关于docker代理的资料,请参阅官方文档

Daemon proxy configuration

Use a proxy server with the Docker CLI


为docker设置代理
https://www.pasiphae.top/2023/12/24/为docker设置代理/
作者
pasiphae
发布于
2023年12月24日
许可协议