Each time I start the docker container, it gets a different IP address

How to fix static IP for container?

First I run the container and check it, it says

"NetworkSettings": { "IPAddress": "XX.XX.206.98", "IPPrefixLen": 27, "Gateway": "XX.XX.206.105", "Bridge": "public", "PortMapping": null, "Ports": {} }, 

then i stop it and restart it he likes

 "NetworkSettings": { "IPAddress": "XX.XX.206.99", "IPPrefixLen": 27, "Gateway": "XX.XX.206.105", "Bridge": "public", "PortMapping": null, "Ports": {} }, 

As you can see, this has changed. I just created a bridge called public and started adding docker with -b=public . How to set a static IP address for a container?

+8
docker lxc ip
source share
1 answer

FROM DOCKER 1.10 ON

 # create a new bridge network with your subnet and gateway for your ip block $ docker network create --subnet 203.0.113.0/24 --gateway 203.0.113.254 iptastic # run a nginx container with a specific ip in that block $ docker run --rm -it --net iptastic --ip 203.0.113.2 nginx # curl the ip from any other place (assuming this is a public ip block duh) $ curl 203.0.113.2 

UPDATE

Now the only way to get a static IP address is through two scripts: pipework or ovs-docker

There is a strong direction for using Open vSwitch as a "battery-supported" version of multi-level docker containers.

Watch out for socketplane .


This is design behavior.

There is a very interesting discussion for its changes in future releases.

So far, the only way to do this is to return to linux containers:

 docker run \ -n=false \ -lxc-conf="lxc.network.type = veth" \ -lxc-conf="lxc.network.ipv4 = 172.16.42.20/24" \ -lxc-conf="lxc.network.ipv4.gateway = 172.16.42.1" \ -lxc-conf="lxc.network.link = docker0" \ -lxc-conf="lxc.network.name = eth0" \ -lxc-conf="lxc.network.flags = up" \ -i -t my_image:my_tag /bin/bash 

Thus, -n=false disables the automatic -lxc-conf network and all -lxc-conf options should actually define the virtual network according to your needs.

+4
source share

All Articles