Docker Community Forums

Share and learn in the Docker community.

Docker network changes ip after reboot

Hello! I’m having a problem when running networks in my host. I create a network using

docker network create name

After that I added the containers to the network by compose.yml, all works great, i’ve different containers in the networks and all can communicate between them and with the host, but after a docker service reboot or a host machine reboot is when the problem comes. Just after the reboot if i put ifconfig on the host, i obtained the correct config to the network

br-93aeb64e21d1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:a4ff:fe86:bce  prefixlen 64  scopeid 0x20<link>
        ether 02:42:a4:86:0b:ce  txqueuelen 0  (Ethernet)
        RX packets 436  bytes 12208 (11.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1090  bytes 369287 (360.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

But after some seconds if i rerun ifconfig, i obtain this:

br-93aeb64e21d1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 169.254.251.214  netmask 255.255.0.0  broadcast 169.254.255.255
        inet6 fe80::42:a4ff:fe86:bce  prefixlen 64  scopeid 0x20<link>
        ether 02:42:a4:86:0b:ce  txqueuelen 0  (Ethernet)
        RX packets 3630  bytes 160808 (157.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19000  bytes 26628297 (25.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

The ip has changed, and the containers are isolated of the host and it does not have connection to external network.

This is the docker version I have

Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)

I’m running it on a terramaster NAS so I cant update the version.

What can be happening?

Thanks for your help.

Stop and disable dhcpcd daemon on system boot since we going to start it manually with /etc/rc.local

NB: we do so, cause ‘docker’ when building or running a container sets up a ‘bridge’ interface which interferes ‘failover’

systemctl stop dhcpcd
systemctl disable dhcpcd

Start dhcpcd daemon on each interface we are interested in

dhcpcd eth0
dhcpcd eth1
dhcpcd wlan0

Start dhcpcd daemon on every reboot

sed -i -e ‘s/^exit 0$//g’ /etc/rc.local
echo “dhcpcd eth0” >> /etc/rc.local
echo “dhcpcd eth1” >> /etc/rc.local
echo “dhcpcd wlan0” >> /etc/rc.local
echo “” >> /etc/rc.local
echo “exit 0” >> /etc/rc.local

Also DNS servers for docker (probably, not necessary)

cat >> /etc/docker/daemon.json << EOF
{
    "dns": ["8.8.8.8", "8.8.4.4"]
}
EOF
service docker restart

That was! Thanks a lot!

I’d just like to chip in and say that I would very much like to see the ability to specify an IP address on container startup (e.g. docker run -d --ip 10.0.0.123), or any other means of specifying a static IP address (inside the container would be fine also, currently I can do this only in privileged mode).

I understand that philosophically this is slightly out of line with how Docker is designed to work, in that port forwarding to the host is the preferred method of accessing services inside the container. However, there are several scenarios where this is not ideal and a known IP address is desirable:

SSH access into the container - again, I know that this may be a controversial one and one may argue that you shouldn’t, but it’s a common pattern when one wants to inspect the inside of the container. It’s more convenient to SSH to an IP address (which I may have mapped a DNS entry to) than to have dozens of different ports on the host for SSHing to
Firewall rules (e.g. Container X (10.0.0.40) should only have certain types of outbound net access)
Exposing services a different way: Currently, a common scenario is to have a reverse proxy like nginx running on the host or inside a container, forwarding traffic to many Docker containers. I might have webapp A on port 10080, webapp B on port 11080, etc. However, I would prefer to forward to the container’s IP on the ‘proper’ ports, e.g. 10.0.0.40:80, etc, keeping the port numbers meaningful
General comfort for server managers who are used to plain LXC, and want their containers to have predictable, stable IP addresses
I have noticed over the last year that many, many issues have come up where people have wanted to be able to specify a static IP address, and many of them have been closed due to the (supposed) ability of their needs to be satisfied through other features, but I do feel this is a very widely desirable feature.