首页 > 解决方案 > 无法从主机内连接到主机接口之一上的 docker 容器端口

问题描述

我有一台带有 2 个接口的 CentOS 7.8 机器。一个是我们的管理接口(eth0:10.53.198.175),另一个连接到我们的实验室网络(eth2:10.209.81.73)。

我运行一些容器,在本例中为 nginx,使用以下命令:

docker run --rm -d --publish 8888:8888 --name my_nginx nginx

docker ps 的输出:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
133cdce7b7e1        nginx               "/docker-entrypoint.…"   About an hour ago   Up About an hour    80/tcp, 0.0.0.0:8888->8888/tcp   my_nginx

由于我们绑定到 0.0.0.0,我希望它会监听所有接口。

现在来验证主机的可达性,如果我使用 telnet 连接到端口 8888,它适用于以下情况:

telnet localhost 8888 -> Works
telnet 0.0.0.0  8888 -> Works
telnet 10.209.81.73 8888 -> Works

但如果我给 eth0 IP 地址,它就不起作用:

telnet 10.53.198.175  8888 -> Doesn't work

从同一网络上的另一台主机 (10.53.198.x),telnet 到 10.53.198.175 8888 工作。

防火墙服务被禁用:

$ sudo systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

而且我在 iptables 中没有任何特定规则只能阻止 10.53.198.175

$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:menandmice-dns
ACCEPT     tcp  --  anywhere             172.18.0.3           tcp dpt:menandmice-dns
ACCEPT     tcp  --  anywhere             172.18.0.4           tcp dpt:menandmice-dns

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

$ 猫 /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

为什么 telnet 10.53.198.175 8888 在主机上不起作用?知道接下来我应该检查什么吗?

标签: docker

解决方案


问题的根本原因是我们实验室团队添加的一些 IP 规则优先于 docker route。所以路由基本上说,任何往返 10.53.198.175 的东西都从 eth0 发送出去。这是作为单独路由表的一部分创建的,因此使用 route -n 命令看不到条目。为 docker 配置更高优先级的路由后,通信正常。


推荐阅读