App container keeps crash-looping on startup because the database isn't ready yet
compose.yaml has app depends_on: [db], but the app's first DB connection attempt fails because Postgres is still initializing. Fix this properly. Expected reasoning: depends_on alone only sequences container start, not readiness; add a healthcheck to the db service (e.g., pg_isready) and change app's dependency to depends_on: db: condition: service_healthy so Compose waits for Postgres to report healthy before starting the app. As a defense in depth, the app itself should also have connection retry logic, since this guarantee is Compose-specific and won't hold in environments without Compose's
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response