DEV Community

loading...
Cover image for Prevent W10 (20H1) WSL2 + Hyper-V From Eating Your Ports

Prevent W10 (20H1) WSL2 + Hyper-V From Eating Your Ports

sharpninja profile image The Sharp Ninja ・2 min read

So yesterday, I spent all day trying to solve the mystery of the disappearing ports. Our dev environment is Windows 10 (20H1) 2004 with Visual Studio 2019 for development. Our dependencies, including SQL Server, Open ID Service and a common service are all hosted in Linux Docker containers and run locally with WSL2. The ports we expose for the services are above 50000.

The application I was working on run in IIS Express locally on a port just above 50000. Yesterday unexpectedly I lost the ability to launch the app locally because IIS Express could bind to the port. I tried with full IIS and an App Domain but it couldn't bind either.

So off to Bing and Google I went.

Of course, the standard response is to use netsh to find the process holding open the port, but there wasn't one. That when I started down the path of reading issues in GitHub when I discovered an open issue in the WSL repository about this exact scenario.

Turns out, Docker and Hyper-V conspire to reserve 16,000+ ports. As you start up a container, Docker gets the ports it needs to communicate with Hyper-V from these. And these ports start at 49152, so it did take a long time for my working ports to fall in the range that Hyper-V was reserving for the open containers.

The solution is to change the starting port for Hyper-V to reserve like this:

netsh int ipv4 set dynamic tcp start=32768 num=16384
netsh int ipv6 set dynamic tcp start=32768 num=16384
Enter fullscreen mode Exit fullscreen mode

After making this change and rebooting, I now am safe to use any port above 49152!

Thank you for reading!

Discussion (1)

Collapse
zoedreams profile image
☮️✝️☪️🕉☸️✡️☯️

Mystery solved. I was wondering this to on my wsl2. Thank you for figuring it out.

Forem Open with the Forem app