Use a specific outbound IP with Docker
I have 3 network interfaces: docker0
, eth0
and eth0:0
(alias).
auto eth0
iface eth0 inet static
address 1.2.3.4
netmask 255.255.255.0
network 1.2.3.0
broadcast 1.2.3.255
gateway 1.2.3.254
auto eth0:0
iface eth0:0 inet static
address 5.6.7.8
netmask 255.255.255.255
broadcast 5.6.7.8
docker0
is the bridge auto created by docker.
What's the easiest way to make Docker and all its containers use the outbound IP 5.6.7.8 instead of 1.2.3.4?
Solution 1:
It appears there is no easy way to do it with docker - at the moment at least.
What I ended up doing is simply using SNAT:
iptables -t nat -I POSTROUTING -p all -s 172.17.0.0/16 -j SNAT --to-source 5.6.7.8
172.17.0.0/16
is the subnet used by docker0
.
Solution 2:
Use the --ip command line parameter.
# echo 'DOCKER_OPTS="$DOCKER_OPTS --ip=5.6.7.8"' >> /etc/default/docker
# docker help
...
--ip=0.0.0.0 Default IP address to use when binding container ports