Network connection from Docker container to WSL2 server not working properly
Windows Build Number
Microsoft Windows [Version 10.0.19043.1110]
WSL Version
- [X] WSL 2
- [ ] WSL 1
Kernel Version
5.4.72
Distro Version
Ubuntu 20.04
Other Software
Docker Desktop (Windows), version 3.5.2
Repro Steps
- Open a WSL2 terminal
- Execute
ip address show eth0and take note of the IP address next to 'inet' - Execute
nc -l 9003 - Run this command from inside a docker container:
curl <ip address from step 2>:9003
Expected Behavior
The terminal where you executed nc -l 9003 should display an output similar to the following:
GET / HTTP/1.1
Host: <ip address from step 2>:9003
User-Agent: curl/7.68.0
Accept: */*
Actual Behavior
Nothing is displayed.
Diagnostic Logs
No response
I tried the following with the same results:
-
In one WSL2 terminal:
nc -l 1234 -
In another WSL2 terminal:
docker run -it --rm alpine /bin/sh -c 'nc <ip-address> 1234'
However, when I run...
nc <ip-address> 1234
...in a WSL2 terminal without any other nc listening to any port anywhere else, it exits immediately. But if I do it in a docker container it doesn't exit immediately, it gets stuck there doing nothing, like if a connection were established without anything listening on the other side.
I also tried shutting down wsl with wsl --shutdown and I even restarted my computer, but got same results.
Are you using the same version of nc in both cases? What is the output if you pass nc -v to both instances? These versions may not talk to eachother correctly.
As well can you run strance nc from within the docker container to see what is happening on that side?
@craigloewen-msft
The -v option usually means version, but in the case of nc, it means verbose.
How can I check what version of netcat am I running?
For Debian, Ubuntu, and Mint:
netcat -h nc -hFor Fedora, Red Hat Enterprise Linux, and CentOS:
ncat -hThe first line of those commands will give the version.
$ nc -h
OpenBSD netcat (Debian patchlevel 1.206-1ubuntu1)
...
$ docker run --rm alpine /bin/sh -c 'nc --help'
BusyBox v1.33.1 () multi-call binary.
...
For some strange reason nc -h shows an error message saying the option is not recognized in the Docker container, but it still shows the version and help anyways.
$ docker run --rm alpine /bin/sh -c 'nc -h'
nc: unrecognized option: h
BusyBox v1.33.1 () multi-call binary.
...
I tried using curl and http-server instead of nc and apparently there is connection between the docker container and WLS2:
In one terminal:
$ docker run -p 8080:8080 --rm -it alpine
/ # apk add npm
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/8) Installing ca-certificates (20191127-r5)
(2/8) Installing nghttp2-libs (1.43.0-r0)
(3/8) Installing brotli-libs (1.0.9-r5)
(4/8) Installing c-ares (1.17.1-r1)
(5/8) Installing libgcc (10.3.1_git20210424-r2)
(6/8) Installing libstdc++ (10.3.1_git20210424-r2)
(7/8) Installing nodejs (14.17.4-r0)
(8/8) Installing npm (7.17.0-r0)
Executing busybox-1.33.1-r2.trigger
Executing ca-certificates-20191127-r5.trigger
OK: 58 MiB in 22 packages
/ # cat > index.html <<EOF
> Hello world!
> EOF
/ # npx http-server
Need to install the following packages:
http-server
Ok to proceed? (y) y
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://172.17.0.2:8080
Hit CTRL-C to stop the server
[2021-08-03T18:25:04.428Z] "GET /" "curl/7.68.0"
(node:25) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
(Use `node --trace-deprecation ...` to show where the warning was created)
In another terminal:
$ curl http://127.0.0.1:8080
Hello world!
It seems like it works, but when I try to use the docker container as a client and WSL2 as a server, this happens:
In one terminal:
$ cat > index.html <<EOF
> Bye world!
> EOF
$ npx http-server
Need to install the following packages:
http-server
Ok to proceed? (y) y
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://172.24.183.47:8080
Hit CTRL-C to stop the server
In another terminal:
$ docker run --rm -it alpine
/ # apk add curl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ca-certificates (20191127-r5)
(2/5) Installing brotli-libs (1.0.9-r5)
(3/5) Installing nghttp2-libs (1.43.0-r0)
(4/5) Installing libcurl (7.78.0-r0)
(5/5) Installing curl (7.78.0-r0)
Executing busybox-1.33.1-r2.trigger
Executing ca-certificates-20191127-r5.trigger
OK: 8 MiB in 19 packages
/ # curl http://127.0.0.1:8080
curl: (7) Failed to connect to 127.0.0.1 port 8080 after 0 ms: Connection refused
/ # curl http://172.24.183.47:8080
Just for the record: The original problem I was trying to solve was with XDebug. I had PHP code running in a docker container and wanted to debug it, and I was using VSCode with the Remote - WSL extension, but the debug wouldn't work. I knew XDebug acts as a client and the IDE it connects to is supposed to act as a server. I checked every configuration and everything was OK, the only thing left to check was the connection between the docker container and WSL2. It was then that I created this issue. I have to use VSCode without WSL2 to be able to use XDebug (You can access the wsl contents using this path: \\wsl$\Ubuntu-20.04. Then you only have to select the WSL bash as a default shell in your terminal settings).
@adriancuadrado Hi, you solved this problem?
@yatutmaster I think so. I have formatted the computer since then and I've tried this again and it works. I suspect there is something going on with the "Windows 10 hibernate" feature because I remember that sometimes some weird things happened when I used hibernate for too long. I'm closing this issue anyways.