一般代理监听在127.0.0.1端口,容器内通过127.0.0.1无法正确访问到代理服务,本文解决如何配置容器通过代理上网问题
Mac
Privoxy 是 mac 的代理软件,可以将http(s)请求转发到 Sock5,在Docker for mac里配置Http代理就可以实现容器代理上网。
安装
brew install privoxy
配置
在/usr/local/etc/privoxy/config中添加:
listen-address 0.0.0.0:1082
forward-socks5 / 127.0.0.1:1081 .
- listen-address 表示
privoxy 监听的端口
- forward-socks5 把所有匹配
/的请求,以sock5协议转发到127.0.0.1:1081,. 表示不转发到http代理
启动服务
brew services start privoxy
配置docker代理
步骤:
Docker for Mac -> Preferences -> Resources -> Proxies配置http和https代理为 http://<ip>:1082 -> 重启Docker服务
启动容器验证
在容器中执行如下命令,看到返回代理的地址即表示配置成功:
curl ifconfig.me
Linux
vim /usr/lib/systemd/system/docker.service
[Service]
Environment="HTTP_PROXY=http://192.168.179.1:8001"
Environment="HTTPS_PROXY=http://192.168.179.1:8001"
systemctl daemon-reload
systemctl restart docker.service
或
cat ~/.docker/config.json
{
"proxies":
{
"default":
{
"httpProxy": "192.168.179.1:8001",
"httpsProxy": "192.168.179.1:8001",
"noProxy": "localhost,127.0.0.1,172.16.0.0/16"
}
}
}