How to assign static public IP to docker container

后端 未结 3 1652
醉酒成梦
醉酒成梦 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:34

    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.

提交回复
热议问题