The api service is up but can't reach the database. Its log repeats could not translate host name "db" to address: Name or service not known. The db container is healthy and accepting connections. Both are running — they just can't find each other by name. Get api able to resolve and reach db.
api — myapp:latest, Up, retrying its DB connection.db — postgres:15-alpine, Up, healthy.$ docker exec api getent hosts db
(no output, exit 2 — the name doesn't resolve)
$ docker network inspect frontend # which containers are here?
$ docker network inspect backend
The name db won't resolve because Docker's embedded DNS only answers for names on the same user-defined network.
You've solved it when:
apidb are on different user-defined networks, so db returns
Name or service not known / NXDOMAIN — and NOT a DB-down, credentials,
or depends_on race issue.
, or attach api to backend) so docker exec api getent hosts db`
resolves and api can reach db.
docker CLI only.db's IP "fixes" it until the next restart re-assigns the IP —bridge network NOT give name resolution,db to frontend safer than hardcoding its container IP?