Interview Prep/Docker/Networking and Compose

A container needs to call a service running on the Docker host.

BeginnerScenarioNetworking and Compose
Answer

Situation read → localhost inside the container is itself.

What I'd do → Docker Desktop: use host.docker.internal. On Linux: --add-host=host.docker.internal:host-gateway (or Compose extra_hosts: ["host.docker.internal:host-gateway"]). The host service must listen on an address reachable from the bridge, not only 127.0.0.1.

Why → The bridge gateway (usually 172.17.0.1) is the host from the container's view.

Interview tip → --network host is an alternative but removes isolation.

All Docker questions Roadmap: Docker & containers

More on Networking and Compose