WSL Container Cannot Connect to Windows API: Fix Causes

Share

If a container in WSL can’t connect to a Windows API, the usual problem is the path to Windows, not the API itself: the wrong host address, a port that is not listening, or network filtering between WSL and Windows. The safest fix is to confirm the Windows service is up, test the address from inside WSL or the container, and only then check firewall, VPN, or permission blocks. In most cases, you do not need to change anything inside the container.

What the error usually looks like

The failure usually shows up as a timeout, connection refused, connection reset, or an auth error that only appears when the request comes from WSL.

connect: connection refused
Request timed out
Failed to connect to host
EOF
Symptom

Sometimes the container can reach the internet, but not the Windows service on a local port. That split is the clue: the container network is working, but the route to Windows is broken, the service is not listening where you think it is, or Windows is blocking the traffic. If the error only happens for one API, that often points to an app-level issue like the wrong URL, token, or certificate. If everything aimed at Windows fails, treat it as a host, address, or network issue first.

When this problem applies and when it does not

This applies when something running in WSL, including a Docker container inside WSL, needs to talk to a service running on Windows itself. Typical cases are a local REST API, a desktop app exposing localhost, or a Windows-only service on a port you expect to reach from Linux.

Before you change settings, make sure you are not actually dealing with a different problem:

  • A container-to-container networking issue inside Docker.
  • A name-resolution problem for a remote server, not Windows.
  • A certificate trust problem that looks like a connection problem.
  • An app listening only on 127.0.0.1 when you are trying to reach it through a different interface.

The exact behavior can differ between WSL 1 and WSL 2, and Docker Desktop can add another network layer on top. That changes how the host is reached, but the same basic checks still apply: is the service listening, is the address correct, and is something blocking the port?

Before you start

  • This applies to WSL 1 and WSL 2, and to Docker Desktop containers running inside WSL.
  • You do not need Administrator or sudo for the first checks, but you may need Administrator on Windows to change firewall rules.
  • No restart or backup is required for the initial checks.
  • First check the Windows service locally, then test the same address from inside WSL or the container.

If you are not sure whether the failure is inside the container, inside WSL, or on Windows, use this split:

  • If Windows can reach the API but the container cannot, the issue is likely in WSL addressing, Docker networking, or filtering.
  • If Windows cannot reach the API either, fix the Windows service first.
  • If neither side can reach it and the error is connection refused, the port is probably not listening.

Check the Windows service before touching WSL

Start on the Windows side. If the service is not running or not listening on the right port, WSL cannot fix that for you.

  1. Confirm the service is running on Windows.
Get-Service | Where-Object {$_.Status -eq 'Running'}
PowerShell
  1. Check that something is listening on the port you expect. Replace 8080 with your API port.
netstat -ano | findstr :8080
PowerShell
  1. Test the service locally from Windows.
Invoke-WebRequest http://localhost:8080/
PowerShell

If Invoke-WebRequest works on Windows but WSL still fails, the service is up and the problem is usually the host address, firewall, or WSL networking. If nothing is listening on the port, fix the service first, because no WSL-side change will make an unopened port answer. If the app listens only on localhost, it may still be reachable from Windows itself but not from the path your container is using.

Use the right address from inside WSL

Inside WSL, localhost can be misleading. Depending on the setup, localhost from Linux may point to the Linux side, not the Windows host you want.

Start with the simplest check from inside the container or WSL shell.

  1. Test the Windows host alias if your setup supports it.
curl -v http://host.docker.internal:8080/
Linux shell
  1. If that name does not resolve, try the Windows host IP from WSL. In many WSL 2 setups, the Windows host is the default gateway on the WSL network. You can inspect it with:
ip route | grep default
Linux shell
  1. Use the gateway IP in a direct test.
curl -v http://<gateway-ip>:8080/
Linux shell

If host.docker.internal works, use that in your app config if it stays stable in your environment. If the gateway IP works but localhost does not, the container was pointing at itself or at the Linux side instead of Windows. If nothing works, move to firewall and VPN checks, because the address may be fine and the traffic may simply be blocked.

