Reported symptoms
- Application returns 500 errors.
- Logs show
connection refusedordial tcp: lookup db: no such host. - The database container is running.
- Restarting the application container does not help.
Evidence provided
$ docker ps
CONTAINER ID STATUS NAMES
abc123def456 Up api
def789ghi012 Up db
$ docker logs api --tail 20
2026-08-08T13:45:23Z ERROR: could not connect to server: Connection refused
Is the server running on host "db" and accepting TCP/IP connections on port 5432?
$ docker exec api sh -c "getent hosts db"
# (no output)
Root cause
The application and database containers are on different Compose
networks, or the database container is on the default bridge
network (which does not support DNS-based service discovery), or
the application is using localhost instead of the service name.
Diagnosis
- Check the network configuration.
docker inspect api --format "{{.NetworkSettings.Networks}}"docker inspect db --format "{{.NetworkSettings.Networks}}"- Confirm both containers are on the same user-defined bridge network.
- Check DNS resolution from the application container.
docker exec api sh -c "getent hosts db"- If empty, DNS is broken. The container is not on the right network.
- Check the database container's exposed port.
docker inspect db --format "{{.NetworkSettings.Ports}}"- Confirm port 5432 is exposed.
- Check the application's connection string.
docker exec api env | grep -i db- Confirm it points to the service name, not
localhostor an IP.
Resolution
If the containers are on different networks:
services:
api:
networks: [app-net, db-net]
db:
networks: [db-net]
If the database is on the default bridge:
services:
api:
networks: [app-net]
db:
networks: [app-net]
If the connection string is wrong:
services:
api:
environment:
DATABASE_URL: postgres://app:secret@db:5432/app
Verification
- DNS resolves.
docker exec api getent hosts dbreturns the database IP. - Network connectivity.
docker exec api nc -zv db 5432succeeds. - Application works. The 500 errors are gone; requests succeed.