docker – 如何将特定IP分配给容器并使其可以在VM主机外部访问?

前端之家收集整理的这篇文章主要介绍了docker – 如何将特定IP分配给容器并使其可以在VM主机外部访问?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我希望在VM主机外部的两个容器可以在它们各自的特定IP地址(192.168.0.222,192.168.0.227)上使用,而无需端口映射.这意味着我希望通过使用其IP来直接访问容器上的任何端口.我已经在VM主机外部的网络中运行的机器在192.168.0.1-192.168.0.221范围内.

现在可以使用Docker 1.10.0,如果是这样,怎么样?

我在OS X 10.11上使用docker版本1.10.0,构建590d5108和docker-machine版本0.6.0,使用boot2docker / VirtualBox驱动程序构建e27fb87.

我一直试图弄清楚这一点,没有运气,我已经阅读了以下问题和答案:

> How to assign static public IP to docker container
> How to expose docker container’s ip and port to outside docker host without port mapping?
> How can I make other machines on my network access my Docker containers (using port mapping)?

最佳答案
根据Jessie Frazelle,这应该是可能的.
见“IPs for all the Things

This is so cool I can hardly stand it.

In Docker 1.10,the awesome libnetwork team added the ability to specifiy a specific IP for a container. If you want to see the pull request it’s here: 07002.

# create a new bridge network with your subnet and gateway for your ip block
$docker network create --subnet 203.0.113.0/24 --gateway 203.0.113.254 iptastic

# run a Nginx container with a specific ip in that block
$docker run --rm -it --net iptastic --ip 203.0.113.2 Nginx

# curl the ip from any other place (assuming this is a public ip block duh)
$curl 203.0.113.2

# BOOM golden

这确实说明了您现在在docker network connect中看到的new docker run --ip option.

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

像往常一样,“可以访问”的部分将涉及port forwarding.

猜你在找的Docker相关文章