Is there any way to assign a static public IP to the container? So the container has the public IP. Clients can then access the container with that public static IP.
3 Answers
This should now be possible with docker 1.10 and the new docker run --ip
option that you now see in docker network connect
.
If specified, the container's IP address(es) is reapplied when a stopped container is restarted. If the IP address is no longer available, the container fails to start.
One way to guarantee that the IP address is available is to specify an
--ip-range
when creating the network, and choose the static IP address(es) from outside that range. This ensures that the IP address is not given to another container while this container is not on the network.
$ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network
$ docker network connect --ip 172.20.128.2 multi-host-network container2
See also Jessie Frazelle's blog post "IPs for all the Things", and pull request docker/docker#19001.
-
1i got that you provided some ip 172.20.128.2 to the container.But how can i access the website running in this container at ip 172.20.128.2 .I do not want to do port forwarding and using host ip. Oct 15, 2016 at 2:24
-
@thinkingmonster good question: ask it as its own question for me or other to propose alternatives.– VonCOct 15, 2016 at 5:38
-
i just asked the question .Please enlighten me. stackoverflow.com/questions/40060764/… Oct 15, 2016 at 15:17
-
@VonC I have a VPS and bought additional IPs to attach. Therefore, I tried to generate a new network for each ip (they are not from the same subnet) and used your commands, but without success. Do we have to setup the IP as alias or bridge?– NaNFeb 16, 2017 at 16:51
-
@NaN Not sure: that would be best asked as an independent question for all to see.– VonCFeb 16, 2017 at 16:53
With currently released versions of Docker this isn't possible (without a lot of manual work behind Docker's back), although it is seldom necessary.
Docker exposes network services in containers through the use of port mappings, and port mappings can bind to specific ip addresses on your host. So if you want to have one web server at 192.168.10.10
and another webserver at 192.168.10.20
, first make sure this addresses are available on your host:
ip addr add 192.168.10.10/24 dev eth0
ip addr add 192.168.10.20/24 dev eth0
Then start the first container:
docker run -p 192.168.10.10:80:80 mywebserver
And finally start the second container:
docker run -p 192.168.10.20:80:80 mywebserver
In the above commands, the -p
option is used to bind the port mapping to a particular ip address. Now you have two containers offering a service on the same port (port 80) but on different ip addresses.
-
It is said I need to assign the static public IP to the host first. Then use
-p
to bind the ip to container.– firelyuJan 9, 2016 at 5:18 -
In the solution suggested by larsks can we also connect the same docker containers to a docker network as created in the solution by @VonC ?– AmitabhDec 9, 2016 at 7:58
-
1What happens when the docker host also has a server bound to 0.0.0.0:80?– Daniel FNov 29, 2017 at 17:52
-
Then you will probably need to modify your server configuration to bind to specific addresses instead.– larsksNov 29, 2017 at 18:05
Since this question pops up on popular searches (docker assign ip container etc), the (currently) accepted answer is obsolete, and the correct one of @VonC is somewhat inconclusive (including discussion) let us summarize with an example of how it can be done and what the result is:
docker run -d nginx:latest #--> container with id be46...
docker network create --subnet 10.30.0.0/24 --ip-range 10.30.0.0/24 multi-host-network
docker network connect --ip 10.30.0.4 multi-host-network be46
now the container has 10.30.0.4/24
attached; you can ping 10.30.0.4
from the host on which the commands were run. After docker stop be46
ping's don't work anymore, and after you docker start be46
pings succeed again. On the host, the following route is created:
10.30.0.0/24 dev br-b74e7b452f23 proto kernel scope link src 10.30.0.1
(so the host assumes 10.30.0.1
).
Note: this completes the task of "assigning given IP to a container", but it is unclear to me for now if you could do that in the "docker swarm" context, and achieve the same level of redundancy we have there (with the assignment of ports to services).