如何从Docker容器连接到主机网络? [英] How do I connect to to hosts network from within a docker container?

查看:369
本文介绍了如何从Docker容器连接到主机网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在局域网中有两个物理服务器相互连接。服务器A使用ubuntu容器运行docker。服务器B运行一个MySQL数据库(没有任何docker东西)。

I have two physical servers connected to each other in a LAN. Server A runs docker with a ubuntu container. Server B runs a MySQL-database (without any docker stuff).

现在,我需要从服务器A上的docker-container内部访问服务器B上的MySQL数据库。

Now I need to access the MySQL-Database on Server B from within a docker-container on Server A.

服务器B只有一个ipv6-地址。

Server B only has an ipv6-address.

当我在主机A上运行 ping6< ipv6_server_b> 时有用。来自主机A的docker-container内部的同一命令为我提供了未知主机

When I run ping6 <ipv6_server_b> on Host A it works. The same command from within the docker-container on Host A gives me a unknown host.

我想我需要创建到服务器B的ipv6-子网的docker bridge。我的所有努力都导致了相同的未知主机错误。而且我不想使用-network = host

I think I need to create a docker bridge to the ipv6-subnet of Server B. All my efforts led to the same unknown host error. And I don't want to use --network=host.

公共Internet连接在主机界面上服务器B在接口 ens17

The public internet is connected on hosts interface ens16 while Server B is in a local security zone connected on interface ens17

我在docker选项中启用了ipv6,如下所示:

I enabled ipv6 in docker options like this:

DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 --ipv6 --fixed-cidr-v6=2001:db8:1::/64 "

1。我试图创建到安全区域的ipv6子网的桥接网络

docker network create --driver bridge --ipv6 --subnet=fcfc:0:0:1::/64 db-link

2。这是我用来启动容器并分配网络的命令

docker run -it --network = db-link nicolaka / netshoot

(nicolaka / netshoot包含很多有用的诊断内容)

(nicolaka/netshoot contains a lot of useful diagnostic stuff)

3。尝试从容器中ping服务器B地址:

ping6 fcfc::1:24fc:25ff:fe25:c903
PING fcfc::1:24fc:25ff:fe25:c903(fcfc::1:24fc:25ff:fe25:c903) 56 data bytes
From fcfc:0:0:1::2: icmp_seq=1 Destination unreachable: Address unreachable
From fcfc:0:0:1::2: icmp_seq=2 Destination unreachable: Address unreachable
From fcfc:0:0:1::2: icmp_seq=3 Destination unreachable: Address unreachable

ping google.com
PING google.com (172.217.18.14) 56(84) bytes of data.
64 bytes from fra02s19-in-f14.1e100.net (172.217.18.14): icmp_seq=1 ttl=56 time=0.787 ms
64 bytes from fra02s19-in-f14.1e100.net (172.217.18.14): icmp_seq=2 ttl=56 time=0.631 ms

4。这是容器中 ip 命令的输出

4. This is the output of the ip-commands in the container

ip -6 route show
fcfc:0:0:1::/64 dev eth0 proto kernel metric 256 pref medium
fe80::/64 dev eth0 proto kernel metric 256 pref medium
default via fcfc:0:0:1::1 dev eth0 metric 1024 pref medium

ip route show
default via 172.21.0.1 dev eth0
172.21.0.0/16 dev eth0 proto kernel scope link src 172.21.0.2

5。我试图创建一个 macvlan 网络

5. I tried to create a macvlan network

docker network create --driver macvlan --ipv6 --subnet=fcfc:0:0:1::/64 -o parent=ens17 db-link

现在,我可以ping服务器B的IP fcfc :: 1:24fc:25ff:fe25:c903 ,但由于macvlan限制了对界面独特,我无法连接到公共互联网。

Now I can ping the Server B's IP fcfc::1:24fc:25ff:fe25:c903 but as the macvlan limits access to the distinct interface, I cant connect to public internet.

6。结论


  • 已创建网桥网络,但无法获得与服务器B的ipv6提交的连接/路由–为什么?

  • macvlan网络使容器可以ping服务器B,但无法访问公共Internet。

推荐答案

与ISP一起,我们找到了一个解决方案:

Together with the ISP we found a solution:

/ etc / docker / daemon.json 我们将子网更改为:

{
"ipv6": true,
"fixed-cidr-v6": "fefe:1234::/80"
}

然后运行以下iptables命令:

And then ran the following iptables-commands:

ip6tables -t nat -I POSTROUTING -s fefe:1234::/80 -o ens16 -j MASQUERADE
ip6tables -t nat -A POSTROUTING -s fefe:1234::/80 -d fcfc::1/32 -o ens17 -j MASQUERADE

现在,我可以从容器内部ping ipv6地址,并且可以正确访问公共互联网,而无需使用任何自定义桥接e或macvlan网络。

Now Im able to ping the ipv6 address from inside the container also with proper access to the public internet without usage of any custom bridge or macvlan networks.

这篇关于如何从Docker容器连接到主机网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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