gpt4 book ai didi

docker - 为什么docker会立即关闭tcp连接?

转载 作者:行者123 更新时间:2023-12-02 18:10:55 24 4
gpt4 key购买 nike

我使用此命令来测试终端中的网络连接:

docker run --rm --name test -it -p 9999:9999 busybox nc -l 0.0.0.0:9999

在另一个终端中

$ telnet localhost 9999
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

好像立即关闭连接,我无法输入任何内容。

当我尝试本地时它工作正常

nc -l 0.0.0.0:9999

telnet localhost 9999

Docker 版本 17.12.1-ce,内部版本 7390fc6Ubuntu VERSION="18.04.1 LTS(仿生海狸)"

最佳答案

共有 2 种不同风格的 netcat。容器中的nc与你的主机不是同一系列,所以主机通过,容器解决方案失败。

我猜你的主机nc不是传统的,如下所示:

# nc
This is nc from the netcat-openbsd package. An alternative nc is available
in the netcat-traditional package.
usage: nc [-46bCDdhjklnrStUuvZz] [-I length] [-i interval] [-O length]
[-P proxy_username] [-p source_port] [-q seconds] [-s source]
[-T toskeyword] [-V rtable] [-w timeout] [-X proxy_protocol]
[-x proxy_address[:port]] [destination] [port]

您的容器nc是一个不同的版本,它具有完全不同的命令语法:

# docker run --rm --name test -it -p 9999:9999 busybox /bin/sh
/ # nc
BusyBox v1.29.3 (2018-10-01 22:37:18 UTC) multi-call binary.

Usage: nc [OPTIONS] HOST PORT - connect
nc [OPTIONS] -l -p PORT [HOST] [PORT] - listen

-e PROG Run PROG after connect (must be last)
-l Listen mode, for inbound connects
-lk With -e, provides persistent server
-p PORT Local port
-s ADDR Local address
-w SEC Timeout for connects and final net reads
-i SEC Delay interval for lines sent
-n Don't do DNS resolution
-u UDP mode
-v Verbose
-o FILE Hex dump traffic
-z Zero-I/O mode (scanning)

如果您在容器中使用netstat,您会发现您的命令没有打开9999端口,因此您的客户端立即退出。

因此,您需要将命令更改为:

docker run --rm --name test -it -p 9999:9999 busybox nc -l -p 9999

关于docker - 为什么docker会立即关闭tcp连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52848058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com