加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 03:49:47 所属栏目:安全 来源:网络整理
导读:我希望在VM主机外部的两个容器可以在它们各自的特定IP地址(192.168.0.222,192.168.0.227)上使用,而无需端口映射.这意味着我希望通过使用其IP来直接访问容器上的任何端口.我已经在VM主机外部的网络中运行的机器在192.168.0.1-192.168.0.221范围内. 现在可以使

我希望在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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读