Docker网络代理配置 & 可能埋下的坑

本文详细描述了如何在Docker中配置HTTP和HTTPS代理,以及解决Harbor登录问题的过程。问题在于Harbor服务器本身由于代理设置导致无法登录,通过添加自身IP到NO_PROXY中解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
配置Docker网络代理,你可以参考Docker官方文档中的配置方法。首先,你需要确定要使用的代理服务器的地址和端口。如果你使用的是本地代理,比如localhost:3128,你需要在docker命令中添加--network host参数。这样Docker容器将能够正常使用本地代理。如果你使用的是外部IP地址的代理服务器,并且代理本身需要开启Gateway模式,你需要在Docker守护进程的配置文件中添加代理设置。具体的配置方法可以参考Linux下安装配置Cntlm代理的教程。完成配置后,你需要重启Docker守护进程使配置生效。你可以使用以下命令来重启Docker守护进程并查看配置是否正确: sudo systemctl daemon-reload sudo systemctl restart docker systemctl show --property=Environment docker /etc/systemd/system/docker.service.d 这样你就成功配置Docker网络代理。请根据你的实际情况进行相应的配置&lt;span class=&quot;em&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;em&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;em&quot;&gt;3&lt;/span&gt; #### 引用[.reference_title] - *1* [快速设置 Docker 的三种网络代理配置](https://blog.csdn.net/peng2hui1314/article/details/124267333)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] - *2* *3* [Docker 配置网络代理](https://blog.csdn.net/qq_39698985/article/details/123748820)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值