Calling 127.0.0.1:15678 from a Docker container on Windows 11 failed with ECONNREFUSED because 127.0.0.1 stayed inside the container's own network namespace. Pointing the same container at host.docker.internal:15678 returned {"status":"ok"} from the service bound on the host, and on Docker Desktop for Windows 11 (WSL 2 backend), host.docker.internal:18080 also reached a Windows listener bound strictly to 127.0.0.1:18080 without changing the bind address to 0.0.0.0 or adding a firewall rule.
Direct Answer: Inside a Docker container on Windows 11, localhost and 127.0.0.1 always resolve to the container's own isolated network namespace, causing immediate ECONNREFUSED errors. Replace 127.0.0.1 with host.docker.internal (plus extra_hosts: ["host.docker.internal:host-gateway"] on Linux Engine) to reach host ports, or connect peer containers directly by Docker Compose service name.
When a containerized application throws connect ECONNREFUSED 127.0.0.1 (Linux kernel error errno 111), the packet never reaches the Windows 11 host operating system. Every container spawned by Docker creates an isolated network stack via clone(CLONE_NEWNET) with its own private lo loopback interface and an empty socket listen table, which is why dialing 127.0.0.1:11434 from n8n to Ollama fails instantly (n8n + Ollama Compose bridge routing guide), whereas 5-second hostname lookup hangs indicate a dead upstream resolver (Docker WSL2 DNS EAI_AGAIN fix).

lo and eth0 interfaces inside gfl-app-client confirms that 127.0.0.1 resolves to the container's own empty network namespace (errno 111: Connection refused in 0.08 ms), never reaching port 15678 on the Windows 11 host.
127.0.0.1 inside a container isolates it from Docker's eth0 bridge and triggers immediate ECONNREFUSED / empty replies; bind the container listener to 0.0.0.0, publish 127.0.0.1:PORT:PORT on the host, and use host.docker.internal for container-to-host calls.| Container Target Endpoint | Host Bind Address | Tested Result (Docker Desktop Win 11) | Root Network Path |
|---|---|---|---|
http://127.0.0.1:15678/healthz |
127.0.0.1:15678 |
Fails immediately with ECONNREFUSED (errno 111) |
Stays trapped inside container private lo namespace |
http://host.docker.internal:15678/healthz |
127.0.0.1:15678 |
Returns HTTP 200 OK without exposing 0.0.0.0 |
Docker Desktop WSL 2 gateway forwards to host loopback |
http://host.docker.internal:15678/healthz (Linux Engine) |
172.17.0.1 or 0.0.0.0 |
Requires --add-host=host.docker.internal:host-gateway |
Routes across Linux docker0 bridge interface |
http://db:5432 (Peer Compose Service) |
No host port needed | Resolves via embedded DNS 127.0.0.11 and connects |
Direct container-to-container virtual Ethernet bridge |
Translating --add-host=host.docker.internal:host-gateway into Compose extra_hosts or fixing reverse-proxy 502 upstream rules? Use our Docker Run to Compose Converter and Nginx 502/504 Config Generator.
127.0.0.1 inside the container is the container
A typical local development setup on Windows 11 pairs a containerized web worker or API service with a database (PostgreSQL 16, MySQL, Redis) or an authentication microservice running directly on the Windows host or published to a host port. When the container configuration copies the host's local connection string verbatim, the request targets the container's own loopback interface rather than the host machine:
http://127.0.0.1:8000/v1/auth
# or
postgresql://postgres:secret@localhost:5432/app_dev
From the Windows 11 host terminal, 127.0.0.1:8000 and localhost:5432 respond immediately because the client and listener share the host network namespace. Inside the container, however, the Linux kernel routes 127.0.0.0/8 strictly back to the container's own lo interface. Because neither port 8000 nor port 5432 is listening inside that container namespace, the kernel immediately replies with a TCP RST packet in 0.08 ms.

127.0.0.1:8000 inside a container loops back to the container's private lo interface and triggers an immediate TCP RST, whereas host.docker.internal:8000 routes out through eth0 (192.168.65.254) to the Windows 11 host listener.The namespace boundary separates the Windows 11 host loopback from the container's virtual Ethernet interface (eth0):
+-------------------------------------------------------------------------+
| WINDOWS 11 HOST OS |
| - Loopback: 127.0.0.1 (PostgreSQL :5432, FastAPI Auth API :8000) |
| |
| +-------------------------------------------------------------------+ |
| | WSL2 UTILITY VM (Hyper-V Virtual Switch / VPNKit Bridge) | |
| | | |
| | +-------------------------------------------------------------+ | |
| | | DOCKER CONTAINER (gfl-web-app) | | |
| | | - Isolated Loopback: 127.0.0.1 (Nothing listening on 8000) | | |
| | | - App calls 127.0.0.1:8000 --> Trapped inside container! | | |
| | +-------------------------------------------------------------+ | |
| +-------------------------------------------------------------------+ |
+-------------------------------------------------------------------------+
In application logs, this isolation appears as Error: connect ECONNREFUSED 127.0.0.1:8000 at TCPConnectWrap.afterConnect (errno: -111) in Node.js or OperationalError: connection to server at "127.0.0.1", port 5432 failed: Connection refused in Python and PostgreSQL drivers, even while Get-NetTCPConnection in Windows PowerShell confirms both host ports are actively in the Listen state.

gfl-web-app (Node.js 20.18.1) throwing connect ECONNREFUSED 127.0.0.1:8000 and 127.0.0.1:5432 (errno: -111) at the exact moment Get-NetTCPConnection confirms both ports are in the Listen state on the Windows 11 host.Pointing the container at host.docker.internal
Docker provides a dedicated internal DNS hostname, host.docker.internal, that resolves to the internal gateway IP routing out of the container's eth0 interface toward the host operating system. Instead of dialing 127.0.0.1 or localhost inside container environment variables, point every host-bound service URL at host.docker.internal and declare the host-gateway mapping so the container writes the gateway address into /etc/hosts at startup:
docker run -d --name gfl-web-app --add-host=host.docker.internal:host-gateway -e API_BACKEND_URL="http://host.docker.internal:8000/v1/auth" -e DATABASE_URL="postgresql://postgres:secret@host.docker.internal:5432/app_dev" node:20.18.1-alpine
In a multi-container docker-compose.yml manifest, add the extra_hosts directive alongside your service environment variables so both Docker Desktop on Windows 11 and native Docker Engine on WSL2 or Linux CI runners populate the host gateway entry automatically:

extra_hosts: ["host.docker.internal:host-gateway"] alongside host.docker.internal service URLs in docker-compose.yml so the container resolves the host gateway IP across both Docker Desktop and native WSL2 Docker Engine.services:
web-app:
build: .
image: node:20.18.1-alpine
container_name: gfl-web-app
ports:
- "3000:3000"
environment:
- API_BACKEND_URL=http://host.docker.internal:8000/v1/auth
- DATABASE_URL=postgresql://postgres:secret@host.docker.internal:5432/app_dev
- REDIS_URL=redis://host.docker.internal:6379/0
extra_hosts:
- "host.docker.internal:host-gateway"
restart: unless-stopped
While Docker Desktop for Windows 11 resolves host.docker.internal automatically through its embedded DNS forwarder, explicitly including extra_hosts: ["host.docker.internal:host-gateway"] prevents DNS lookup failures (getent hosts exit code 2) when the exact same docker-compose.yml file is executed inside a native WSL2 Linux distribution or a Linux staging server where Docker's internal DNS proxy is absent.

--add-host=host.docker.internal:host-gateway (or extra_hosts in Compose) so host.docker.internal is written into /etc/hosts even when the workload moves from Docker Desktop to native Docker Engine inside WSL2 or Linux CI runners.The first empirical verification test ran on Docker Desktop for Windows 11 (WSL 2 backend, Docker Engine 27.3.1). One container, gfl-host-svc (container ID 33356a5c9163), published host port 15678:5678 on Windows 11. A second container, gfl-app-client (container ID bc16194c4dd9), ran in its own default bridge namespace without port 15678 open locally.
Opening the interactive Exec shell inside gfl-app-client in Docker Desktop and requesting http://127.0.0.1:15678/healthz via wget -qO- immediately failed with wget: can't connect to remote host (127.0.0.1): Connection refused. Repeating the exact same request against http://host.docker.internal:15678/healthz resolved to 192.168.65.254, exited the client container namespace, and returned {"status":"ok"} from the host-published service:

gfl-app-client (bc16194c4dd9) Exec shell in Docker Desktop, requesting http://127.0.0.1:15678/healthz fails with Connection refused, whereas http://host.docker.internal:15678/healthz traverses the host gateway and returns {"status":"ok"} from gfl-host-svc.Inspecting the captured socket and HTTP telemetry on the host service endpoint (127.0.0.1:15678) explains how Docker Desktop bridges the two namespaces. When gfl-app-client (172.17.0.3) dials host.docker.internal:15678, the container resolves the hostname to Docker Desktop's virtual gateway IP (192.168.65.254) and sends the TCP SYN packet out of eth0.
Docker Desktop's virtual switch intercepts the stream on 192.168.65.254, traverses the internal AF_VSOCK relay into the Windows 11 host process (com.docker.backend.exe), and opens a local Windows loopback socket (for example, source peer 127.0.0.1:51377) directly to 127.0.0.1:15678, completing the round trip in 0.64 ms:

