为什么docker 立即关闭tcp 连接?
Why docker close the tcp connection immediately?
我用这个命令在终端测试网络连通性:
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.
好像马上就关闭了,我什么都不能输入
当我尝试 localy 时它工作正常
nc -l 0.0.0.0:9999
和
telnet localhost 9999
Docker 版本 17.12.1-ce,内部版本 7390fc6
Ubuntu VERSION="18.04.1 LTS(仿生海狸)"
总共有 2 种不同的风格 netcat
。 nc
container 和你的host 不是同一系列,所以host pass,container 解决方案失败。
我猜你的主机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 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.
好像马上就关闭了,我什么都不能输入
当我尝试 localy 时它工作正常
nc -l 0.0.0.0:9999
和
telnet localhost 9999
Docker 版本 17.12.1-ce,内部版本 7390fc6 Ubuntu VERSION="18.04.1 LTS(仿生海狸)"
总共有 2 种不同的风格 netcat
。 nc
container 和你的host 不是同一系列,所以host pass,container 解决方案失败。
我猜你的主机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