WSL 2 is a wonderful technology, but it is not suited for the every possible workflow right from the start. Sometimes you need to use some tweaks to fit it in your toolchain.
In case you need to access some service running inside of WSL 2 container, web server for example, from your local network, you'll need to things:
1. Create a rule in a firewall allowing you TCP or UDP packets to reach some port on your host machine.
2. Map this port to another one listening within the WSL environment. Check IP address assigned to your VM in WSL. On Ubuntu, for example:
Now you only need to create the mapping. Start the elevated PowerShell console, and run the following command:
And you're all set! Any risks from running open ports on your Windows host are your own 😝
WSL 2 differs from WSL 1, you don't need such manipulations when using the first version.
I actually wrote this not to keep another link in my Pocket.