I create docker containers with a node.js app (depends on dockerode API from apocas) with multiple docker networks connected to each container. By default every container gets a random ip address for each network interface.
Example: dev-container with ip: 172.18.0.2 and connected to network demo-net on interface eth4
When I do a docker inspect dev-container
i can see that network demo-net is connected to the container dev-container with ip address 172.18.0.2 (as expected).
Now I execute the dev-container with docker exec -it dev-container /bin/bash
and configure the ip address with ifconfig eth4 192.168.99.169 netmask 255.255.240.0
.
Result: ifconfig
shows that the new address 192.168.99.169 is connected to interface eth4. But when I do a docker inspect dev-container
again, there is still the old address 172.18.0.2 connected with network demo-net (not expected).
Is there a way that docker can update the internal network settings so that docker inspect will show the updated ip settings or is there a better way to change a containers ip address?
(Docker version is 19.03.12)
Docker will not notice if you try to make manual changes to your container's network configuration. You can docker rm
your container and docker run
it again to bring things back into a consistent state.
By way of analogy, you might try logging into your home WiFi router. It might say your laptop is 192.168.1.2 and your printer is 192.168.1.3. You can certainly run ifconfig
to change your laptop's IP address to 192.168.1.3/24, or to 10.20.30.40/16; but if you do your router won't have any idea this is going on, IP routing won't work correctly if you try to call out (or if you've set up a reverse NAT rule to allow inbound calls back in), and you can get strange network conflicts if you try to print.
Docker completely manages containers' network interfaces. There's no reason to run ifconfig
or ip
in a container. The container-private IP address is really an implementation detail, and it's unusable in many common circumstances; I would also avoid looking this up. You similarly shouldn't need to manually set it, but if you do, the "create a container" Docker API (basically directly exposed by dockerode) lets you specify the NetworkingConfig
.