Compose API starts before Postgres is ready
Summary: depends_on only controls startup order. The API starts while Postgres is still initializing, so integration tests see connection refused.
Failure type: compose_readiness
Ecosystem: compose
Difficulty: medium

Repository files:
path: docker-compose.yml
content: services:
  api:
    build: .
    environment:
      DATABASE_URL: postgres://postgres:postgres@db:5432/postgres
    depends_on:
      - db
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: postgres
path: Dockerfile
content: FROM python:3.12-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]
path: app.py
content: import os
print(os.environ['DATABASE_URL'])

Failure log:
api-1  | psycopg.OperationalError: connection failed: Connection refused
db-1   | database system is ready to accept connections

Build command: docker compose up --build --abort-on-container-exit
Verify commands:
docker compose config --quiet

Oracle patch:
diff --git a/docker-compose.yml b/docker-compose.yml
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,11 +1,16 @@
 services:
   api:
     build: .
     environment:
       DATABASE_URL: postgres://postgres:postgres@db:5432/postgres
     depends_on:
-      - db
+      db:
+        condition: service_healthy
   db:
     image: postgres:16-alpine
     environment:
       POSTGRES_PASSWORD: postgres
+    healthcheck:
+      test: ["CMD-SHELL", "pg_isready -U postgres"]
+      interval: 2s
+      timeout: 3s
+      retries: 20

Scoring - must touch:
docker-compose.yml
Scoring - must contain:
path: docker-compose.yml
text: condition: service_healthy
path: docker-compose.yml
text: pg_isready -U postgres
Scoring - must not contain: