Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created June 12, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/9cafe020426f7d0651042cfbbed15af6 to your computer and use it in GitHub Desktop.
Save guitarrapc/9cafe020426f7d0651042cfbbed15af6 to your computer and use it in GitHub Desktop.
Allow WSL2 Ubuntu 22.04 to access Host Windows Docker Desktop (Windows 11 21H2)
# set this on Windows TaskScheduler with `At Logon, Run with highest priviledge, Delay task for 1min`
wsl.exe -d Ubuntu-22.04 --exec cat /etc/resolv.conf
Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)" -Name Public
docker run -it --rm -p 8080:80 nginx
curl "http://$(ip route list default | awk '{print $3}'):8080/"
@guitarrapc
Copy link
Author

guitarrapc commented Jun 12, 2022

Summary

WSL2 connection to the Host Windows is blocked via Windows Firewall.
This can be unlock by disabling Firewall profile for Interface WSL2 (vEthernet (WSL) on Public profile.

Due to WSL2 interface created everytime logged in to Windows, this operation would better run by TaskScheduler so that script will run everytime logged on.

Note
I've tried allowing WSL2 to Windows by Firewall rule Intstead of disable Windows Firewall for ethernet. However, it wasn't work with allowing port for Remote Computer or Interface.

日本語

ホスト Windows への WSL2 からの接続は Windows Firewall によってブロックされます。
これは Windows Firewall の Public Profile における WSL2 (vEthernet (WSL) を無効にすることで回避することができます。

ただWSL2 のインターフェースは Windows へのログオンのたびに再作成されるため、この処理は タスクスケジューラーなどに登録して自動的に実行するようにする必要があります。

Note
WSL2 のインターフェースに対してファイアウォールを無効にするのではなく、ポートやインターフェースを許可するルールで対処を試みましたが、残念ながら機能しませんでした。

Reference

  • Windows Defender Firewall blocks access from WSL2 · Issue #4139 · microsoft/WSL microsoft/WSL#4139
  • Add "allow" rule to Windows firewall for WSL2 network · Issue #4585 · microsoft/WSL microsoft/WSL#4585

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment