services:
  postgres:
    image: postgres:16-alpine
    container_name: deepseek-ocr-postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-ocr_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-ocr_password}
      POSTGRES_DB: ${POSTGRES_DB:-ocr_db}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-ocr_user} -d ${POSTGRES_DB:-ocr_db}"]
      interval: 5s
      timeout: 5s
      retries: 10

  backend:
    build: ./backend
    container_name: deepseek-ocr-backend
    env_file:
      - .env
    environment:
      MODEL_NAME: ${MODEL_NAME:-deepseek-ai/DeepSeek-OCR}
      HF_HOME: ${HF_HOME:-/models}
      API_HOST: ${API_HOST:-0.0.0.0}
      API_PORT: ${API_PORT:-8000}
      MAX_UPLOAD_SIZE_MB: ${MAX_UPLOAD_SIZE_MB:-100}
      DATABASE_URL: ${DATABASE_URL:-postgresql://ocr_user:ocr_password@postgres:5432/ocr_db}
      OCR_IMAGES_DIR: ${OCR_IMAGES_DIR:-/data/ocr_images}
      ENABLE_DEEPSEEK_LOCAL: ${ENABLE_DEEPSEEK_LOCAL:-true}
      OLLAMA_BASE_URL: ${OLLAMA_BASE_URL:-http://host.docker.internal:11434}
      OLLAMA_MODELS: ${OLLAMA_MODELS:-}
      DEFAULT_OCR_MODEL: ${DEFAULT_OCR_MODEL:-deepseek-local}
      OLLAMA_TIMEOUT: ${OLLAMA_TIMEOUT:-300}
    # Lets the container reach an Ollama server running on the Docker host
    # (works out of the box on Docker Desktop; required for Linux engines).
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./models:/models
      - ./ocr_images:/data/ocr_images
    depends_on:
      postgres:
        condition: service_healthy
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    shm_size: "4g"
    ports:
      - "${API_PORT:-8000}:${API_PORT:-8000}"

  frontend:
    build: ./frontend
    container_name: deepseek-ocr-frontend
    ports:
      - "${FRONTEND_PORT:-3000}:80"
    depends_on:
      - backend

volumes:
  postgres_data:

networks:
  default:
    name: rw-research
