# Adapt to the repository root. Local Linux-container example, not production.
name: ${COMPOSE_PROJECT_NAME:?Set a unique local project or worktree name}

services:
  db:
    image: ${POSTGRES_IMAGE:?Choose a supported PostgreSQL 17 image tag or digest}
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      POSTGRES_USER: ${POSTGRES_USER:-dev}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set a private disposable local password}
    ports:
      - "127.0.0.1:${DB_HOST_PORT:-15432}:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
      interval: 5s
      timeout: 3s
      retries: 12
      start_period: 10s
    stop_grace_period: 30s

  cache:
    image: ${REDIS_IMAGE:?Choose a supported Redis image tag or digest}
    command: ["redis-server", "--appendonly", "yes"]
    ports:
      - "127.0.0.1:${CACHE_HOST_PORT:-16379}:6379"
    volumes:
      - cache_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 12

  db-client:
    profiles: ["tools"]
    image: ${POSTGRES_IMAGE:?Choose a supported PostgreSQL 17 image tag or digest}
    entrypoint: ["psql"]
    command: ["--version"]
    environment:
      PGHOST: db
      PGPORT: "5432"
      PGDATABASE: ${POSTGRES_DB:-app}
      PGUSER: ${POSTGRES_USER:-dev}
      PGPASSWORD: ${POSTGRES_PASSWORD:?Set a private disposable local password}
    depends_on:
      db:
        condition: service_healthy

volumes:
  db_data:
  cache_data:
