Docker localhost issue
Inside a container, localhost usually means that container itself. It may not mean your host machine or another container.
Fast checklist
- 1 Decide where the request is made: browser, host terminal, container, or another container.
- 2 Use localhost only when the target service is in the same network namespace.
- 3 Use container service names for container-to-container calls on the same Docker network.
- 4 Publish ports when the host browser needs to reach a container service.
Quick diagnostic checks
docker psFor host browser access, look for a mapping such as 0.0.0.0:8080->80/tcp, then open the host-side port in the browser.
docker compose psContainer-to-container traffic should usually use the Compose service name, not localhost.
host.docker.internalOn Docker Desktop, this special DNS name is the usual local-development bridge from a container back to the host machine.
Live request check
What this page sees
HTTP request headers
Sensitive headers such as cookies and authorization tokens are filtered before display.
The most common mistake
A backend inside Docker tries to call localhost and accidentally calls itself instead of a database, API, or host service.
Map the direction of traffic
Browser to container, container to host, and container to container are different cases. Fix the network address for the case you actually have.
Choose the address by direction
A Docker localhost issue is usually not one bug. It is a mismatch between who is making the request and where the target service actually lives.
- Host browser to container: publish a port and open the host-side port.
- Container to container: use the service name on the Docker network.
- Container to host: use the supported host alias for your Docker platform.
Related localhost guides
Localhost refused to connect
Fix localhost refused to connect by checking the server process, URL, port, protocol, firewall, and local logs.
Port guidelocalhost:8080
Open localhost:8080 and learn why Java servers, Tomcat apps, local proxies, and admin tools often use port 8080.
Developer toolLocalhost Port Lookup
Look up common localhost ports, what they are usually used for, and which command can check if a port is busy.
Network guideAccess localhost from phone
Learn why localhost on a phone points to the phone itself and how to access a local development server from mobile.
FAQ
Why can my browser open the app but the container cannot call localhost?
Your browser is on the host machine. The container is a different network context. Localhost means different places in those contexts.
Should I hard-code host.docker.internal everywhere?
No. Use environment-specific configuration so local Docker, CI, and production can use the right hostnames.
What is the fastest way to verify a localhost explanation?
Test the exact host, port, and path in the browser, then check the terminal or service logs. Localhost problems are easiest to solve when the URL and the running process match.
When should I use a LAN IP instead of localhost?
Use a LAN IP when another device, browser profile, virtual machine, or container needs to reach a service running on your computer.
Can a coding assistant give the wrong localhost URL?
Yes. Tutorials and coding assistants often assume a default port. Always compare the suggested URL with the actual URL printed by your local development server.