I'm perfectly happy with the IP range that docker is giving me by default 176.17.x.x
, so I don't need to create a new bridge, I just want to give my containers a static address within that range so I can point client browsers to it directly.
I tried using
RUN echo "auto eth0" >> /etc/network/interfaces
RUN echo "iface eth0 inet static" >> /etc/network/interfaces
RUN echo "address 176.17.0.250" >> /etc/network/interfaces
RUN echo "netmask 255.255.0.0" >> /etc/network/interfaces
RUN ifdown eth0
RUN ifup eth0
from a Dockerfile, and it properly populated the interfaces file, but the interface itself didn't change. In fact, running ifup eth0 within the container gets this error:
RTNETLINK answers: Operation not permitted Failed to bring up eth0
I'm using the method written here from the official Docker documentation and I have confirmed it works:
Using this approach I run my containers always with net=none and set IP addresses with an external script.
Docker containers by default do not have sufficient privileges to manipulate the network stack. You can try adding
--cap-add=NET_ADMIN
to the run command to allow this specific capability. Or you can try--privileged=true
(grants all rights) for testing.Another option is to use pipework from the host.
For completeness: there's another method suggested on the Docker forums. (Edit: and mentioned in passing by the answer from Андрей Сердюк).
Add the static IP address on the host system, then publish ports to that ip, e.g.
docker run -p 192.0.2.1:80:80 -d mywebserver
.Of course that syntax won't work for IPv6 and the documentation doesn't mention that...
It sounds wrong to me: the usual wildcard binds (*:80) on the host theoretically conflict with the container. In practice the Docker port takes precedence and doesn't conflict, because of how it's implemented using iptables. But your public container IP will still respond on all the non-conflicting ports, e.g. ssh.
Actually, despite my initial failure, @MarkO'Connor's answer was correct. I created a new interface (docker0) in my host /etc/network/interfaces file, ran sudo ifup docker0 on the host, and then ran
which picked up the static IP and assigned it to docker0 in the container.
Thanks!
You can set up SkyDns with service discovery tool - https://github.com/crosbymichael/skydock
Or: Simply create network interface and publish docker container ports in it like here https://gist.github.com/andreyserdjuk/bd92b5beba2719054dfe