代理Docker容器,用于传出连接

我的应用程序需要访问webservice需要白名单IP。 由于我有很多应用程序服务器,我最终得到了这样的解决scheme

iptables -t nat -A OUTPUT -p tcp --dport 80 -d a.webservices.dimension.abc.com -j DNAT --to 52.1.2.3:9999

这意味着到a.webservices.dimension.abc.com的传出连接将被路由到52.1.2.3:9999(代理服务器)

有用。

现在我将我的应用程序服务器(ec2 vm)移动到Docker容器。 但是这样做,iptables规则不起作用。

这是最后的规则

Chain PREROUTING (policy ACCEPT) target prot opt source destination DOCKER all -- anywhere anywhere ADDRTYPE match dst-type LOCALChain INPUT (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination DOCKER all -- anywhere !127.0.0.0/8 ADDRTYPE match dst-type LOCAL DNAT tcp -- anywhere 1.2.3.4 tcp dpt:http to:52.1.2.3:9999 Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- ip-172-17-0-0.us-west-2.compute.internal/16 anywhere MASQUERADE tcp -- ip-172-17-0-2.us-west-2.compute.internal ip-172-17-0-2.us-west-2.compute.internal tcp dpt:http Chain DOCKER (2 references) target prot opt source destination LOG all -- anywhere anywhere limit: avg 2/min burst 5 LOG level warning prefix "DOCKER CHAIN " RETURN all -- anywhere anywhere DNAT tcp -- anywhere anywhere tcp dpt:9090 to:172.17.0.2:80

因此,下面的命令d

docker exec -it some-nginx curl -iL 1.2.3.4不起作用

curl -iL 1.2.3.4作品

我不知道为什么。 从Docker容器代理传出连接应该是常见的,但我还没有find任何现有的解决scheme。

请帮忙。

更新以下链接https://serverfault.com/questions/734526/whitelisting-outgoing-traffic-from-docker-containers给我一些灯。

使用这个规则使它工作

iptables -t nat -A PREROUTING -i docker0 -p tcp a.webservices.dimension.abc.com -j DNAT --to 52.1.2.3:9999