Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk5 min

How WSL Networking and Ports Work for Linux Containers

WSL 2 localhost forwarding, Docker port publishing, and container-to-host connections use different paths. Here’s how to choose the right address and troubleshoot failed ports.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where a service runs—and which way a connection is going—determines which address and port to use. Windows can usually reach a service in WSL 2 through localhost; reaching a Docker Desktop container requires publishing a port; and a container connecting back to a Windows service uses host.docker.internal. These paths cross distinct networking contexts, so the right fix depends on the source and destination of the connection.

Which networking layer is involved?

With Docker Desktop and Linux containers, think of the connection as crossing separate environments: the Windows host, a WSL 2 distribution, Docker Desktop’s Linux VM, and the container. A service running directly in your WSL distribution is not the same as one running inside a container. Docker Desktop forwards published container ports through its backend and Linux VM; WSL’s localhost forwarding is a separate feature.

As an Amazon Associate I earn from qualifying purchases.

Connection direction Address or setting to use
Windows → service running directly in WSL 2 localhost:<port> with the default NAT setup and localhost forwarding enabled
Windows → Docker Desktop container Publish a port, then connect to the published host port, commonly through localhost:<host-port>
WSL Linux process → Windows service under NAT The Windows host IP, obtainable from WSL’s default route
Container → Windows-hosted service host.docker.internal:<port>

Reach a service running directly in WSL from Windows

In the default WSL 2 NAT configuration, start the service in the distribution and open http://localhost:<port> from Windows. WSL localhost forwarding is enabled by default. If the connection fails, first confirm the process is running and listening on the intended port and interface. Then check the localhostForwarding setting in %UserProfile%.wslconfig; setting it to true enables the documented forwarding behavior.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To query a distribution’s IP from Windows, run:

wsl.exe --distribution <DistroName> hostname -I

That is the distribution’s address. It is not the Windows host address that a Linux process needs when it connects in the opposite direction under NAT.

Connect from WSL to a service on Windows

Under NAT, Linux-to-Windows traffic does not use the same localhost-forwarding path as Windows-to-WSL traffic. From the WSL shell, get the Windows host address from the default route:

ip route show | grep -i default | awk '{ print $3}'

Use the returned address with the Windows service’s listening port. The Windows service must also be listening on an interface reachable from WSL, and firewall rules must permit the connection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Publish a Docker Desktop container port to Windows

A Docker port mapping has the form HOST_PORT:CONTAINER_PORT. The container-side port must match the port on which the application listens inside the container; the host-side port can be different. For example, this maps Windows loopback port 8080 to port 80 in an Nginx container:

docker run --rm -p 127.0.0.1:8080:80 nginx

Open http://localhost:8080 on the Windows host. Docker Desktop accepts the connection and forwards it through its backend and Linux VM to the container.

For a mapping intended to listen on all host interfaces, use -p 8080:80. That broad binding can make the service reachable from other machines, depending on network configuration and firewall rules. Use the explicit 127.0.0.1 binding when only local host access is intended.

Published, exposed, and randomly assigned ports

Option What it does Host port behavior
EXPOSE in a Dockerfile or --expose Documents or exposes a container port to Docker networking; it does not by itself publish that port to the host. No host mapping is created by this option alone.
-p HOST_PORT:CONTAINER_PORT Publishes a specified container port to a specified host port. Fixed host port; without a host IP, Docker binds all host interfaces by default.
-P Publishes ports marked exposed to host ports selected by Docker. Randomly selected host ports; inspect the mapping with docker port.

Connect from a container to a Windows-hosted service

From a Docker Desktop container, use host.docker.internal as the hostname for a service on the Docker Desktop host—for example, host.docker.internal:5000 for a service listening on port 5000. This is the container-to-host direction. It does not publish a port for incoming connections to the container; use a Docker port mapping for that.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose between WSL NAT and mirrored networking

NAT is the WSL default. Microsoft documents mirrored networking for Windows 11 version 22H2 and later. In mirrored mode, Windows and WSL can use IPv4 127.0.0.1 for the documented localhost connection path; ::1 is not supported for that path. The mode also adds IPv6 support, improves VPN compatibility, supports multicast, and allows direct LAN access to WSL. LAN reachability still depends on firewall policy.

Consideration NAT Mirrored
Default and eligibility Default WSL networking mode. Requires Windows 11 version 22H2 or later.
Windows ↔ WSL localhost Windows can generally reach WSL services through localhost forwarding. For Linux-to-Windows connections, use the Windows host IP from the default route. The documented Windows/WSL localhost route uses IPv4 127.0.0.1; ::1 is unsupported for this path.
Additional networking needs Does not provide the mirrored-mode benefits listed by Microsoft. Adds IPv6 support, improved VPN compatibility, multicast, and direct LAN access to WSL.
Docker Desktop published-port caveat The documented mirrored-mode issue does not apply as stated. Microsoft documents a published-port failure at container creation under the default namespace; see the troubleshooting section.

WSL’s configuration reference lists nat, mirrored, none, deprecated bridged, and consomme as networkingMode values. To switch modes, edit %UserProfile%.wslconfig; for example, use networkingMode=mirrored for mirrored mode. Check the configuration reference for the applicable settings before changing networking behavior.

Troubleshoot a port that will not open

  1. Locate the service. Decide whether it runs directly in the WSL distribution or inside a Docker container. The WSL guest, Docker Desktop Linux VM, and container are different networking contexts.
  2. Check the listener. Confirm that the service is running on the port you expect. For a container, verify that the application listens on the container-side port in the mapping and on an interface reachable through the container network.
  3. Verify publication. Inspect the Docker run command or Compose configuration. EXPOSE alone does not publish a container port to the host; create a host-to-container mapping with -p or the equivalent Compose setting. If using -P, run docker port to find the selected host port.
  4. Match the address to the direction. For Windows-to-WSL under NAT, try localhost. For Windows-to-container, connect to the published host port. For a container-to-Windows connection, use host.docker.internal. For WSL-to-Windows under NAT, use the Windows host IP from the default route.
  5. Check binding and firewall rules. A service listening only on an unsuitable interface may not accept forwarded traffic. Docker’s host-side mapping without an explicit host IP binds all host interfaces by default; use 127.0.0.1 to restrict it to host loopback. For inbound access beyond the local host, review Windows Firewall and, where applicable, Hyper-V firewall rules.
  6. If mirrored mode is involved, check the known issue. Microsoft documents a Docker Desktop published-port failure during container creation under the default namespace. Its listed workarounds are --network host or the experimental ignoredPorts setting. Check Microsoft’s current WSL troubleshooting guidance before using either workaround: host networking changes the container’s network isolation and port-publishing behavior, while ignoredPorts is experimental.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.