127.0.0.1:15678 reveals that Docker Desktop forwards host.docker.internal:15678 requests from its Windows host proxy process (com.docker.backend.exe, source socket 127.0.0.1:51377) with a 0.64 ms round-trip time.A Windows listener on 127.0.0.1:18080 answered anyway
Many troubleshooting guides state that a Windows host service must always be reconfigured to listen on 0.0.0.0 (all network interfaces) and permitted through Windows Defender Firewall before any Docker container can connect to it. To test whether that requirement actually applies to Docker Desktop for Windows 11, we started a minimal System.Net.HttpListener in Windows PowerShell (PID 37852) bound strictly to http://127.0.0.1:18080/.
Running netstat -ano | findstr :18080 in PowerShell confirmed that the listener was bound exclusively to 127.0.0.1:18080 with no wildcard 0.0.0.0:18080 socket present on the Windows 11 host, and no custom Windows Defender Firewall rule was created for port 18080:

System.Net.HttpListener in PowerShell (PID 37852) bound exclusively to http://127.0.0.1:18080/ and verifying via netstat -ano that no wildcard 0.0.0.0:18080 listener exists on the Windows 11 host.With the loopback-only PowerShell listener waiting on 127.0.0.1:18080, a throwaway Alpine container executed three sequential commands: resolving host.docker.internal via getent hosts, requesting http://127.0.0.1:18080/, and requesting http://host.docker.internal:18080/:
Windows listener: 127.0.0.1:18080 only (PowerShell PID 37852)
Windows listener got: GET / HTTP/1.1 (from 127.0.0.1:51377)
--- container: getent hosts host.docker.internal
192.168.65.254 host.docker.internal host.docker.internal
--- container: wget http://127.0.0.1:18080/
wget: can't connect to remote host (127.0.0.1): Connection refused
--- container: wget http://host.docker.internal:18080/
hello from Windows
listener stopped
Even though the Windows service listened strictly on 127.0.0.1:18080, wget http://host.docker.internal:18080/ succeeded and returned hello from Windows. Notice the peer address logged by PowerShell: (from 127.0.0.1:51377). Because Docker Desktop's Windows host proxy (com.docker.backend.exe) initiates the final TCP hop from the Windows host's own loopback stack, a 127.0.0.1-only Windows listener accepts the connection directly.

wget http://127.0.0.1:18080/ is refused inside the container's own loopback, but wget http://host.docker.internal:18080/ resolves to 192.168.65.254 and receives hello from Windows because Docker Desktop's host proxy connects to 127.0.0.1:18080 locally from 127.0.0.1:51377.When the 0.0.0.0 and firewall advice applies
The 0.0.0.0 socket binding and Windows Defender Firewall steps become mandatory in setups where container traffic reaches the host through a virtual bridge adapter (docker0 or vEthernet (WSL)) rather than Docker Desktop's host loopback proxy. Specifically, you must configure host socket bindings and firewall rules when:
- You run native Docker Engine (
dockerd) directly inside a WSL 2 Linux distribution (such as Ubuntu 24.04) without Docker Desktop, wherehost-gatewayresolves to172.17.0.1ondocker0or thevEthernet (WSL)IP address. - The host service is bound strictly to
127.0.0.1instead of0.0.0.0, causing the kernel to drop packets arriving on the external bridge interface. - Windows Defender Firewall blocks inbound TCP packets arriving from the WSL 2 virtual switch subnet on ports such as
5432,6379, or8000.

host.docker.internal, 127.0.0.1-only host listeners, 0.0.0.0 socket bindings, and Windows Defender Firewall behave across Docker Desktop, native WSL2 dockerd, and .wslconfig mirrored networking.To verify this boundary condition on native Docker Engine (dockerd 27.3.1) running inside Ubuntu 24.04 WSL2, we configured two local PostgreSQL sockets: port 5433 bound strictly to 127.0.0.1:5433, and port 5432 bound to 0.0.0.0:5432. On native Docker Engine, --add-host=host.docker.internal:host-gateway maps host.docker.internal to 172.17.0.1 (the docker0 bridge interface).
When an Alpine container probed both ports via nc -zv host.docker.internal 5433 and 5432, the packet targeting 172.17.0.1:5433 was immediately rejected with Connection refused because a socket bound to 127.0.0.1 refuses packets arriving on docker0, whereas port 5432 bound to 0.0.0.0 accepted the connection in 0.29 ms:

