The app service is up but can't reach its database. Its log repeats:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
The db container is Up and healthy. Both containers are running on the same Docker network — they just aren't connecting. Find out why and state the fix.
app — webapp:3.1 (python), Up 20 minutes, retrying its DB connection.db — postgres:15-alpine, Up 1 hour, ready to accept connections.appnet.$ docker ps
# app and db are BOTH Up (the DB is not down)
$ docker exec app nc -zv 127.0.0.1 5432
nc: connect to 127.0.0.1 (127.0.0.1) port 5432 (tcp) failed: Connection refused
$ docker exec app nc -zv db 5432
Connection to db (172.28.0.3) 5432 port [tcp/*] succeeded!
The DB is reachable by its service name db, but not on 127.0.0.1.
You've solved it when:
127.0.0.1:5432 is refused fromdocker exec app nc -zv 127.0.0.1 5432) while the
DB IS reachable by its service name (docker exec app nc -zv db 5432
succeeds). Inside the app's network namespace, 127.0.0.1 is the app's own
loopback — Postgres lives in the db container's separate netns, so nothing
answers on the app's localhost:5432. NOT misdiagnosed as the DB being down,
a crashed Postgres, or a wrong port (docker ps shows db Up; db answers
on 5432 by name).
db (e.g. DATABASE_URL=…@db:5432/…) instead of
localhost/127.0.0.1, and recreate the app.
docker CLI only.docker restart app does NOT help — the 127.0.0.1 host is in the app'sdb answers on 5432 by name.127.0.0.1 mean something different inside the app container thandb container?
db to an IP at all — what serves that0.0.0.0 necessary-but-not-sufficient here, and-p 5432:5432 be the wrong fix for app↔db traffic?