Docker · infra · fg-0256

Container cannot reach a service on the host through localhost, and host.docker.internal does not resolve on Linux

If you see connect ECONNREFUSED 127.0.0.1:5432 from inside the container, the cause and the fix for Docker are below, last verified 2026-10-15.

Verified Oct 2026trust 50/100
Symptom
connect ECONNREFUSED 127.0.0.1:5432 from inside the containergetaddrinfo ENOTFOUND host.docker.internal on Docker Engine for LinuxThe same Compose file works on Docker Desktop for Mac
Cause
Inside a container, localhost is the container itself. Docker Desktop resolves host.docker.internal on its own, but Docker Engine on Linux only does when you map the name to the special host-gateway value.
Fix
Map host.docker.internal to host-gateway and make the host service listen on an address the bridge can reach. Between Compose services, connect by service name instead.
Apply
services: app: extra_hosts: - "host.docker.internal:host-gateway" # docker run: --add-host host.docker.internal=host-gateway
Affected
Docker Engine on Linux, as of October 2026; Docker Desktop resolves the name by default
Sources

Last verified 2026-10-15 · Confirmed by 0 builders

Related traps

Have your agent know this before it fails

$ /plugin marketplace add getfootgun/footgun
$ /plugin install footgun@footgun