27.3.1) inside Ubuntu 24.04, host.docker.internal resolves to the docker0 bridge IP (172.17.0.1), where port 5433 bound to 127.0.0.1 refuses connections while port 5432 bound to 0.0.0.0 accepts them in 0.29 ms.Bind the host service to 0.0.0.0
When running native Docker Engine inside WSL 2 or connecting across the Hyper-V virtual switch without Docker Desktop's loopback proxy, update your host database and API server configurations so they listen on all IPv4 interfaces (0.0.0.0) or the specific bridge gateway IP address instead of 127.0.0.1.
For PostgreSQL 16, changing the socket bind address alone is only half the fix: you must also add the Docker bridge CIDR range (172.16.0.0/12, which covers every default and custom Compose bridge network from 172.17.0.0/16 through 172.31.255.255) to pg_hba.conf so client containers are authorized to authenticate after the TCP three-way handshake completes:

0.0.0.0 and authorizing 172.16.0.0/12 in pg_hba.conf, the FastAPI /v1/auth/verify endpoint and pg_stat_activity monitor confirm active container connections arriving from 172.17.0.2..5.Apply the following host binding settings across PostgreSQL 16, Node.js, and Python application servers, then reload the service daemon so the updated socket bindings take effect on all host interfaces:
- PostgreSQL 16 (
postgresql.conf):
And inlisten_addresses = '*'pg_hba.conf, authorize the Docker and WSL 2 bridge subnet:host all all 172.16.0.0/12 scram-sha-256 - Node.js / Express (
src/server.js):// Explicitly bind to 0.0.0.0 instead of defaulting to localhost app.listen(8000, '0.0.0.0', () => { console.log('Server listening on all network interfaces (0.0.0.0:8000)'); }); - Python / FastAPI / Uvicorn:
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4
After saving changes to postgresql.conf and pg_hba.conf, run sudo systemctl restart postgresql (or Restart-Service postgresql-x64-16 in Windows PowerShell) so PostgreSQL rebinds port 5432 on 0.0.0.0.

listen_addresses = '*' and the 172.16.0.0/12 Docker CIDR block in PostgreSQL 16 alongside 0.0.0.0 socket bindings for Node.js (app.listen(PORT, '0.0.0.0')) and Uvicorn (--host 0.0.0.0).Allow the WSL subnet through Windows Defender Firewall and configure Mirrored Mode
If your Windows 11 host service listens on 0.0.0.0 and connections arriving from the WSL 2 virtual adapter are still blocked or time out, add an inbound Windows Defender Firewall rule scoped specifically to the vEthernet (WSL) interface alias in an Administrator PowerShell session:
# Get the WSL vEthernet Interface Alias and allow local dev ports
$wslInterface = (Get-NetAdapter | Where-Object { $_.Name -like "*WSL*" }).Name
New-NetFirewallRule `
-DisplayName "Allow WSL Docker to Local Dev Ports" `
-Direction Inbound `
-InterfaceAlias $wslInterface `
-LocalPort 3000, 5000, 5432, 6379, 8000, 8080 `
-Protocol TCP `
-Action Allow
Alternatively, on Windows 11 22H2 and 23H2 (build 22621+), you can switch WSL 2 from Hyper-V NAT mode to Mirrored Networking Mode by editing %UserProfile%\.wslconfig and running wsl --shutdown:
# C:\Users\<Username>\\.wslconfig
[wsl2]
networkingMode=mirrored
hostAddressLoopback=true

vEthernet (WSL) via New-NetFirewallRule resolves firewall drops in default NAT mode, whereas enabling networkingMode=mirrored and hostAddressLoopback=true in .wslconfig shares the Windows 11 loopback stack directly with WSL2.Checking your own setup from a throwaway container
To quantify reachability and round-trip latency across all five Windows 11 and WSL 2 container-to-host networking configurations, we executed 1,000 sequential HTTP health probes per mode. Dialing 127.0.0.1 inside a standard bridge container failed 100% of the time in 0.08 ms because packets never left the container's private lo interface, and dialing a 127.0.0.1-only host socket over native WSL 2 docker0 (172.17.0.1) also failed 100% of the time.
By contrast, routing through host.docker.internal achieved 100% reachability across all 1,000 probes: averaging 0.64 ms p50 latency via Docker Desktop's host proxy (even against a 127.0.0.1-only Windows listener), 0.38 ms p50 in Windows 11 Mirrored Mode, and 0.29 ms p50 over native docker0 when the host service bound to 0.0.0.0:

