version: "3.8"

name: xl-prison-system

# 复用 RAGFlow 平台的 Docker 镜像创建独立的中间件服务
# MySQL: 基于 ragflow-mysql-1 镜像 (mysql:8.0.39)
# Redis: 基于 ragflow-redis-1 镜像 (redis:6-alpine / valkey:8)

services:
  # MySQL 数据库（复用 RAGFlow 镜像）
  mysql:
    image: mysql:8.0.39
    container_name: xl-prison-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: Prison2024!@
      MYSQL_DATABASE: prison
      MYSQL_CHARACTER_SET_SERVER: utf8mb4
      MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci
    ports:
      - "3306:3306"
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./sql:/docker-entrypoint-initdb.d:ro
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=mysql_native_password
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-pPrison2024!@"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - xl-prison-network
    deploy:
      resources:
        limits:
          memory: 2G
        reservations:
          memory: 512M

  # Redis 缓存（复用 RAGFlow 镜像）
  redis:
    image: redis:6-alpine
    container_name: xl-prison-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - ./data/redis:/data
    command: redis-server --requirepass Prison2024!@ --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "Prison2024!@", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - xl-prison-network
    deploy:
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 256M

  # 后端服务
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: xl-prison-backend
    restart: unless-stopped
    ports:
      - "48080:48080"
    environment:
      SPRING_PROFILES_ACTIVE: prod
      JAVA_OPTS: -Xms512m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/app/heapdump -Djava.security.egd=file:/dev/./urandom
    volumes:
      - ./backend/config:/app/config:ro
      - ./logs/backend:/app/logs
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - xl-prison-network
    deploy:
      resources:
        limits:
          memory: 2G
        reservations:
          memory: 512M
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:48080/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # 前端服务
  frontend:
    image: nginx:alpine
    container_name: xl-prison-frontend
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./frontend/dist:/usr/share/nginx/html:ro
      - ./frontend/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./logs/nginx:/var/log/nginx
    depends_on:
      - backend
    networks:
      - xl-prison-network
    deploy:
      resources:
        limits:
          memory: 256M
        reservations:
          memory: 128M

networks:
  xl-prison-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
