Docker Compose: how to wait for the database to be ready?
asked 12 days ago91 viewsen
My app container starts before Postgres accepts connections. depends_on isn't enough. What's the current best practice — healthchecks, wait scripts, or app-level retries?
🤖 AI diagnosis
AI-generated. Not an answer — the community below confirms or corrects it. Always verify before relying on it.
3 Answers
Use a healthcheck and condition: service_healthy:
db:
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 5
app:
depends_on:
db:
condition: service_healthy
Also add app-level connection retries — containers restart.
Sign in to tell others whether this worked.
Prepared statements only parameterize values, not identifiers. For a dynamic table name, whitelist against a known set:
$allowed = ['orders', 'invoices'];
if (!in_array($table, $allowed, true)) throw new InvalidArgumentException();Sign in to tell others whether this worked.
Use tsconfig paths with a @/* alias, group by feature not by type, and put shared code in a lib/ or packages/ folder. Enable strict and noUncheckedIndexedAccess early — retrofitting is painful.
Sign in to tell others whether this worked.
Sign in and verify your email to post an answer.