defect
defect

Reputation: 558

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.

I have a super basic rust tcp server. 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; 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.

I wanted to turn it into a docker image. Here's the 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 (i.e. no panic), but attempting to curl localhost:8080 yields 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.

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. I can curl it on the specified ports.

I don't know how to debug further. Thanks in advance.

Upvotes: 2

Views: 4817

Answers (2)

defect
defect

Reputation: 558

@CarlosRafaelRamirez resolved it for me. It was as simple as binding to 0.0.0.0 rather than the loopback address 127.0.0.1. More info here: https://pythonspeed.com/articles/docker-connection-refused/

Upvotes: 0

Vikrant Pawar
Vikrant Pawar

Reputation: 909

EXPOSE keyword is to open up ports for inter container communication for using these ports from host you have to use -p 8080:8080 while running docker via docker run

Upvotes: 1

Related Questions