x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  litellm:
    <<: *defaults
    build:
      context: .
      args:
        target: runtime
    image: ${GHCR_IO_REGISTRY:-ghcr.io}/berriai/litellm:${LITELLM_VERSION:-main-stable}
    # Uncomment these lines to start proxy with a config.yaml file
    # volumes:
    #   - ./config.yaml:/app/config.yaml:ro
    # command:
    #   - "--config=/app/config.yaml"
    ports:
      - '${LITELLM_PORT_OVERRIDE:-4000}:4000'
    environment:
      - DATABASE_URL=postgresql://llmproxy:${POSTGRES_PASSWORD}@db:5432/litellm
      - STORE_MODEL_IN_DB=True
      - TZ=${TZ:-UTC}
    env_file:
      - .env
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: [CMD, curl, -f, 'http://localhost:4000/health/liveliness']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    extra_hosts:
      - 'host.docker.internal:host-gateway'
    deploy:
      resources:
        limits:
          cpus: ${LITELLM_CPU_LIMIT:-2.00}
          memory: ${LITELLM_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${LITELLM_CPU_RESERVATION:-0.50}
          memory: ${LITELLM_MEMORY_RESERVATION:-512M}

  db:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:${POSTGRES_VERSION:-16}
    environment:
      - POSTGRES_DB=litellm
      - POSTGRES_USER=llmproxy
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - TZ=${TZ:-UTC}
    ports:
      - '${POSTGRES_PORT_OVERRIDE:-5432}:5432'
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [CMD-SHELL, pg_isready -d litellm -U llmproxy]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: ${LITELLM_DB_CPU_LIMIT:-1.00}
          memory: ${LITELLM_DB_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${LITELLM_DB_CPU_RESERVATION:-0.25}
          memory: ${LITELLM_DB_MEMORY_RESERVATION:-256M}

  prometheus:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}prom/prometheus:${PROMETHEUS_VERSION:-v3.3.1}
    profiles:
      - metrics
    volumes:
      - prometheus_data:/prometheus
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
    ports:
      - '${PROMETHEUS_PORT_OVERRIDE:-9090}:9090'
    command:
      - --config.file=/etc/prometheus/prometheus.yml
      - --storage.tsdb.path=/prometheus
      - --storage.tsdb.retention.time=15d
    environment:
      - TZ=${TZ:-UTC}
    healthcheck:
      test: [CMD, wget, --spider, -q, 'http://localhost:9090/-/healthy']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: ${LITELLM_PROMETHEUS_CPU_LIMIT:-1.00}
          memory: ${LITELLM_PROMETHEUS_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${LITELLM_PROMETHEUS_CPU_RESERVATION:-0.25}
          memory: ${LITELLM_PROMETHEUS_MEMORY_RESERVATION:-256M}

volumes:
  prometheus_data:
  postgres_data:
