# 多客户生产环境 Docker Compose 配置
# 共享MySQL实例，独立前端/后端/Redis容器
# 
# 重要说明：
# - 所有租户前端共享同一个 dist 目录: /root/aiedu/kaopeilian-frontend/dist
# - 编译一次前端，所有租户自动更新（无需重新构建镜像）
# - 更新前端步骤：cd /root/aiedu/kaopeilian-frontend && npm run build
# - 后端API地址由nginx反向代理根据域名自动路由，前端无需区分
#
name: prod-multi

services:
  # ============================================
  # 共享MySQL数据库服务
  # ============================================
  prod-mysql:
    image: mysql:8.0.43
    container_name: prod-mysql
    restart: unless-stopped
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: ProdMySQL2025!@#
      MYSQL_CHARACTER_SET_SERVER: utf8mb4
      MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci
    ports:
      - "3309:3306"
    volumes:
      - /data/mysql-data:/var/lib/mysql
      - ./kaopeilian-backend/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    command: --default-authentication-plugin=mysql_native_password
    networks:
      - prod-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pProdMySQL2025!@#"]
      timeout: 20s
      retries: 10
      start_period: 30s

  # ============================================
  # 华尔倍丽 (hua.ireborn.com.cn)
  # ============================================
  hua-frontend:
    # 使用共享的前端镜像，挂载统一的dist目录
    image: kaopeilian-frontend:shared
    container_name: hua-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "3010:80"
    volumes:
      - /root/aiedu/kaopeilian-frontend/dist:/usr/share/nginx/html:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      - hua-backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  hua-backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile
    image: prod-multi-hua-backend:latest
    container_name: hua-backend
    restart: unless-stopped
    env_file:
      - ./kaopeilian-backend/.env.hua
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
      - WORKERS=4        # 生产环境4个workers
      - RELOAD=false     # 生产环境关闭热重载
    ports:
      - "8010:8000"
    volumes:
      - ./kaopeilian-backend/app:/app/app
      - /data/prod-envs/uploads-hua:/app/uploads
      - /data/prod-envs/logs-hua:/app/logs
      - /data/prod-envs/secrets:/app/secrets:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      prod-mysql:
        condition: service_healthy
      hua-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  hua-redis:
    image: redis:7.2-alpine
    container_name: hua-redis
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6390:6379"
    volumes:
      - /data/redis-data/hua:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
    command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # ============================================
  # 杨扬宠物 (yy.ireborn.com.cn)
  # ============================================
  yy-frontend:
    # 使用共享的前端镜像，挂载统一的dist目录
    image: kaopeilian-frontend:shared
    container_name: yy-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "3011:80"
    volumes:
      - /root/aiedu/kaopeilian-frontend/dist:/usr/share/nginx/html:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      - yy-backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  yy-backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile
    image: prod-multi-yy-backend:latest
    container_name: yy-backend
    restart: unless-stopped
    env_file:
      - ./kaopeilian-backend/.env.yy
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
      - WORKERS=4
      - RELOAD=false
    ports:
      - "8011:8000"
    volumes:
      - ./kaopeilian-backend/app:/app/app
      - /data/prod-envs/uploads-yy:/app/uploads
      - /data/prod-envs/logs-yy:/app/logs
      - /data/prod-envs/secrets:/app/secrets:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      prod-mysql:
        condition: service_healthy
      yy-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  yy-redis:
    image: redis:7.2-alpine
    container_name: yy-redis
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6391:6379"
    volumes:
      - /data/redis-data/yy:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
    command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # ============================================
  # 武汉禾丽 (hl.ireborn.com.cn)
  # ============================================
  hl-frontend:
    # 使用共享的前端镜像，挂载统一的dist目录
    image: kaopeilian-frontend:shared
    container_name: hl-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "3012:80"
    volumes:
      - /root/aiedu/kaopeilian-frontend/dist:/usr/share/nginx/html:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      - hl-backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  hl-backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile
    image: prod-multi-hl-backend:latest
    container_name: hl-backend
    restart: unless-stopped
    env_file:
      - ./kaopeilian-backend/.env.hl
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
      - WORKERS=4
      - RELOAD=false
    ports:
      - "8012:8000"
    volumes:
      - ./kaopeilian-backend/app:/app/app
      - /data/prod-envs/uploads-hl:/app/uploads
      - /data/prod-envs/logs-hl:/app/logs
      - /data/prod-envs/secrets:/app/secrets:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      prod-mysql:
        condition: service_healthy
      hl-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  hl-redis:
    image: redis:7.2-alpine
    container_name: hl-redis
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6392:6379"
    volumes:
      - /data/redis-data/hl:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
    command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # ============================================
  # 芯颜定制 (xy.ireborn.com.cn)
  # ============================================
  xy-frontend:
    # 使用共享的前端镜像，挂载统一的dist目录
    image: kaopeilian-frontend:shared
    container_name: xy-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "3013:80"
    volumes:
      - /root/aiedu/kaopeilian-frontend/dist:/usr/share/nginx/html:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      - xy-backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  xy-backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile
    image: prod-multi-xy-backend:latest
    container_name: xy-backend
    restart: unless-stopped
    env_file:
      - ./kaopeilian-backend/.env.xy
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
      - WORKERS=4
      - RELOAD=false
    ports:
      - "8013:8000"
    volumes:
      - ./kaopeilian-backend/app:/app/app
      - /data/prod-envs/uploads-xy:/app/uploads
      - /data/prod-envs/logs-xy:/app/logs
      - /data/prod-envs/secrets:/app/secrets:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      prod-mysql:
        condition: service_healthy
      xy-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  xy-redis:
    image: redis:7.2-alpine
    container_name: xy-redis
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6393:6379"
    volumes:
      - /data/redis-data/xy:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
    command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # ============================================
  # 飞沃 (fw.ireborn.com.cn)
  # ============================================
  fw-frontend:
    # 使用共享的前端镜像，挂载统一的dist目录
    # 这样只需编译一次前端，所有租户自动更新
    image: kaopeilian-frontend:shared
    container_name: fw-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "3014:80"
    volumes:
      - /root/aiedu/kaopeilian-frontend/dist:/usr/share/nginx/html:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      - fw-backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  fw-backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile
    image: prod-multi-fw-backend:latest
    container_name: fw-backend
    restart: unless-stopped
    env_file:
      - ./kaopeilian-backend/.env.fw
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
      - WORKERS=4
      - RELOAD=false
    ports:
      - "8014:8000"
    volumes:
      - ./kaopeilian-backend/app:/app/app
      - /data/prod-envs/uploads-fw:/app/uploads
      - /data/prod-envs/logs-fw:/app/logs
      - /data/prod-envs/secrets:/app/secrets:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      prod-mysql:
        condition: service_healthy
      fw-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  fw-redis:
    image: redis:7.2-alpine
    container_name: fw-redis
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6394:6379"
    volumes:
      - /data/redis-data/fw:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
    command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # ============================================
  # 崔曦文 (cxw.ireborn.com.cn)
  # ============================================
  cxw-frontend:
    image: kaopeilian-frontend:shared
    container_name: cxw-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "3016:80"
    volumes:
      - /root/aiedu/kaopeilian-frontend/dist:/usr/share/nginx/html:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      - cxw-backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  cxw-backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile
    image: prod-multi-cxw-backend:latest
    container_name: cxw-backend
    restart: unless-stopped
    env_file:
      - ./kaopeilian-backend/.env.cxw
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
      - WORKERS=4
      - RELOAD=false
    ports:
      - "8016:8000"
    volumes:
      - ./kaopeilian-backend/app:/app/app
      - /data/prod-envs/uploads-cxw:/app/uploads
      - /data/prod-envs/logs-cxw:/app/logs
      - /data/prod-envs/secrets:/app/secrets:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
      - kaopeilian-network
    depends_on:
      prod-mysql:
        condition: service_healthy
      cxw-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  cxw-redis:
    image: redis:7.2-alpine
    container_name: cxw-redis
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6396:6379"
    volumes:
      - /data/redis-data/cxw:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - prod-network
    command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

# 网络配置
networks:
  prod-network:
    driver: bridge
    name: prod-network
  kaopeilian-network:
    external: true
    name: kaopeilian-network



