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.
More on Networking and Compose
Containers on the default bridge cannot resolve each other by name. Why, and what do you do?BeginnerHow do you expose a container only to the local host, not the internet?BeginnerDesign a Compose file for web + api + postgres + redis with correct startup ordering.IntermediateFrontend must reach the API but not the database. Segment the network.IntermediateWhen would you use `--network host`?IntermediateTwo Compose projects need to talk to each other.Intermediate