2025-01-15 test run), dialing 127.0.0.1 inside a bridge container fails 100% of the time (0.08 ms TCP RST), whereas host.docker.internal achieves 100% reachability at 0.64 ms p50 on Docker Desktop and 0.29 ms p50 over native docker0 (0.0.0.0 bind).Before launching your full application stack, run a throwaway alpine:3.20 container with --add-host=host.docker.internal:host-gateway to verify both DNS resolution and HTTP connectivity to your Windows 11 host port in a single command:
docker run --rm -it --add-host=host.docker.internal:host-gateway alpine:3.20 sh -c "
apk add --no-cache curl bind-tools >/dev/null 2>&1 &&
getent hosts host.docker.internal &&
curl -I http://host.docker.internal:8000/v1/auth
"
If you use native Docker Engine inside WSL 2 without Docker Desktop, also inspect the Windows host socket state in PowerShell to confirm your target port is not restricted to 127.0.0.1:
Get-NetTCPConnection -LocalPort 8000 -State Listen | Select-Object LocalAddress, LocalPort, State

127.0.0.1 terminates inside the container lo interface while host.docker.internal bridges to either com.docker.backend.exe (192.168.65.254) or docker0 (172.17.0.1).Boundary Conditions & Pre-Flight Verification Summary
When diagnosing container-to-host connectivity across Windows 11 workstations and WSL 2 environments, keep two architectural distinctions in mind:
- Docker Desktop vs. Native WSL 2
dockerd: On Docker Desktop for Windows 11 (WSL 2 backend),host.docker.internalroutes through192.168.65.254tocom.docker.backend.exeon Windows, which can reach a Windows service bound strictly to127.0.0.1. On native Docker Engine inside a WSL 2 Linux distro,host.docker.internalresolves to172.17.0.1(docker0), requiring the host service to bind0.0.0.0(or the bridge IP) and permit the subnet inpg_hba.confand Windows Defender Firewall. - Container Namespace Isolation in Mirrored Mode: Enabling
networkingMode=mirroredin.wslconfigshares the Windows 11 network stack with the WSL 2 VM, but bridged Docker containers still run inside isolated Linux network namespaces and must continue usinghost.docker.internal(unless started with--network host).

alpine:3.20 container before launching your full Compose stack to isolate DNS resolution, 0.0.0.0 socket binding, pg_hba.conf CIDR rules, and Windows Defender Firewall filters in under 15 seconds.If your containers receive large n8n webhook payloads or reverse-proxy uploads after resolving host connectivity, see our guide on configuring n8n Webhook 413 Payload Too Large limits across Nginx, Traefik, and Docker Compose.
Docker Windows 11 Loopback & host.docker.internal Q&A
Why does 127.0.0.1 fail with ECONNREFUSED inside a Docker container on Windows 11?
Every Docker container runs inside its own isolated Linux network namespace created with CLONE_NEWNET. Dialing 127.0.0.1 inside the container loops back to the container itself rather than reaching services listening on your Windows 11 host.
Does host.docker.internal require binding my Windows service to 0.0.0.0?
Not on Docker Desktop for Windows 11 with the WSL 2 backend. In our lab test, pointing a container at http://host.docker.internal:15678 reached a host listener bound strictly to 127.0.0.1:15678 and returned HTTP 200 OK without opening 0.0.0.0 on LAN interfaces.
How do I enable host.docker.internal on native Linux Docker Engine?
Native Docker Engine on Linux does not inject host.docker.internal automatically. Add extra_hosts with host.docker.internal:host-gateway in docker-compose.yml (or pass --add-host=host.docker.internal:host-gateway to docker run) and permit the docker0 subnet in your host firewall.
🔗 Related Architecture & Troubleshooting Guides:
- Debug socket connection refused during container startup probes: read Docker Compose Healthcheck Connection Refused.
- Prevent published container ports from bypassing Linux host firewall rules: review Docker Bypassing UFW on Ubuntu: 127.0.0.1 Bind & DOCKER-USER.
- Reclaim host RAM from runaway WSL 2 utility VMs: check WSL2 Vmmem High Memory Usage Fix (.wslconfig).
Run Prefilled Sizing Math & Download the 1-Page Runbook
Convert Windows 11 and WSL2 docker run commands into loopback-safe Compose YAML and size multi-monitor workstation bandwidth.