Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Write a docker-compose.yml for an app, Postgres and Redis, where the app waits for a healthy database.

IntermediateHands-onWrite-a-Dockerfile tasks
Answer
services:
  app:
    build: .
    ports: ["8000:8000"]
    environment:
      DATABASE_URL: postgresql://app:app@db:5432/app
      REDIS_URL: redis://cache:6379/0
    depends_on:
      db:    { condition: service_healthy }
      cache: { condition: service_started }
  db:
    image: postgres:16
    environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: app, POSTGRES_DB: app }
    volumes: [pgdata:/var/lib/postgresql/data]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d app"]
      interval: 5s
      retries: 10
  cache:
    image: redis:7-alpine
volumes:
  pgdata:

Key trick → Plain depends_on only orders startup. condition: service_healthy actually waits for the healthcheck to pass. Services reach each other by service name (db) on the Compose network.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks