Docker 1.10容器在局域网中的IP [英] Docker 1.10 container's IP in LAN

查看:162
本文介绍了Docker 1.10容器在局域网中的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Docker 1.10(和libnetwork更新),我们可以手动给用户定义的网络中的容器提供一个IP,这很酷!



我想给一个容器在我的局域网中的一个IP地址(就像我们可以用桥模式下的虚拟机一样)。我的局域网是192.168.1.0/24,我所有的电脑都有IP地址。我希望我的容器在这个范围内有IP,以便从我的LAN中的任何地方(没有NAT / PAT / etc ...)到达他们。



我显然阅读Jessie Frazelle的博客文章,还有很多其他人在此发布,如下所示:





等等,但没有出来;我的容器仍然在我的Docker主机里面有IP地址,并且我的局域网上的其他计算机无法访问。



阅读Jessie Frazelle的博文,我以为使用公共IP)我们可以做我想做的事情。



编辑:的确,如果我做的事情就像:

  network create --subnet 192.168.1.0/24 --gateway 192.168.1.1 homenet 
docker run --rm -it --net homenet --ip 192.168.1.100 nginx

docker主机上的新界面(br- [a- z0-9] +)采取'--gateway'IP,这是我的路由器IP。和网络上的两台电脑上相同的IP ... BOOM



提前感谢

解决方案

在寻找有同样问题的人后,我们解决方法:



总结:




  • (V)LAN是192.168.1.0/24

  • 默认网关(=路由器)为192.168.1.1

  • 多个Docker主机

  • 注意:我们有两个NIC:eth0和eth1(专门用于Docker)



我们想要什么:



我们希望在192.168.1.0/24网络(如计算机)中拥有ip,而不需要任何NAT / PAT /转换/端口转发等等。



问题



这样做:

 code> network create --subnet 192.168.1.0/24 --gateway 192.168.1.1 homenet 

我们可以给容器我们w的IP ant,由docker创建的桥( br- [a-z0-9] + )将具有IP 192.168.1.1,其中是我们的路由器。



解决方案



1。设置Docker网络



使用 DefaultGatewayIPv4 参数:

  docker network create --subnet 192.168.1.0/24 --aux-addressDefaultGatewayIPv4 = 192.168.1.1homenet 

默认情况下,Docker将提供桥接口( br- [a-z0-9] + )第一个IP,可能已被另一台机器占用。解决方案是使用 - gateway 参数来告诉docker分配任意IP(可用):

  docker network create --subnet 192.168.1.0/24 --aux-addressDefaultGatewayIPv4 = 192.168.1.1--gateway = 192.168.1.200 homenet 

我们可以通过添加 -o com.docker.network.bridge.name = br-home来指定桥名-net 到上一个命令。



2。桥梁!



现在我们有一个桥( br- [a-z0-9] + )创建由Docker。我们需要将它连接到物理接口(在我的情况下,我必须使用NIC,所以我使用eth1):

  brctl addif br-home-net eth1 



3。删除桥IP



现在我们可以从桥中删除IP地址,因为我们不需要这个IP地址:

  ip a del 192.168.1.200/24 dev br-home-net 
$ / pre>

IP 192.168.1.200 可以用作多个Docker主机上的桥接器,因为我们不使用它,我们将其删除。


Since Docker 1.10 (and libnetwork update) we can manually give an IP to a container inside a user-defined network, and that's cool!

I want to give a container an IP address in my LAN (like we can do with Virtual Machines in "bridge" mode). My LAN is 192.168.1.0/24, all my computers have IP addresses inside it. And I want my containers having IPs in this range, in order to reach them from anywhere in my LAN (without NAT/PAT/etc...).

I obviously read Jessie Frazelle's blog post and a lot of others post here and everywhere like :

and so much more, but nothing came out; my containers still have IP addresses "inside" my docker host, and are not reachable for others computers on my LAN.

Reading Jessie Frazelle's blog post, I thought (since she uses public IP) we can do what I want to do?

Edit: Indeed, if I do something like :

network create --subnet 192.168.1.0/24 --gateway 192.168.1.1 homenet
docker run --rm -it --net homenet --ip 192.168.1.100 nginx

The new interface on the docker host (br-[a-z0-9]+) take the '--gateway' IP, which is my router IP. And the same IP on two computers on the network... BOOM

Thanks in advance.

解决方案

After looking for people who have the same problem, we went to a workaround :

Sum up :

  • (V)LAN is 192.168.1.0/24
  • Default Gateway (= router) is 192.168.1.1
  • Multiple Docker Hosts
  • Note : We have two NIC : eth0 and eth1 (which is dedicated to Docker)

What do we want :

We want to have containers with ip in the 192.168.1.0/24 network (like computers) without any NAT/PAT/translation/port-forwarding/etc...

Problem

When doing this :

network create --subnet 192.168.1.0/24 --gateway 192.168.1.1 homenet

we are able to give containers the IP we want to, but the bridge created by docker (br-[a-z0-9]+) will have the IP 192.168.1.1, which is our router.

Solution

1. Setup the Docker Network

Use the DefaultGatewayIPv4 parameter :

docker network create --subnet 192.168.1.0/24 --aux-address "DefaultGatewayIPv4=192.168.1.1" homenet

By default, Docker will give to the bridge interface (br-[a-z0-9]+) the first IP, which might be already taken by another machine. The solution is to use the --gateway parameter to tell docker to assign a arbitrary IP (which is available) :

docker network create --subnet 192.168.1.0/24 --aux-address "DefaultGatewayIPv4=192.168.1.1" --gateway=192.168.1.200 homenet

We can specify the bridge name by adding -o com.docker.network.bridge.name=br-home-net to the previous command.

2. Bridge the bridge !

Now we have a bridge (br-[a-z0-9]+) created by Docker. We need to bridge it to a physical interface (in my case I have to NIC, so I'm using eth1 for that):

brctl addif br-home-net eth1

3. Delete the bridge IP

We can now delete the IP address from the bridge, since we don't need one :

ip a del 192.168.1.200/24 dev br-home-net

The IP 192.168.1.200 can be used as bridge on multiple docker host, since we don't use it, and we remove it.

这篇关于Docker 1.10容器在局域网中的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