Docker Compose: how to wait for the database to be ready?

asked 12 days ago91 viewsen

4

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.

asked 12 days ago

3 Answers

8

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.

answered 12 days ago
2

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.

answered 12 days ago
1

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.

answered 12 days ago

Sign in and verify your email to post an answer.