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

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

问题描述

从 Docker 1.10(和 libnetwork 更新)开始,我们可以手动为用户定义网络内的容器提供 IP,这很酷!

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!

我想在我的 LAN 中为容器提供一个 IP 地址(就像我们可以在桥接"模式下使用虚拟机所做的那样).我的局域网是 192.168.1.0/24,我所有的电脑里面都有 IP 地址.而且我希望我的容器具有此范围内的 IP,以便从我 LAN 中的任何地方访问它们(无需 NAT/PAT/等...).

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...).

我显然阅读了 Jessie Frazelle 的博文,还有很多其他人在这里发帖到处都是:

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

还有很多,但什么都没有;我的容器仍然在我的 docker 主机内部"有 IP 地址,并且我的 LAN 上的其他计算机无法访问.

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.

阅读 Jessie Frazelle 的博文,我想(因为她使用公共 IP)我们可以做我想做的事?

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

docker 主机上的新接口 (br-[a-z0-9]+) 采用--gateway"IP,这是我的路由器 IP.并且网络上两台计算机的IP相同...... BOOM

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

提前致谢.

推荐答案

EDIT : 这个解决方案现在没用了.从 1.12 版本开始,Docker 提供了两个网络驱动程序:macvlan 和 ipvlan.它们允许从 LAN 网络分配静态 IP.请参阅下面的答案.

EDIT : This solution is now useless. Since version 1.12, Docker provides two network drivers : macvlan and ipvlan. They allow assigning static IP from the LAN network. See the answer below.

在寻找遇到同样问题的人后,我们找到了一个解决方法:

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

  • (V)LAN 是 192.168.1.0/24
  • 默认网关(= 路由器)是 192.168.1.1
  • 多个 Docker 主机
  • 注意:我们有两个网卡:eth0 和 eth1(专用于 Docker)

我们希望在 192.168.1.0/24 网络(如计算机)中拥有带有 ip 的容器,而无需任何 NAT/PAT/translation/port-forwarding/etc...

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...

这样做时:

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

我们能够为容器提供我们想要的 IP,但是由 docker 创建的网桥 (br-[a-z0-9]+) 将具有IP 192.168.1.1,这是我们的路由器.

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.

使用 DefaultGatewayIPv4 参数:

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

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

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

我们可以通过在前面的命令中添加-o com.docker.network.bridge.name=br-home-net来指定网桥名称.

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

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

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.删除网桥IP

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

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

IP 192.168.1.200 可以用作多个docker主机上的网桥,因为我们不使用它,我们将其删除.

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天全站免登陆