Docker_Static_IP_To_Container
Why static IP to the container?
Each time when we restart container, IP address changed. Infect if we restart container in different sequences, its changed so to resolve this confliction we set static IP to the container.
For the testing here i am going to create two containers with the name of …
-
test11
-
test12
Creating a container with "centos" image and pass below parameter.
[root@feenixdv conf]# docker run -itd --privileged --name test11 -p 81:80 centos
1ea63b664f6f12c564fe72d9378a1b3bd1fad1115c897e69959394fca53836ce
[root@feenixdv conf]# docker run -itd --privileged --name test12 -p 82:80 centos
77cd950fcae3b204e26938453cb718eea8840cca5be96979bf99b2a3fdfdd811
[root@feenixdv conf]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77cd950fcae3 centos "/bin/bash" 14 seconds ago Up 14 seconds 0.0.0.0:82->80/tcp test12
1ea63b664f6f centos "/bin/bash" 27 seconds ago Up 24 seconds 0.0.0.0:81->80/tcp test11
Now check the IP address of both containers.
[root@feenixdv conf]# docker inspect test11 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
[root@feenixdv conf]# docker inspect test12 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.4",
"IPAddress": "172.17.0.4",
Now going to stop and start the container. But I am not starting the container in the same sequence. First start "test12" then "test11".
[root@feenixdv conf]# docker stop test11 test12
test11
test12
[root@feenixdv conf]# docker start test12 test11
test12
test11
[root@feenixdv conf]# docker inspect test11 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.4",
"IPAddress": "172.17.0.4",
[root@feenixdv conf]# docker inspect test12 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
Here we can see IP sequence changes. test11 IP shifted into test12.
To resolve this, I am going to create my own bridge network with the name of "feenix_network", with below information.
[root@feenixdv conf]# docker network create --driver bridge --subnet 172.19.0.0/16 --gateway 172.19.0.1 feenix_network
2eff5c11b84832f422d2f0ea2996496ceb5396ae5cf8a67d129d061e4efa6c8a
[root@feenixdv conf]# docker network ls
NETWORK ID NAME DRIVER SCOPE
38b84019872b bridge bridge local
2eff5c11b848 feenix_network bridge local
c1c37ffd2bc3 host host local
855f2c69ad58 none null local
Now assign IP to both container.
[root@feenixdv conf]# docker network connect --ip 172.19.0.2 feenix_network test11
[root@feenixdv conf]# docker network connect --ip 172.19.0.3 feenix_network test12
[root@feenixdv conf]# docker inspect test11 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.4",
"IPAddress": "172.17.0.4",
"IPAddress": "172.19.0.2",
Here we can see, two IP address assign to test11 and test12.
Release old IP with below command.
[root@feenixdv conf]# docker network disconnect bridge test11
[root@feenixdv conf]# docker network disconnect bridge test12
[root@feenixdv conf]# docker inspect test11 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.19.0.2",
[root@feenixdv conf]# docker inspect test12 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.19.0.3",
Here we can see everything is ok. All container have new IP.
Now going to test with restart container in different sequences.
[root@feenixdv conf]# docker stop test11 test12
test11
test12
[root@feenixdv conf]# docker start test12 test11
test12
test11
[root@feenixdv conf]# docker inspect test11 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.19.0.2",
[root@feenixdv conf]# docker inspect test12 |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.19.0.3",
Done.