访问 docker 容器内的其他 docker 服务时超时

Dan*_*nny 5 networking web docker

我的主机 web1 和 web2 上运行着 2 个 docker 容器。容器web1将8080端口映射到主机18080端口,web2将8080端口映射到主机28080端口。主机的IP地址是192.168.20.111(没错是局域网)。我可以从同一局域网中的其他机器访问 192.168.20.111:18080 和 192.168.20.111:28080。

当我尝试从 web1/web2 容器中访问 192.168.20.111:18080 (curl 192.168.20.111:18080) 或 192.168.20.111:28080 时,出现超时错误。

但是,运行在主机8080端口的Apache服务器可以通过192.168.20.111:8080从web1/web2容器内访问,这意味着从容器到主机的路由是明确的。

所以我的问题是:为什么会发生超时错误以及如何从 docker 容器访问 192.168.20.111:18080?

这是我用来启动web2的docker-compose文件(web1也差不多):

version: '2' 
services:
    web:
        build: .
        ports:
          - "28080:8080"
        expose:
          - "8080"
        environment:
          - TALENTS_AUTH_HOST=192.168.20.111
          - TALENTS_AUTH_PORT=18080
          - TALENTS_ANALYSIS_HOST=192.168.20.111
          - TALENTS_ANALYSIS_PORT=18082
Run Code Online (Sandbox Code Playgroud)

这是 curl 的输出:

root@ea49393e56a4:/# curl -v http://192.168.20.111:28080
* Rebuilt URL to: http://192.168.20.111:28080/
*   Trying 192.168.20.111...
* TCP_NODELAY set
* connect to 192.168.20.111 port 28080 failed: Connection timed out
* Failed to connect to 192.168.20.111 port 28080: Connection timed out
* Closing connection 0
curl: (7) Failed to connect to 192.168.20.111 port 28080: Connection timed out
root@ea49393e56a4:/# 
Run Code Online (Sandbox Code Playgroud)

这是容器 web2 的 ip 路由:

root@ea49393e56a4:/# ip route
default via 172.25.0.1 dev eth0 
172.25.0.0/16 dev eth0 proto kernel scope link src 172.25.0.2 
root@ea49393e56a4:/# 
Run Code Online (Sandbox Code Playgroud)

这是 curl 使用网关 ip 的输出:

root@ea49393e56a4:/# curl -v http://172.25.0.1:28080
* Rebuilt URL to: http://172.25.0.1:28080/
*   Trying 172.25.0.1...
* TCP_NODELAY set
* connect to 172.25.0.1 port 28080 failed: Connection timed out
* Failed to connect to 172.25.0.1 port 28080: Connection timed out
* Closing connection 0
curl: (7) Failed to connect to 172.25.0.1 port 28080: Connection timed out
root@ea49393e56a4:/# 
Run Code Online (Sandbox Code Playgroud)

顺便说一下,主机(192.168.20.111)是一个运行 Ubuntu-16.04 的 VirtualBox 虚拟机,它托管在 Windows-10 桌面上,使用桥接网络连接到 LAN。

Dan*_*nny 7

非常感谢@atline

从容器内 ping 主机 ip 是可以的:

root@ea49393e56a4:/# ping 192.168.20.111
PING 192.168.20.111 (192.168.20.111) 56(84) bytes of data.
64 bytes from 192.168.20.111: icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from 192.168.20.111: icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from 192.168.20.111: icmp_seq=3 ttl=64 time=0.036 ms
64 bytes from 192.168.20.111: icmp_seq=4 ttl=64 time=0.036 ms
64 bytes from 192.168.20.111: icmp_seq=5 ttl=64 time=0.036 ms
64 bytes from 192.168.20.111: icmp_seq=6 ttl=64 time=0.039 ms
64 bytes from 192.168.20.111: icmp_seq=7 ttl=64 time=0.038 ms
^C
--- 192.168.20.111 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6000ms
rtt min/avg/max/mdev = 0.036/0.038/0.046/0.006 ms
Run Code Online (Sandbox Code Playgroud)

这意味着路线是明确的。正如@atline 所建议的,我用“sudo service ufw stop”在主机上停止了 ufw,然后一切正常。

我猜问题是 ufw 范围设置为允许 LAN 请求,但容器位于“172.25.0.0/16”子网中,与主机(192.168.0.0/16)不同的 LAN,因此请求被ufw屏蔽了。