Docker 网络代理配置
1. 在 /etc/systemd/system
目录下创建 docker.service.d
目录
sudo mkdir -p /etc/systemd/system/docker.service.d
2. 在/etc/systemd/system/docker.service.d
下创建 http-proxy.conf
文件
sudo touch /etc/systemd/system/docker.service.d/http-proxy.conf
3. 编辑http-proxy.conf
文件
vim /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/"
Environment="HTTPS_PROXY=http://proxy.example.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
# 【注意】HTTP_PROXY 用于代理访问 http 请求,HTTPS_PROXY 用于代理访问 https 请求,如果想某个 IP或域名不走代理则配置到 NO_PROXY中。
4. 刷新更改并重启Docker
sudo systemctl daemon-reload
sudo systemctl restart docker
Docker 网络代理配置后,关于Harbor无法登录的坑
1.问题:
部署好harbor,并且也安装好docker,按照上面的步骤配置好网络代理。但是最后出现的现象,本地和其他人都能使用
docker login <ip>
的方式登录到harbor,但唯独harbor服务所在的本机无法登录。
[root@ps-cnrjkflc-01 ~]# docker login xx.x.xx.x
Username: *****
Password:
Error response from daemon: Get "http://xx.x.xx.x/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
2.排查
唯一的区别就是harbor服务所在的本机安装的docker配置了网络代理,通过
docker info
命令排查,得到下面的信息
HTTP Proxy: http://xx.x.xx.xxx:xxxx/
HTTPS Proxy: http://xx.x.xx.xxx:xxxx/
No Proxy: localhost,127.0.0.1
心里想着也配置了
No Proxy
,难道localhost,127.0.0.1
无法等效本机Ipxx.x.xx.xxx
?
3.解决
抱着试一试的态度,在
/etc/systemd/system/docker.service.d/http-proxy.conf
文件里,修改NO_PROXY
,追加了本机IP,然后测试居然可以登录了~
[root@ps-cnrjkflc-01 harbor]# cd /etc/systemd/system/docker.service.d
[root@ps-cnrjkflc-01 docker.service.d]# cat http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://xx.x.xx.xxx:xxxx/" "HTTPS_PROXY=http://xx.x.xx.xxx:xxxx/" "NO_PROXY=localhost,127.0.0.1,xx.x.xx.x"
[root@ps-cnrjkflc-01 harbor]# docker login xx.x.xx.x
Username: ******
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded