Skip to content

docker分配独立公网ip

问题

多个相同服务的容器调用第三方的服务,该第三方服务对调用者的ip进行了限制,业务上需要对这些服务设置不同出口ip才行。而这些容器所在的服务器拥有多个公网ip,可以给不同的服务容器分配单独的公网ip。

思路

docker的默认网络通信方式是桥接的docker0,而docker0通过修改iptable将请求转发出去

处理步骤

添加新的docker network

docker network create --subnet=172.18.0.0/24 --opt "com.docker.network.bridge.name"="docker1" docker1

添加当前服务器可用的公网IP

iptables -t nat -I POSTROUTING -p all -s 172.18.0.100 -j SNAT --to-source  你的公网ip

测试下

docker run --rm --network docker1 --ip=172.18.0.100 byrnedo/alpine-curl http://www.myip.ch

Published in自建服务

One Comment

  1. 廖

    谢谢,查了半天资料没查不来。你的办法非常好!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *