【问题标题】:Can't connect from docker container to another. Connection refused无法从 docker 容器连接到另一个容器。拒绝连接
【发布时间】:2018-12-18 05:20:49
【问题描述】:

我正在尝试使用 telnet 建立从一个 docker 容器到另一个容器的连接,但是我一直收到“telnet:无法连接到远程主机:连接被拒绝”错误。

到目前为止我执行的命令:

docker run -dit --name nA --net mynet -p 8081:80 -p 1001:443 test_image:1.0 bash

docker run -dit --name nB --net mynet -p 8080:80 -p 1000:443 test_image:1.0 bash

这是 docker inspect mynet 的输出

"Containers": {
        "1df3f3821710b3f8103fe79e338d709a93baf301dafc015bb5f1e162bca206de": {
            "Name": "nB",
            "EndpointID": "bf7cbc2e60ddb99cd3b8bd416b066787840ced6de74eb1961b4f9663a28fdd80",
            "MacAddress": "02:42:ac:13:00:03",
            "IPv4Address": "172.19.0.3/16",
            "IPv6Address": ""
        },
        "7aea617e121eda0884ae7ce46e1534800af2d30822f3899f69c4165a40c1370d": {
            "Name": "nA",
            "EndpointID": "7a60026517ff0b1b337a5af9ccf8d7dc9c896606196bcb1bafe537e6fddaa8dc",
            "MacAddress": "02:42:ac:13:00:02",
            "IPv4Address": "172.19.0.2/16",
            "IPv6Address": ""
        }
    },

Ping 完美运行:

root@1df3f3821710:/# ping nA
PING nA (172.19.0.2) 56(84) bytes of data.
64 bytes from nA.mynet (172.19.0.2): icmp_seq=1 ttl=64 time=0.141 ms
64 bytes from nA.mynet (172.19.0.2): icmp_seq=2 ttl=64 time=0.261 ms

然后我尝试使用已发布的端口从一个容器连接到另一个容器。

root@1df3f3821710:/# telnet nA 8081
Trying 172.19.0.2...
telnet: Unable to connect to remote host: Connection refused
root@1df3f3821710:/# telnet nA 1001
Trying 172.19.0.2...
telnet: Unable to connect to remote host: Connection refused
root@1df3f3821710:/# telnet nA 80
Trying 172.19.0.2...
telnet: Unable to connect to remote host: Connection refused
root@1df3f3821710:/# telnet nA 443
Trying 172.19.0.2...
telnet: Unable to connect to remote host: Connection refused

从另一个容器连接时也会发生同样的情况。

root@7aea617e121e:/# telnet nB 8080
Trying 172.19.0.3...
telnet: Unable to connect to remote host: Connection refused
root@7aea617e121e:/# telnet nB 80  
Trying 172.19.0.3...
telnet: Unable to connect to remote host: Connection refused
root@7aea617e121e:/# telnet nB 1000
Trying 172.19.0.3...
telnet: Unable to connect to remote host: Connection refused
root@7aea617e121e:/# telnet nB 443 
Trying 172.19.0.3...
telnet: Unable to connect to remote host: Connection refused

这里有什么问题?

Docker 版本:17.09.0 操作系统:Mac 和 Windows

【问题讨论】:

    标签: docker networking


    【解决方案1】:
    1. 已发布的端口可从主机获得,它们不用于容器间通信。

    2. 您正在覆盖为您的映像定义的任何命令并改为启动 bash,因此这是在您的容器上运行的唯一进程,而您在容器内的应用程序根本没有启动。

    你的意思是如下

    docker run -d --name nA --net mynet -p 8081:80 -p 1001:443 test_image:1.0
    docker run -d --name nB --net mynet -p 8080:80 -p 1000:443 test_image:1.0
    

    然后连接到正在运行的容器

    $ docker exec -it nA bash
    
    $ docker exec -it nB bash
    

    【讨论】:

    • 其实并不是我执行的所有命令。在测试连接性之前,我在容器内运行了一些服务。在您发表评论之后,现在很明显,服务一定在某个时候崩溃了,导致这里的所有混乱。谢谢!
    • 我同意 808 端口?和100?不能用于容器间通信,但为​​什么不能使用端口 80(也许还有 443)?
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2020-08-10
    相关资源
    最近更新 更多