Windows 版 WSL2 和 Docker Desktop 的网络问题

Pet*_*rch 12 networking docker windows-subsystem-for-linux docker-desktop wsl-2

长话短说

\n

在使用 WSL2 和 Windows 版 Docker Desktop 时,我似乎不断遇到网络问题。

\n

通常,我可以通过查找 WSL2 实例的 IP 地址并从容器内部引用该 IP 地址,从 Docker 容器内部访问从 WSL2 实例(WSL2 所说的“分发”)启动的端口。但今天由于某种原因这不起作用了。当遇到 WSL2+DockerDesktop 问题时,我已经习惯了重新启动,但现在我想最终了解到底出了什么问题。

\n

我非常确定,当我稍后重新启动时,一切都会好起来的。

\n

您知道如何调试这个东西吗?

\n

我还有一个公司 Forticlient VPN,可能会搞砸。但在这件事上我别无选择......;-(

\n

细节

\n

我找到了IP:

\n
WSL2:\xc2\xbb ip -br a\nlo               UNKNOWN        127.0.0.1/8 ::1/128 \nbond0            DOWN           \ndummy0           DOWN           \nsit0@NONE        DOWN           \neth0             UP             172.26.183.209/20 fe80::215:5dff:fef9:cc5c/64 \n
Run Code Online (Sandbox Code Playgroud)\n

所以 WSL2 机器的 IP 地址是172.26.183.209

\n

现在,我有一个 Web 服务器在 WSL2 进程中侦听端口 8080,可以通过http://172.26.183.209:8080访问它,正如 WSL2 所确认nmapcurl

\n
WSL2@~\xc2\xbb nmap -Pn -p 8080 172.26.183.209            \nStarting Nmap 7.80 ( https://nmap.org ) at 2020-12-23 16:15 CET\nNmap scan report for ubuntu-wsl2 (172.26.183.209)\nHost is up (0.000045s latency).\n\nPORT     STATE SERVICE\n8080/tcp open  http-proxy\n\nNmap done: 1 IP address (1 host up) scanned in 0.02 seconds\n\nWSL2@~\xc2\xbb curl http://172.26.183.209:8080 > /dev/null\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n
Run Code Online (Sandbox Code Playgroud)\n

然而,从容器内部:

\n
[root@0467b7ef17e7 /]# nmap -Pn -p 8080 172.26.183.209\nStarting Nmap 7.70 ( https://nmap.org ) at 2020-12-23 15:12 UTC\nNmap done: 1 IP address (0 hosts up) scanned in 0.43 seconds\n[root@0467b7ef17e7 /]# curl http://172.26.183.209:8080\ncurl: (7) Failed to connect to 172.26.183.209 port 8080: No route to host\n
Run Code Online (Sandbox Code Playgroud)\n

它也可以在 WSL2 容器内部运行docker-desktop

\n
WSL2@~\xc2\xbb wsl.exe -d docker-desktop wget http://172.26.183.209:8080\nConnecting to 172.26.183.209:8080 (172.26.183.209:8080)     \nConnecting to host.docker.internal:8180 (192.168.0.202:8180)\nindex.html           100% |********************************|  7308  0:00:00 ETA\n
Run Code Online (Sandbox Code Playgroud)\n

只是不是从容器内部。直到我重新启动。啊啊啊啊啊啊啊啊啊啊:-)

\n

一个提示 - 对我来说没有意义的东西 - 是来自容器内部的:

\n
[root@0467b7ef17e7 /]# ping 172.26.183.209\nPING 172.26.183.209 (172.26.183.209) 56(84) bytes of data.\nFrom 172.26.0.2 icmp_seq=1 Destination Host Unreachable\nFrom 172.26.0.2 icmp_seq=2 Destination Host Unreachable\nFrom 172.26.0.2 icmp_seq=3 Destination Host Unreachable\n^C\n--- 172.26.183.209 ping statistics ---\n6 packets transmitted, 0 received, +3 errors, 100% packet loss, time 202ms\npipe 4\n
Run Code Online (Sandbox Code Playgroud)\n

什么是172.26.0.2?唔...

\n

网络设置:

\n

(我已经修改了以太网 3 的 IP 地址,因为它位于公司网络中)

\n
WSL2@~\xc2\xbb ip -br a\nlo               UNKNOWN        127.0.0.1/8 ::1/128 \nbond0            DOWN           \ndummy0           DOWN           \nsit0@NONE        DOWN           \neth0             UP             172.26.183.209/20 fe80::215:5dff:fef9:cc5c/64 \nWSL2@~\xc2\xbb ipconfig.exe\n\nWindows IP Configuration\n\n\nEthernet adapter Ethernet 3:\n\n   Connection-specific DNS Suffix  . :\n   Link-local IPv6 Address . . . . . : fe80::405f:b7d2:70b4:b405%19\n   IPv4 Address. . . . . . . . . . . : 10.14.11.17\n   Subnet Mask . . . . . . . . . . . : 255.255.255.255\n   Default Gateway . . . . . . . . . : 10.14.11.18\n\nEthernet adapter Ethernet 4:\n\n   Media State . . . . . . . . . . . : Media disconnected\n   Connection-specific DNS Suffix  . :\n\nEthernet adapter Ethernet:\n\n   Connection-specific DNS Suffix  . :\n   Link-local IPv6 Address . . . . . : fe80::7cd1:5641:ac65:7004%15\n   IPv4 Address. . . . . . . . . . . : 192.168.0.202\n   Subnet Mask . . . . . . . . . . . : 255.255.255.0\n   Default Gateway . . . . . . . . . : 192.168.0.1\n\nEthernet adapter VirtualBox Host-Only Network:\n\n   Connection-specific DNS Suffix  . :\n   Link-local IPv6 Address . . . . . : fe80::f06d:9785:cff5:2ad0%6\n   IPv4 Address. . . . . . . . . . . : 192.168.225.1\n   Subnet Mask . . . . . . . . . . . : 255.255.255.0\n   Default Gateway . . . . . . . . . :\n\nWireless LAN adapter Wi-Fi:\n\n   Media State . . . . . . . . . . . : Media disconnected\n   Connection-specific DNS Suffix  . :\n\nWireless LAN adapter Local Area Connection* 7:\n\n   Media State . . . . . . . . . . . : Media disconnected\n   Connection-specific DNS Suffix  . :\n\nWireless LAN adapter Local Area Connection* 10:\n\n   Media State . . . . . . . . . . . : Media disconnected\n   Connection-specific DNS Suffix  . :\n\nEthernet adapter Ethernet 2:\n\n   Media State . . . . . . . . . . . : Media disconnected\n   Connection-specific DNS Suffix  . :\n\nEthernet adapter Bluetooth Network Connection:\n\n   Media State . . . . . . . . . . . : Media disconnected\n   Connection-specific DNS Suffix  . :\n\nEthernet adapter vEthernet (WSL):\n\n   Connection-specific DNS Suffix  . :\n   Link-local IPv6 Address . . . . . : fe80::c507:3c2b:62d:7270%39\n   IPv4 Address. . . . . . . . . . . : 172.26.176.1\n   Subnet Mask . . . . . . . . . . . : 255.255.240.0\n   Default Gateway . . . . . . . . . :\n\nWSL2@~\xc2\xbb route -n\nKernel IP routing table\nDestination     Gateway         Genmask         Flags Metric Ref    Use Iface\n0.0.0.0         172.26.176.1    0.0.0.0         UG    0      0        0 eth0\n172.26.176.0    0.0.0.0         255.255.240.0   U     0      0        0 eth0\n
Run Code Online (Sandbox Code Playgroud)\n

Dav*_*der 15

我遇到了类似的问题,WSL2 的 IP 地址与172.17.0.0/16Docker 内部使用的地址空间发生冲突。就我而言,在 WSL2 中:

$ hostname -I
172.17.112.35
Run Code Online (Sandbox Code Playgroud)

解决方法是将 Docker 使用的地址空间更改为另一个不与我的本地网络冲突的地址空间。在 Docker Desktop 的设置中,"bip": "192.168.200.1/24"在 Docker Engine 配置页面进行设置。

信用: https: //www.beyondjava.net/docker-wsl-network

在此输入图像描述

重新启动 PC 或 WSL 可以暂时“修复”此问题,因为分配给 WSL 的 IP 恰好与 Docker 使用的地址不冲突。

  • 这应该是固定答案。IT 陈述了根本冲突和永久解决方案。 (2认同)

小智 0

您是否尝试过仅重新启动容器而不是整个机器?听起来容器失去了其网络配置的跟踪。容器的很多东西都是在启动时编写的,并且从未更新或不一致更新。