Docker:从容器内部到 localhost:port 的连接被拒绝

Docker: Connection from inside the container to localhost:port Refused

我正在尝试确保不同容器之间的连接和与端口 8040 一起使用的本地主机地址 (127.0.0.1) .(我的 Web 应用程序容器 运行 使用此端口。)

 root@a70b20fbda00:~# curl -v http://127.0.0.1
 * Rebuilt URL to: http://127.0.0.1/
 * Hostname was NOT found in DNS cache
 *   Trying 127.0.0.1...
 * connect to 127.0.0.1 port 80 failed: Connection refused
 * Failed to connect to 127.0.0.1 port 80: Connection refused
 * Closing connection 0
 curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused

这是我想从容器内部连接到本地主机时得到的结果

root@a70b20fbda00:~# curl -v http://127.0.0.1:8040
* Rebuilt URL to: http://127.0.0.1:8040/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* connect to 127.0.0.1 port 8040 failed: Connection refused
* Failed to connect to 127.0.0.1 port 8040: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 127.0.0.1 port 8040: Connection refused

关于每个容器中的iptables

 root@a70b20fbda00:~# iptables
 bash: iptables: command not found

容器之间的连接良好

root@635114ca18b7:~# ping 172.17.0.1
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.061 ms
64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.253 ms
--- 172.17.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
root@635114ca18b7:~# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.080 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.100 ms
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
root@635114ca18b7:~# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.149 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.180 ms
--- 172.17.0.3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.149/0.164/0.180/0.020 ms

Ping 127.0.0.1:8040

root@635114ca18b7:~# ping 127.0.01:8040
ping: unknown host 127.0.0.1:8040

在这种情况下我需要做什么?

所以全局镜像有两个容器,

您将必须使用 docker run --network host IMAGE:TAG 来实现所需的连接

进一步阅读here

示例:-

docker run --network host --name CONTAINER1 IMAGE:tag

docker run --network host --name CONTAINER2 IMAGE:tag

在容器内 - CONTAINER2 您将能够作为主机 CONTAINER1 访问其他容器

要访问该服务,您必须执行 CONTAINER:

根据提供的信息,看起来有两个容器。如果这两个容器由 docker 启动而不是 --net=host 那么它们每个都会获得两个不同的 IP 地址。假设您的第一个容器 172.17.0.2 而第二个容器 172.17.0.3.

在这种情况下,每个容器都有自己的网络堆栈。所以127.0.0.1指的是它自己的网络栈不一样。

正如@kakabali 所指出的,可以运行 容器与主机网络共享主机的网络堆栈。

其他选项之一是在第二个容器中使用第一个容器的实际 IP 地址。

second-container# curl http://172.17.0.2

或者另一种选择是 运行 第二个容器作为 sidekick/sidecar 容器共享第一个容器的网络堆栈。

docker run --net=container:${ID_OF_FIRST_CONTAINER} ${IMAGE_SECOND}:${IMAGE_TAG_SECOND}

或者如果您正确使用 links docker run --name web -itd ${IMAGE_FIRST}:${TAG_FIRST} docker run --link web -itd ${IMAGE_SECOND}:${TAG_SECOND}

注意:docker --link 功能已弃用。

另一种选择是使用容器管理平台,自动为您发现服务。

PS:您无法在不同的端口上 ping 一个 IP 地址。有关详细信息,请单击 here