I need a system whereby containers have predictable internal IP addresses for our development environment (most of us are on Macs). To date we’ve used Boot2Docker, but we want to move everything into the native Docker for Mac.
However, we’ve hit an issue that I cannot figure out if we’re doing something wrong, or perhaps there are limitations in what’s possible with Docker for Mac.
Firstly I set up a custom Docker network with a large subnet:
docker network create --driver=bridge --subnet=172.18.0.0/16 -o com.docker.network.bridge.enable_icc=true -o com.docker.network.bridge.enable_ip_masquerade=true -o com.docker.network.bridge.host_binding_ipv4=0.0.0.0 foo.local
I then run a simple container and assign it an IP address in the newly defined network:
docker run --rm -t -i --ip 172.18.10.0 --net=foo.local ubuntu:trusty /bin/netcat -l 4444
From the host, I try and netcat to it and nothing happens:
netcat 172.18.10.0 4444. It is unable to connect using the private IP address on that port.
So then I try exposing a port:
docker run --rm -t -i --ip 172.18.10.0 --expose 4444 --net=foo.local ubuntu:trusty /bin/netcat -l 4444
And try and
telnet to 172.18.10.0 on port 4444 and still nothing.
Then I thought that maybe the OS X host is simply unable to communicate with the new private network, so I’ll try this all using container to container communication.
docker run --rm -t -i --ip 172.18.10.5 --net=foo.local ubuntu:trusty /bin/netcat 172.18.10.0 4444
And voila, one container is able to talk to the other container on that network.
So does this mean the mac OS is unable to communicate directly with containers unless they are exposing a port on the host network? If so, that’s really not great and should be mentioned in the Docker for Mac documentation. I have read that binding ports from containers to more than host network interface is not possible, but I had assumed that restrictions like this would not apply internally.
Do I perhaps need to set up a bridge of some sort to explicitly allow this? An
ifconfig on the host unfortunately only reveals on interface.
If anyone can help advise how one can use a custom network (so that IP addresses can be specified for hosts) I’d greatly appreciate it!