How to assign static public IP to docker container

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

    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.

提交回复
热议问题