首頁 >後端開發 >php教程 >docker容器内部无法连接宿主机所在局域网

docker容器内部无法连接宿主机所在局域网

WBOY
WBOY原創
2016-06-06 20:07:575535瀏覽

  1. 使用centos镜像创建容器,在容器内部署php项目,由于要向宿主机所在局域网的另一台服务器请求接口服务,发起的http请求始终连接不上,但可以连接国际互联网。

2.
宿主机IP:172.20.20.152,
请求接口的目标服务器IP:172.17.3.97

创建Docker容器的命令:
docker run -p 80:80 --name=mycns -it eeb3a076a0be /bin/bash

进入容器后,执行命令:
ping 172.17.3.97
返回结果是:
From 172.17.0.3 icmp_seq=1 Destination Host Unreachable
From 172.17.0.3 icmp_seq=2 Destination Host Unreachable
From 172.17.0.3 icmp_seq=3 Destination Host Unreachable
From 172.17.0.3 icmp_seq=4 Destination Host Unreachable
From 172.17.0.3 icmp_seq=5 Destination Host Unreachable
From 172.17.0.3 icmp_seq=6 Destination Host Unreachable
From 172.17.0.3 icmp_seq=8 Destination Host Unreachable
From 172.17.0.3 icmp_seq=9 Destination Host Unreachable

安装docker版本信息:
docker version
返回:
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64

3.
宿主机执行ifconfig命令,返回:
docker0 Link encap:Ethernet HWaddr EE:25:C5:DA:10:47

<code>      inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
      inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:64400 errors:0 dropped:0 overruns:0 frame:0
      TX packets:69800 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:3945235 (3.7 MiB)  TX bytes:105962604 (101.0 MiB)
</code>

eth0 Link encap:Ethernet HWaddr F2:3D:F0:2B:43:31

<code>      inet addr:172.20.20.152  Bcast:172.20.255.255  Mask:255.255.0.0
      inet6 addr: fe80::f03d:f0ff:fe2b:4331/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:29702118 errors:0 dropped:0 overruns:0 frame:0
      TX packets:583992 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:2997801386 (2.7 GiB)  TX bytes:73288231 (69.8 MiB)
</code>

lo Link encap:Local Loopback

<code>      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:65536  Metric:1
      RX packets:7059 errors:0 dropped:0 overruns:0 frame:0
      TX packets:7059 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:4819120 (4.5 MiB)  TX bytes:4819120 (4.5 MiB)
</code>

veth50f5616 Link encap:Ethernet HWaddr EE:25:C5:DA:10:47

<code>      inet6 addr: fe80::ec25:c5ff:feda:1047/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:17 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:1014 (1014.0 b)  TX bytes:468 (468.0 b)
</code>

4.
宿主机本可以正常连接172.17.3.97这台机器,但docker服务启动后,无法正常连接。

来一位大神帮看看呵呵

回复内容:

  1. 使用centos镜像创建容器,在容器内部署php项目,由于要向宿主机所在局域网的另一台服务器请求接口服务,发起的http请求始终连接不上,但可以连接国际互联网。

2.
宿主机IP:172.20.20.152,
请求接口的目标服务器IP:172.17.3.97

创建Docker容器的命令:
docker run -p 80:80 --name=mycns -it eeb3a076a0be /bin/bash

进入容器后,执行命令:
ping 172.17.3.97
返回结果是:
From 172.17.0.3 icmp_seq=1 Destination Host Unreachable
From 172.17.0.3 icmp_seq=2 Destination Host Unreachable
From 172.17.0.3 icmp_seq=3 Destination Host Unreachable
From 172.17.0.3 icmp_seq=4 Destination Host Unreachable
From 172.17.0.3 icmp_seq=5 Destination Host Unreachable
From 172.17.0.3 icmp_seq=6 Destination Host Unreachable
From 172.17.0.3 icmp_seq=8 Destination Host Unreachable
From 172.17.0.3 icmp_seq=9 Destination Host Unreachable

安装docker版本信息:
docker version
返回:
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64

3.
宿主机执行ifconfig命令,返回:
docker0 Link encap:Ethernet HWaddr EE:25:C5:DA:10:47

<code>      inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
      inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:64400 errors:0 dropped:0 overruns:0 frame:0
      TX packets:69800 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:3945235 (3.7 MiB)  TX bytes:105962604 (101.0 MiB)
</code>

eth0 Link encap:Ethernet HWaddr F2:3D:F0:2B:43:31

<code>      inet addr:172.20.20.152  Bcast:172.20.255.255  Mask:255.255.0.0
      inet6 addr: fe80::f03d:f0ff:fe2b:4331/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:29702118 errors:0 dropped:0 overruns:0 frame:0
      TX packets:583992 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:2997801386 (2.7 GiB)  TX bytes:73288231 (69.8 MiB)
</code>

lo Link encap:Local Loopback

<code>      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:65536  Metric:1
      RX packets:7059 errors:0 dropped:0 overruns:0 frame:0
      TX packets:7059 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:4819120 (4.5 MiB)  TX bytes:4819120 (4.5 MiB)
</code>

veth50f5616 Link encap:Ethernet HWaddr EE:25:C5:DA:10:47

<code>      inet6 addr: fe80::ec25:c5ff:feda:1047/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:17 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:1014 (1014.0 b)  TX bytes:468 (468.0 b)
</code>

4.
宿主机本可以正常连接172.17.3.97这台机器,但docker服务启动后,无法正常连接。

来一位大神帮看看呵呵

找到原因了: http://dev.bidlust.com/?p=103

你可以不适用桥接网络,使用仅主机网络,这样就和主机一模一样了,docker run添加参数:--net host

应该是防火墙给拦了

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn