[英]Trouble connecting to a TCP server through Docker on WSL 2
I'm using WSL2 on Windows 10 using an Ubuntu image, and Docker for Desktop Windows (2.2.2.0) with the WSL integration.我在使用 Ubuntu 映像的 Windows 10 上使用 WSL2,以及使用 WSL 集成的桌面 Windows (2.2.2.0) Docker。
I have a super basic rust tcp server.我有一个超级基本的 rust tcp 服务器。 I think the only relevant bit is:
我认为唯一相关的一点是:
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
println!("Listening on 8080");
for stream in listener.incoming() {
println!("Received connection");
let stream = stream.unwrap();
handle_connection(stream);
}
I can cargo install
and run the binary without issue;我可以毫无问题地
cargo install
和运行二进制文件; the line above prints, I can curl localhost:8080
from WSL and see the response as I'd expect from the rest of the code.上面的行打印,我可以从 WSL 卷曲
localhost:8080
并查看响应,正如我对其余代码所期望的那样。
I wanted to turn it into a docker image.我想把它变成一个 docker 镜像。 Here's the Dockerfile.
这是 Dockerfile。
FROM rust:1.40 as builder
COPY . .
RUN cargo install --path . --root .
FROM debian:buster-slim
COPY --from=builder ./bin/coolserver ./coolserver
EXPOSE 8080
ENTRYPOINT ["./coolserver"]
I then do:然后我做:
docker build -t coolserver .
docker run -it --rm -p 8080:8080 coolserver
I see Listening on 8080
as expected (ie no panic), but attempting to curl localhost:8080
yields curl: (52) Empty reply from server
.我看到
Listening on 8080
按预期Listening on 8080
(即没有恐慌),但尝试curl localhost:8080
产生curl: (52) Empty reply from server
。 This, I don't know what to make of.这,我不知道该怎么办。 Logging suggests my program gets to the point where it reaches
listener.incoming()
, but does not enter into the block.日志记录表明我的程序到达了
listener.incoming()
,但没有进入块。
To see if it was something to do with my setup (Docker for Desktop, WSL, etc.) or my Dockerfile, I followed the README for the docker-http-https-echo image, successfully.要查看它是否与我的设置(桌面 Docker、WSL 等)或我的 Dockerfile 相关,我成功地遵循了 docker -http-https-echo映像的自述文件。 I can curl it on the specified ports.
我可以将它卷曲在指定的端口上。
I don't know how to debug further.我不知道如何进一步调试。 Thanks in advance.
提前致谢。
EXPOSE
关键字是打开用于容器间通信的端口以使用来自主机的这些端口您必须在通过docker run
运行 docker 时使用-p 8080:8080
@CarlosRafaelRamirez resolved it for me. @CarlosRafaelRamirez 为我解决了这个问题。 It was as simple as binding to
0.0.0.0
rather than the loopback address 127.0.0.1
.它就像绑定到
0.0.0.0
而不是环回地址127.0.0.1
一样简单。 More info here: https://pythonspeed.com/articles/docker-connection-refused/更多信息在这里: https : //pythonspeed.com/articles/docker-connection-refused/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.