Reputation: 15967
It seems I'm constantly running into networking problems when using WSL2 and Docker Desktop for Windows.
Normally, I can access a port started from a WSL2 instance ("distribution" in WSL2-speak) from inside Docker containers, by finding the IP address of the WSL2 instance, and referencing to that IP address from inside the containers. But today that doesn't work for some reason. I'm getting used to rebooting when I run into WSL2+DockerDesktop problems, but now I'd like to finally understand what is actually wrong.
I'm pretty sure that when I reboot in a moment, all will be fine and dandy.
Do you have an idea on how to debug something this?
I also have a corporate Forticlient VPN that could be messing this up. But I have no choice in that matter... ;-(
I find the IP with:
WSL2:» ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
bond0 DOWN
dummy0 DOWN
sit0@NONE DOWN
eth0 UP 172.26.183.209/20 fe80::215:5dff:fef9:cc5c/64
So the WSL2 machine's IP address is 172.26.183.209
.
So now that I have a web server listening on port 8080 in a process my WSL2, it can be accessed with http://172.26.183.209:8080, as confirmed by nmap
and curl
from WSL2:
WSL2@~» nmap -Pn -p 8080 172.26.183.209
Starting Nmap 7.80 ( https://nmap.org ) at 2020-12-23 16:15 CET
Nmap scan report for ubuntu-wsl2 (172.26.183.209)
Host is up (0.000045s latency).
PORT STATE SERVICE
8080/tcp open http-proxy
Nmap done: 1 IP address (1 host up) scanned in 0.02 seconds
WSL2@~» curl http://172.26.183.209:8080 > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
From inside a container, however:
[root@0467b7ef17e7 /]# nmap -Pn -p 8080 172.26.183.209
Starting Nmap 7.70 ( https://nmap.org ) at 2020-12-23 15:12 UTC
Nmap done: 1 IP address (0 hosts up) scanned in 0.43 seconds
[root@0467b7ef17e7 /]# curl http://172.26.183.209:8080
curl: (7) Failed to connect to 172.26.183.209 port 8080: No route to host
It also works from inside the docker-desktop
WSL2 container:
WSL2@~» wsl.exe -d docker-desktop wget http://172.26.183.209:8080
Connecting to 172.26.183.209:8080 (172.26.183.209:8080)
Connecting to host.docker.internal:8180 (192.168.0.202:8180)
index.html 100% |********************************| 7308 0:00:00 ETA
Just not from inside the containers. Until I reboot. AAAARRRRRHHHHHHHHHHHHHHHH :-)
One hint - something that doesn't make sense to me - is this from inside a container:
[root@0467b7ef17e7 /]# ping 172.26.183.209
PING 172.26.183.209 (172.26.183.209) 56(84) bytes of data.
From 172.26.0.2 icmp_seq=1 Destination Host Unreachable
From 172.26.0.2 icmp_seq=2 Destination Host Unreachable
From 172.26.0.2 icmp_seq=3 Destination Host Unreachable
^C
--- 172.26.183.209 ping statistics ---
6 packets transmitted, 0 received, +3 errors, 100% packet loss, time 202ms
pipe 4
What is 172.26.0.2
? Hmm...
(I've modified Ethernet 3's IP address since it's in the corporate network)
WSL2@~» ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
bond0 DOWN
dummy0 DOWN
sit0@NONE DOWN
eth0 UP 172.26.183.209/20 fe80::215:5dff:fef9:cc5c/64
WSL2@~» ipconfig.exe
Windows IP Configuration
Ethernet adapter Ethernet 3:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::405f:b7d2:70b4:b405%19
IPv4 Address. . . . . . . . . . . : 10.14.11.17
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 10.14.11.18
Ethernet adapter Ethernet 4:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::7cd1:5641:ac65:7004%15
IPv4 Address. . . . . . . . . . . : 192.168.0.202
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
Ethernet adapter VirtualBox Host-Only Network:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::f06d:9785:cff5:2ad0%6
IPv4 Address. . . . . . . . . . . : 192.168.225.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 7:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 10:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Bluetooth Network Connection:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter vEthernet (WSL):
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::c507:3c2b:62d:7270%39
IPv4 Address. . . . . . . . . . . : 172.26.176.1
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . :
WSL2@~» route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.26.176.1 0.0.0.0 UG 0 0 0 eth0
172.26.176.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
Upvotes: 16
Views: 30140
Reputation: 438
I faced this problem recently while trying to use Xdebug with Docker and WSL2. I tried everything from changing VSCode/Xdebug settings, disabling firewalls, and various internet/network troubleshooting steps, but nothing seemed to help or suggest any reason why my Docker container couldn't access my WSL2 host IP.
Finally, in desperation I uninstalled Docker and reinstalled it and Xdebug magically works again! So if all else fails, try reinstalling Docker Desktop.
Upvotes: 0
Reputation: 1033
I had a similar issue where the IP address for WSL2 conflicted with the 172.17.0.0/16
address space which Docker uses internally. In my case, in WSL2:
$ hostname -I
172.17.112.35
The fix was to change the address space Docker uses to a different one which also doesn't conflict with my local network. In the settings in Docker Desktop, set "bip": "192.168.200.1/24"
on the Docker Engine configuration page.
Credit: https://www.beyondjava.net/docker-wsl-network
Restarting your PC or WSL "fixes" this issue temporarily because the IP assigned to WSL happens to not conflict with addresses used by Docker.
Upvotes: 22
Reputation: 139
I'm still looking for a solution, but I found a quicker workaround: just restart WSL2. From a cmd window, enter:
wsl --shutdown
Docker will complain about having stopped unexpectedly, and then will give you another message when you restart it, but that's not harmful.
Upvotes: 0
Reputation: 101
Have you tried just restarting the container and not the whole machine? It sounds like it's the container losing track of its network configuration. A lot of things with containers are written at startup and never updated or not consistently updated.
Upvotes: 0