Why localhost stops working when your app moves into Docker
Diagnose connection refused errors by identifying which container or browser a localhost address refers to.
App Cities · 2 min · Updated
Localhost means this place, not your whole app. In your browser, it means your computer. In a Docker container, it means that container. If your backend asks localhost for a database in another container, it is looking in the wrong place. Check who sends the request before changing the address.
The steps
- Read the failing connection's full address and identify the process making it. Start with the backend log or browser network panel.
- For two Compose services, replace localhost with the destination service name and its internal port. Keep this value in server-side configuration.
- Make the public web process listen on the container interface, not only its loopback address. Match its listening port to the App Cities web-service label.
- Retest one request at a time. If name resolution works but the connection is still refused, check whether the destination service is running and ready.
A common question
Should all localhost values be replaced?
No. Health checks within one container may correctly use localhost. Change the values whose caller and destination are in different environments.
Ready to try your project?
Prepare your project, check the prices and choose how you want to deploy.