How to assign static public IP to docker container

后端 未结 3 1654
醉酒成梦
醉酒成梦 2020-12-12 21:03

Is there any way to assign the static public IP to the container. So the container has the public IP. Client can access to container with the IP.

3条回答
  •  情歌与酒
    2020-12-12 21:47

    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).

提交回复
热议问题