简体   繁体   English

用于 windows 的 WSL2 和 Docker 桌面的网络问题

[英]Networking problems with WSL2 and Docker Desktop for windows

TL;DR TL;博士

It seems I'm constantly running into networking problems when using WSL2 and Docker Desktop for Windows.在为 Windows 使用 WSL2 和 Docker 桌面时,我似乎经常遇到网络问题。

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.通常,我可以从 Docker 容器内部访问从 WSL2 实例(WSL2 中的“分发”)启动的端口,方法是找到 WSL2 实例的 IP 地址,并从容器内部引用该地址 ZA12A3079E18ACEDB2E1。 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.当我遇到 WSL2+DockerDesktop 问题时,我已经习惯了重新启动,但现在我想终于明白到底出了什么问题。

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.我还有一个公司的 Forticlient VPN 可能会搞砸。 But I have no choice in that matter... ;-(但在这件事上我别无选择...... ;-(

Details细节

I find the IP with:我发现 IP 有:

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 .所以 WSL2 机器的 IP 地址是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: 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:它也可以在 docker docker-desktop WSL2 容器内部工作:

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:-) 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 ?什么是172.26.0.2 Hmm...唔...

Networking setup:网络设置:

(I've modified Ethernet 3's IP address since it's in the corporate network) (我修改了以太网 3 的 IP 地址,因为它在公司网络中)

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

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.容器的很多东西都是在启动时编写的,并且从未更新或没有持续更新。

I'm still looking for a solution, but I found a quicker workaround: just restart WSL2.我仍在寻找解决方案,但我找到了更快的解决方法:只需重新启动 WSL2。 From a cmd window, enter:从 cmd window 输入:

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. Docker 会抱怨意外停止,然后在重新启动时会给您另一条消息,但这无害。

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.我有一个类似的问题,WSL2 的 IP 地址与 Docker 内部使用的172.17.0.0/16地址空间冲突。 In my case, in WSL2:就我而言,在 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.解决方法是将 Docker 使用的地址空间更改为另一个也不与我的本地网络冲突的地址空间。 In the settings in Docker Desktop, set "bip": "192.168.200.1/24" on the Docker Engine configuration page.在Docker Desktop的设置中,在Docker Engine配置页面设置"bip": "192.168.200.1/24"

Credit: https://www.beyondjava.net/docker-wsl-network信用: 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.重新启动您的 PC 或 WSL 暂时“修复”此问题,因为分配给 WSL 的 IP 恰好与 Docker 使用的地址不冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM