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

poze 12 days ago90 gadeen

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?

🤖 Dyagnostik IA

Se IA ki fè l. Se pa yon repons — kominote a anba a konfime oswa korije l. Toujou verifye anvan ou fye l.

poze 12 days ago

3 Repons

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.

Konekte pou di lòt moun si li te mache.

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

Konekte pou di lòt moun si li te mache.

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.

Konekte pou di lòt moun si li te mache.

answered 12 days ago

Sign in and verify your email to post an answer.