version: "3.8"

services:
  api:
    # build:
    #   context: .
    #   dockerfile: Dockerfile.multi
    #   target: api-build
    image: ghcr.io/danny-avila/librechat-dev-api:latest
    # ports:
    #   - 3080:3080
    depends_on:
      - mongodb
      - rag_api
    networks:
      - net
    extra_hosts:
      - "host.docker.internal:host-gateway"
    env_file:
      - stack.env
    environment:
      - HOST=0.0.0.0
      - NODE_ENV=production
      - MONGO_URI=mongodb://mongodb:27017/LibreChat
      - MEILI_HOST=http://meilisearch:7700
      - RAG_PORT=${RAG_PORT:-8000}
      - RAG_API_URL=http://rag_api:${RAG_PORT:-8000}
    volumes:
      - type: bind
        source: /home/trav/apps/LibreChat/librechat.yaml
        target: /app/librechat.yaml
      - /home/trav/apps/LibreChat/images:/app/client/public/images
      - /home/trav/apps/LibreChat/uploads:/app/uploads
      - /home/trav/apps/LibreChat/logs:/app/api/logs
      - /home/trav/claude-scripts:/mnt/claude-scripts
      - /home/trav/apps:/mnt/apps
      - /home/trav/biz-bud:/mnt/biz-bud
      - /home/trav/portainer:/mnt/portainer
      - /home/trav/repos:/mnt/repos
    deploy:
      replicas: 1
      restart_policy:
        condition: any
        delay: 20s
        max_attempts: 10
        window: 2m
      update_config:
        parallelism: 1
        delay: 20s
        failure_action: rollback
      placement:
        constraints:
          - node.hostname == little

  client:
    image: nginx:1.27.0-alpine
    # ports:
    #   - 80:80
    #   - 443:443
    depends_on:
      - api
    networks:
      - net
      - badge-net
    volumes:
      - /home/trav/apps/LibreChat/client/nginx.conf:/etc/nginx/conf.d/default.conf
    deploy:
      replicas: 1
      restart_policy:
        condition: any
        delay: 15s
        max_attempts: 10
        window: 2m
      update_config:
        parallelism: 1
        delay: 15s
        failure_action: rollback
      placement:
        constraints:
          - node.hostname == little

  mongodb:
    # ports:  # Uncomment this to access mongodb from outside docker, not safe in deployment
    #   - 27018:27017
    image: mongo
    networks:
      - net
    volumes:
      - librechat-mongodb:/data/db
    command: mongod --noauth
    deploy:
      replicas: 1
      restart_policy:
        condition: any
        delay: 20s
        max_attempts: 15
        window: 3m
      update_config:
        parallelism: 1
        delay: 20s
        failure_action: rollback
      placement:
        constraints:
          - node.hostname == little

  meilisearch:
    image: getmeili/meilisearch:v1.12.3
    networks:
      - net
    # ports: # Uncomment this to access meilisearch from outside docker
    #   - 7700:7700 # if exposing these ports, make sure your master key is not the default value
    env_file:
      - stack.env
    environment:
      - MEILI_HOST=http://meilisearch:7700
      - MEILI_NO_ANALYTICS=true
    volumes:
      - librechat-meili_data:/meili_data
    deploy:
      replicas: 1
      restart_policy:
        condition: any
        delay: 20s
        max_attempts: 15
        window: 3m
      update_config:
        parallelism: 1
        delay: 20s
        failure_action: rollback
      placement:
        constraints:
          - node.hostname == little

  vectordb:
    image: pgvector/pgvector:0.8.0-pg15-trixie
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
    networks:
      - net
    volumes:
      - librechat-pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U myuser -d mydatabase"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    deploy:
      replicas: 1
      restart_policy:
        condition: any
        delay: 20s
        max_attempts: 15
        window: 3m
      update_config:
        parallelism: 1
        delay: 20s
        failure_action: rollback
      placement:
        constraints:
          - node.hostname == little

  rag_api:
    image: ghcr.io/danny-avila/librechat-rag-api-dev-lite:latest
    environment:
      - DB_HOST=vectordb
      - RAG_PORT=${RAG_PORT:-8000}
    networks:
      - net
    depends_on:
      - vectordb
    env_file:
      - stack.env
    healthcheck:
      test: ["CMD-SHELL", "python3 -c \"import urllib.request; urllib.request.urlopen('http://localhost:8000/health')\""]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 40s
    deploy:
      replicas: 1
      restart_policy:
        condition: any
        delay: 20s
        max_attempts: 15
        window: 3m
      update_config:
        parallelism: 1
        delay: 20s
        failure_action: rollback
      placement:
        constraints:
          - node.hostname == little

  metrics:
    image: ghcr.io/virtuos/librechat_exporter:main
    depends_on:
      - mongodb
    # ports:
    #   - "8000:8000"
    networks:
      - net
      - observability_observability
    deploy:
      replicas: 1
      restart_policy:
        condition: any
        delay: 15s
        max_attempts: 10
        window: 2m
      update_config:
        parallelism: 1
        delay: 15s
        failure_action: rollback
      placement:
        constraints:
          - node.hostname == little

volumes:
  librechat-pgdata:
    name: librechat-pgdata
  librechat-mongodb:
    name: librechat-mongodb
  librechat-meili_data:
    name: librechat-meili_data

networks:
  net:
    driver: overlay
    attachable: true
  badge-net:
    external: true
  observability_observability:
    external: true