Look at firewall, VPN, and permission blocks

A Windows service can be up and still reject traffic from WSL because something on the Windows side blocks the port. The most common blockers are Windows Defender Firewall, endpoint protection, and VPN software that changes routes or filters local traffic.

Start with the firewall rules for the port your service uses. If the service only accepts local loopback traffic, allow it to listen on the interface WSL is using, or create a rule that permits traffic from the WSL subnet if your policy allows that.

Do not open broad firewall access just to get the connection working. Allow only the port and source you actually need.

If you are on a VPN, disconnect it briefly and test again. Some VPN clients route or block private traffic in a way that breaks access from WSL to Windows-hosted services. If the connection works off VPN, the fix usually belongs in the VPN policy or split-tunnel settings, not in WSL.

Permission issues can also show up as access denied rather than a network error. In that case, the service may be listening, but it requires a token, a client certificate, or a Windows user context your container does not have. If the app has its own auth layer, check that separately from the network path.

How to verify the connection is really fixed

Verify from inside the same container that failed before. Use one simple request to the exact host and port your app uses, not a different shortcut.

  1. Run a plain HTTP request.
curl -v http://host.docker.internal:8080/
Linux shell
  1. If your API needs a specific path, test that path.
curl -v http://host.docker.internal:8080/health
Linux shell
  1. If the API is HTTPS, make sure you are testing HTTPS, not HTTP.
curl -vk https://host.docker.internal:8443/
Linux shell

You want to see an actual HTTP response, even if it is a 401 Unauthorized or 404 Not Found from the API itself. That still proves the network path works. If you still get a timeout or refusal, the problem is not fixed yet; go back to the host address and firewall checks and compare the working Windows-side test with the failing WSL-side test.

How to keep it from breaking again

Once it works, write down the exact host name or IP, port, and protocol your container should use. That matters because localhost may behave differently after a WSL restart, a Docker change, or a Windows update.

Keep the service bound to the correct interface, and avoid changing it to a narrower listen address unless you mean to. If the app depends on a VPN being connected, document that dependency so the next failure is easier to explain. After Windows updates, recheck the service, the firewall rule, and the WSL host address before assuming the container broke.

If you use scripts or environment files, centralize the API base URL there instead of hard-coding it in the app. That makes it much easier to adjust one value if the host address changes.

Common questions people hit next

WSL 1 and WSL 2 do behave differently. WSL 2 uses a virtualized network, so the Windows host address and port reachability are usually the first things to check. WSL 1 is closer to the Windows network stack, so some localhost cases are simpler there, but the service still has to be listening and allowed through any firewall rules.

Docker Desktop can change the path again, because a container inside WSL may be talking through Docker’s own network layer before it reaches Windows. If host.docker.internal works, that is often the easiest stable address to use in a container.

If only one API fails, compare that API’s port, protocol, and auth settings with one that works. One service may be HTTP while another is HTTPS, or one may require a certificate or token that the other does not. A single failing endpoint is often a configuration mismatch, not a general WSL networking failure.

Conclusion

Start with the Windows service itself, then test the address from inside WSL, then check firewall or VPN filtering if the service is up but still unreachable. Once you have a working test from the same container, copy that exact host, port, and protocol into the app config and keep it documented for the next time Windows or WSL changes.

Frequently Asked Questions

Does WSL 1 behave differently from WSL 2 for Windows-hosted APIs?

Yes. WSL 2 uses a virtual network, so host addressing is usually the first thing to check. WSL 1 is closer to the Windows network stack, but it can still fail if the service is not listening or if firewall rules block it.

Does Docker Desktop change the fix?

Sometimes. Docker Desktop can add its own network layer, so a container may need `host.docker.internal` or another host address instead of plain `localhost`. The same checks still apply: service listening, correct host, and no firewall block.

What if one Windows API works and another does not?

Compare the two endpoints carefully. Different ports, protocols, certificates, or auth rules can make one work and the other fail even though WSL networking is fine.

Read more