# Honcho self-hosted deployment
# Primary LLM via "vllm" slot, optional backup via "custom" slot
#
# Usage:
#   docker compose up -d
#   docker compose logs -f api deriver
#
# First run will build the image and run migrations automatically.

services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    entrypoint: ["sh", "docker/entrypoint.sh"]
    depends_on:
      database:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - "8000:8000"
    env_file:
      - .env
    environment:
      - DB_CONNECTION_URI=postgresql+psycopg://honcho:honcho@database:5432/honcho
      - CACHE_URL=redis://redis:6379/0?suppress=true
      - CACHE_ENABLED=true
    volumes:
      - ./docker/entrypoint.sh:/app/docker/entrypoint.sh:ro
    restart: unless-stopped

  deriver:
    build:
      context: .
      dockerfile: Dockerfile
    entrypoint: ["/app/.venv/bin/python", "-m", "src.deriver"]
    depends_on:
      database:
        condition: service_healthy
      redis:
        condition: service_healthy
    env_file:
      - .env
    environment:
      - DB_CONNECTION_URI=postgresql+psycopg://honcho:honcho@database:5432/honcho
      - CACHE_URL=redis://redis:6379/0?suppress=true
      - CACHE_ENABLED=true
      - METRICS_ENABLED=false
    restart: unless-stopped

  database:
    image: pgvector/pgvector:pg15
    restart: unless-stopped
    ports:
      - "127.0.0.1:5432:5432"
    command: ["postgres", "-c", "max_connections=200"]
    environment:
      - POSTGRES_DB=honcho
      - POSTGRES_USER=honcho
      - POSTGRES_PASSWORD=honcho
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
      - pgdata:/var/lib/postgresql/data/
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U honcho -d honcho"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis:8.2
    restart: unless-stopped
    ports:
      - "127.0.0.1:6379:6379"
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  pgdata:
  redis-data:
