services:
  api:
    build:
      context: .
      dockerfile: docker/api.Dockerfile
    image: yuxi-api:0.6.prod
    container_name: api-prod
    working_dir: /app
    networks:
      - app-network
    volumes:
      - ./saves:/app/saves
    extra_hosts:
      - "host.docker.internal:host-gateway"
    env_file:
      - .env.prod
    environment:
      - HOST_IP=${HOST_IP:-}
      - LITE_MODE=${LITE_MODE:-}
      - POSTGRES_URL=${POSTGRES_URL:-postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/${POSTGRES_DB:-yuxi_know}}
      - REDIS_URL=${REDIS_URL:-redis://redis:6379/0}
      - RUN_EVENTS_STREAM_TTL_SECONDS=${RUN_EVENTS_STREAM_TTL_SECONDS:-7200}
      - RUN_CANCEL_KEY_TTL_SECONDS=${RUN_CANCEL_KEY_TTL_SECONDS:-1800}
      - NEO4J_URI=${NEO4J_URI:-bolt://graph:7687}
      - NEO4J_USERNAME=${NEO4J_USERNAME:-neo4j}
      - NEO4J_PASSWORD=${NEO4J_PASSWORD:-0123456789}
      - MILVUS_URI=${MILVUS_URI:-http://milvus:19530}
      - MILVUS_TOKEN=${MILVUS_TOKEN:-}
      - MILVUS_DB_NAME=${MILVUS_DB_NAME:-default}
      - MINERU_VL_SERVER=${MINERU_VL_SERVER:-http://mineru-vllm-server:30000}
      - MINERU_API_URI=${MINERU_API_URI:-http://mineru-api:30001}
      - PADDLEX_URI=${PADDLEX_URI:-http://paddlex:8080}
      - MINIO_URI=${MINIO_URI:-http://minio:9000}
      - MODEL_DIR_IN_DOCKER=/models
      - RUNNING_IN_DOCKER=true
      - NO_PROXY=localhost,127.0.0.1,milvus,graph,minio,milvus-etcd,etcd,minio,mineru,paddlex,api.siliconflow.cn
      - no_proxy=localhost,127.0.0.1,milvus,graph,minio,milvus-etcd,etcd,minio,mineru,paddlex,api.siliconflow.cn
    command: uv run --no-dev uvicorn server.main:app --host 0.0.0.0 --port 5050
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:5050/api/system/health || exit 1"]
      interval: 30s
      timeout: 15s
      retries: 8
      start_period: 180s
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy

  worker:
    build:
      context: .
      dockerfile: docker/api.Dockerfile
    image: yuxi-api:0.6.prod
    container_name: worker-prod
    working_dir: /app
    networks:
      - app-network
    volumes:
      - ./saves:/app/saves
    extra_hosts:
      - "host.docker.internal:host-gateway"
    env_file:
      - .env.prod
    environment:
      - HOST_IP=${HOST_IP:-}
      - LITE_MODE=${LITE_MODE:-}
      - POSTGRES_URL=${POSTGRES_URL:-postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/${POSTGRES_DB:-yuxi_know}}
      - REDIS_URL=${REDIS_URL:-redis://redis:6379/0}
      - RUN_EVENTS_STREAM_TTL_SECONDS=${RUN_EVENTS_STREAM_TTL_SECONDS:-7200}
      - RUN_CANCEL_KEY_TTL_SECONDS=${RUN_CANCEL_KEY_TTL_SECONDS:-1800}
      - NEO4J_URI=${NEO4J_URI:-bolt://graph:7687}
      - NEO4J_USERNAME=${NEO4J_USERNAME:-neo4j}
      - NEO4J_PASSWORD=${NEO4J_PASSWORD:-0123456789}
      - MILVUS_URI=${MILVUS_URI:-http://milvus:19530}
      - MILVUS_TOKEN=${MILVUS_TOKEN:-}
      - MILVUS_DB_NAME=${MILVUS_DB_NAME:-default}
      - MINERU_VL_SERVER=${MINERU_VL_SERVER:-http://mineru-vllm-server:30000}
      - MINERU_API_URI=${MINERU_API_URI:-http://mineru-api:30001}
      - PADDLEX_URI=${PADDLEX_URI:-http://paddlex:8080}
      - MINIO_URI=${MINIO_URI:-http://minio:9000}
      - MODEL_DIR_IN_DOCKER=/models
      - RUNNING_IN_DOCKER=true
      - NO_PROXY=localhost,127.0.0.1,milvus,graph,minio,milvus-etcd,etcd,minio,mineru,paddlex,api.siliconflow.cn
      - no_proxy=localhost,127.0.0.1,milvus,graph,minio,milvus-etcd,etcd,minio,mineru,paddlex,api.siliconflow.cn
    command: uv run --no-dev arq server.worker_main.WorkerSettings
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy

  web:
    build:
      context: .
      dockerfile: docker/web.Dockerfile
      target: production
    image: yuxi-web:0.6.prod
    container_name: web-prod
    ports:
      - "80:80"
    depends_on:
      - api
    networks:
      - app-network
    environment:
      - NODE_ENV=production
      - VITE_LITE_MODE=${LITE_MODE:-}
    command: nginx -g "daemon off;"
    restart: unless-stopped

  graph:
    image: neo4j:5.26
    container_name: graph
    volumes:
      - ./docker/volumes/neo4j/data:/data
      - ./docker/volumes/neo4j/logs:/var/lib/neo4j/logs
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:7474 || exit 1"]
      interval: 20s
      timeout: 10s
      retries: 5
      start_period: 30s
    environment:
      - NEO4J_AUTH=${NEO4J_USERNAME:-neo4j}/${NEO4J_PASSWORD:-0123456789}
      - NEO4J_server_bolt_listen__address=0.0.0.0:7687
      - NEO4J_server_http_listen__address=0.0.0.0:7474
      - ENTITY_EMBEDDING=true
    networks:
      - app-network
    restart: unless-stopped

  etcd:
    container_name: milvus-etcd
    image: quay.io/coreos/etcd:v3.5.5
    environment:
      - ETCD_AUTO_COMPACTION_MODE=revision
      - ETCD_AUTO_COMPACTION_RETENTION=1000
      - ETCD_QUOTA_BACKEND_BYTES=4294967296
      - ETCD_SNAPSHOT_COUNT=50000
    volumes:
      - ./docker/volumes/milvus/etcd:/etcd
    command: etcd -advertise-client-urls=http://etcd:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd
    healthcheck:
      test: ["CMD", "etcdctl", "endpoint", "health"]
      interval: 60s
      timeout: 30s
      retries: 5
      start_period: 30s
    networks:
      - app-network
    restart: unless-stopped

  minio:
    container_name: minio
    image: minio/minio:RELEASE.2023-03-20T20-16-18Z
    environment:
      MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY:-minioadmin}
      MINIO_SECRET_KEY: ${MINIO_SECRET_KEY:-minioadmin}
    volumes:
      - ./docker/volumes/milvus/minio:/minio_data
      - ./docker/volumes/milvus/minio_config:/root/.minio
    command: minio server /minio_data --address 0.0.0.0:9000 --console-address 0.0.0.0:9001
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 60s
      timeout: 30s
      retries: 5
      start_period: 30s
    networks:
      - app-network
    restart: unless-stopped

  milvus:
    image: milvusdb/milvus:v2.5.6
    container_name: milvus
    command: ["milvus", "run", "standalone"]
    security_opt:
      - seccomp:unconfined
    environment:
      ETCD_ENDPOINTS: etcd:2379
      MINIO_ADDRESS: minio:9000
      MILVUS_LOG_LEVEL: error
    volumes:
      - ./docker/volumes/milvus/milvus:/var/lib/milvus
      - ./docker/volumes/milvus/logs:/var/lib/milvus/logs
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9091/healthz"]
      interval: 60s
      start_period: 120s
      timeout: 30s
      retries: 5
    depends_on:
      - "etcd"
      - "minio"
    networks:
      - app-network
    restart: unless-stopped

  postgres:
    image: postgres:16
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=${POSTGRES_USER:-postgres}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
      - POSTGRES_DB=${POSTGRES_DB:-yuxi_know}
      - TZ=${TZ:-Asia/Shanghai}
    volumes:
      - ./docker/volumes/postgresql:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-yuxi_know} || exit 1"]
      interval: 5s
      timeout: 3s
      retries: 30
    networks:
      - app-network
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: redis
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 10
    volumes:
      - ./docker/volumes/redis:/data
    networks:
      - app-network
    restart: unless-stopped

  mineru-vllm-server:
    build:
      context: .
      dockerfile: docker/mineru.Dockerfile
    image: mineru-vllm:latest
    container_name: mineru-vllm-server
    profiles:
      - all
    env_file:
      - .env.prod
    environment:
      MINERU_MODEL_SOURCE: local
    entrypoint: mineru-vllm-server
    command:
      --host 0.0.0.0
      --port 30000
    ulimits:
      memlock: -1
      stack: 67108864
    ipc: host
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:30000/health || exit 1"]
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              device_ids: ["0"]
              capabilities: [gpu]
    networks:
      - app-network
    restart: unless-stopped

  mineru-api:
    build:
      context: .
      dockerfile: docker/mineru.Dockerfile
    image: mineru-vllm:latest
    container_name: mineru-api
    profiles:
      - all
    env_file:
      - .env.prod
    ports:
      - 30001:30001
    environment:
      MINERU_MODEL_SOURCE: local
    entrypoint: mineru-api
    command:
      --host 0.0.0.0
      --port 30001
    ulimits:
      memlock: -1
      stack: 67108864
    ipc: host
    restart: unless-stopped
    networks:
      - app-network

  paddlex:
    build:
      context: .
      dockerfile: docker/paddlex.Dockerfile
    image: paddlex:latest
    container_name: paddlex-ocr
    profiles:
      - all
    volumes:
      - ./docker/volumes/paddlex:/paddlex
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"]
      interval: 20s
      timeout: 10s
      retries: 5
      start_period: 60s
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              device_ids: ['0']
              capabilities: [gpu]
    networks:
      - app-network
    restart: unless-stopped

networks:
  app-network:
    driver: bridge
