My API worked every time on my laptop. A new teammate cloned the repo, ran docker compose up , and got ECONNREFUSED 172.18.0.2:5432 on the first try. CI hit the same error about once a week. When I ran it again it was fine, and that's the most annoying kind of bug there is.
The cause was Docker Compose depends_on , which does less than its name suggests. Then I fixed it with a Postgres healthcheck, and that healthcheck lied too, but only on a fresh volume. That explained why it never broke for me.
I wrote a loop that ran docker compose down -v && docker compose up 40 times for each config. Here's what I found.
TL;DR
depends_on: [db] (short syntax) waits until the db container is started . It does not wait for Postgres to accept connections. With that config, 11 of my 40 fresh starts crashed.
To wait for readiness, use the long syntax with condition: service_healthy and give the db service a healthcheck .
pg_isready with no -h checks the Unix socket .…