Multi-container App

API + Database + Cache

Example

A typical stack: the API is built from the local Dockerfile, waits until PostgreSQL is healthy, and keeps its data in a volume:

compose.yaml

services:
  api:
    build: .
    ports:
      - "8000:8000"
    environment:
      DATABASE_URL: postgresql://app:secret@db:5432/app
      REDIS_URL: redis://cache:6379/0
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app"]
      interval: 5s
      retries: 10

  cache:
    image: redis:7-alpine

volumes:
  pgdata:

How It Fits Together

  • Compose creates one network for the project; services reach each other by service name (db, cache).
  • depends_on with condition: service_healthy starts the API only after the database health check passes.
  • Named volumes declared at the bottom survive docker compose down.