Docker / Networking, Ports & Service Connectivity
Understand why localhost reaches the wrong backend in a container
Inside an ordinary container, localhost refers to that container's network namespace, not another container or the Docker host.
Environment and inputs: Docker Engine · Compose v2 and Buildx where specified; Linux containers unless noted. Verify daemon context, installed feature support and authorized host or Swarm manager access first.
Start with this check
Check which system and tools you are using. Replace example objects, namespaces, images, file paths and hosts with approved values. Remove sensitive information before sharing output. Follow the application owner’s process for changes.
docker container inspect --format '{{.HostConfig.NetworkMode}}' app
What the results mean
The network mode helps establish whether normal namespace isolation, host mode or another mode applies. It does not identify the correct backend address by itself.
Fix the cause and check again
Use the approved peer service name and internal port, or the platform's documented host-access mechanism. Validate the actual connection from the workload context.
Before changing production
Do not disable network isolation or replace names with an arbitrary host IP without reviewing portability and exposure.