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

services:
  dify-api:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}langgenius/dify-api:${DIFY_VERSION:-0.18.2}
    depends_on:
      dify-db:
        condition: service_healthy
      dify-redis:
        condition: service_healthy
    environment:
      - TZ=${TZ:-UTC}
      - MODE=api
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
      - SECRET_KEY=${SECRET_KEY:-sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U}
      - DATABASE_URL=postgresql://${POSTGRES_USER:-dify}:${POSTGRES_PASSWORD:-difypass}@dify-db:5432/${POSTGRES_DB:-dify}
      - REDIS_HOST=dify-redis
      - REDIS_PORT=6379
      - REDIS_DB=0
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
      - CELERY_BROKER_URL=redis://:${REDIS_PASSWORD:-}@dify-redis:6379/1
      - STORAGE_TYPE=${STORAGE_TYPE:-local}
      - VECTOR_STORE=${VECTOR_STORE:-weaviate}
      - WEAVIATE_ENDPOINT=http://dify-weaviate:8080
    volumes:
      - dify_storage:/app/api/storage
    deploy:
      resources:
        limits:
          cpus: ${DIFY_API_CPU_LIMIT:-1.0}
          memory: ${DIFY_API_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${DIFY_API_CPU_RESERVATION:-0.1}
          memory: ${DIFY_API_MEMORY_RESERVATION:-1G}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:5001/health'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  dify-worker:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}langgenius/dify-api:${DIFY_VERSION:-0.18.2}
    depends_on:
      dify-db:
        condition: service_healthy
      dify-redis:
        condition: service_healthy
    environment:
      - TZ=${TZ:-UTC}
      - MODE=worker
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
      - SECRET_KEY=${SECRET_KEY:-sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U}
      - DATABASE_URL=postgresql://${POSTGRES_USER:-dify}:${POSTGRES_PASSWORD:-difypass}@dify-db:5432/${POSTGRES_DB:-dify}
      - REDIS_HOST=dify-redis
      - REDIS_PORT=6379
      - REDIS_DB=0
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
      - CELERY_BROKER_URL=redis://:${REDIS_PASSWORD:-}@dify-redis:6379/1
      - STORAGE_TYPE=${STORAGE_TYPE:-local}
      - VECTOR_STORE=${VECTOR_STORE:-weaviate}
      - WEAVIATE_ENDPOINT=http://dify-weaviate:8080
    volumes:
      - dify_storage:/app/api/storage
    deploy:
      resources:
        limits:
          cpus: ${DIFY_WORKER_CPU_LIMIT:-1.0}
          memory: ${DIFY_WORKER_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${DIFY_WORKER_CPU_RESERVATION:-0.1}
          memory: ${DIFY_WORKER_MEMORY_RESERVATION:-1G}

  dify-web:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}langgenius/dify-web:${DIFY_VERSION:-0.18.2}
    depends_on:
      dify-api:
        condition: service_healthy
    environment:
      - TZ=${TZ:-UTC}
      - NEXT_PUBLIC_API_URL=${DIFY_API_URL:-http://localhost:5001}
      - NEXT_PUBLIC_APP_URL=${DIFY_APP_URL:-http://localhost:3000}
    ports:
      - '${DIFY_PORT_OVERRIDE:-3000}:3000'
    deploy:
      resources:
        limits:
          cpus: ${DIFY_WEB_CPU_LIMIT:-0.5}
          memory: ${DIFY_WEB_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${DIFY_WEB_CPU_RESERVATION:-0.1}
          memory: ${DIFY_WEB_MEMORY_RESERVATION:-256M}

  dify-db:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:15-alpine
    environment:
      - TZ=${TZ:-UTC}
      - POSTGRES_USER=${POSTGRES_USER:-dify}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-difypass}
      - POSTGRES_DB=${POSTGRES_DB:-dify}
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - dify_db_data:/var/lib/postgresql/data
    deploy:
      resources:
        limits:
          cpus: ${DIFY_DB_CPU_LIMIT:-0.5}
          memory: ${DIFY_DB_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${DIFY_DB_CPU_RESERVATION:-0.1}
          memory: ${DIFY_DB_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD-SHELL, pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  dify-redis:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:7-alpine
    command: redis-server --requirepass ${REDIS_PASSWORD:-}
    environment:
      - TZ=${TZ:-UTC}
    volumes:
      - dify_redis_data:/data
    deploy:
      resources:
        limits:
          cpus: ${DIFY_REDIS_CPU_LIMIT:-0.3}
          memory: ${DIFY_REDIS_MEMORY_LIMIT:-256M}
        reservations:
          cpus: ${DIFY_REDIS_CPU_RESERVATION:-0.1}
          memory: ${DIFY_REDIS_MEMORY_RESERVATION:-128M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

  dify-weaviate:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}semitechnologies/weaviate:${WEAVIATE_VERSION:-1.28.12}
    profiles:
      - weaviate
    environment:
      - TZ=${TZ:-UTC}
      - QUERY_DEFAULTS_LIMIT=25
      - AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=true
      - PERSISTENCE_DATA_PATH=/var/lib/weaviate
      - DEFAULT_VECTORIZER_MODULE=none
      - CLUSTER_HOSTNAME=node1
    volumes:
      - dify_weaviate_data:/var/lib/weaviate
    deploy:
      resources:
        limits:
          cpus: ${DIFY_WEAVIATE_CPU_LIMIT:-0.5}
          memory: ${DIFY_WEAVIATE_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${DIFY_WEAVIATE_CPU_RESERVATION:-0.1}
          memory: ${DIFY_WEAVIATE_MEMORY_RESERVATION:-512M}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:8080/v1/.well-known/ready'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  dify_storage:
  dify_db_data:
  dify_redis_data:
  dify_weaviate_data:
