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

posée 12 days ago91 vuesen

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?

🤖 Diagnostic IA

Généré par IA. Ce n'est pas une réponse — la communauté ci-dessous le confirme ou le corrige. Vérifiez toujours avant de vous y fier.

posée 12 days ago

3 réponses

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.

Connectez-vous pour dire si ça a marché.

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();

Connectez-vous pour dire si ça a marché.

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.

Connectez-vous pour dire si ça a marché.

answered 12 days ago

Sign in and verify your email to post an answer.