version: '3.8'

networks:
  group-buy-network:
    external: true

services:
  # 1. 前端
  group-buy-market-front:
    image: nginx:alpine
    container_name: group-buy-market-front
    restart: always
    ports:
      - '86:80'
    volumes:
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
    privileged: true
    networks:
      - group-buy-network

  # 2. MySQL
  mysql:
    image: mysql:8.0
    container_name: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: 123456
    ports:
      - '13306:3306'
    volumes:
      - ./mysql/my.cnf:/etc/mysql/conf.d/mysql.cnf:ro
      - ./mysql/sql:/docker-entrypoint-initdb.d
    healthcheck:
      test: ['CMD', 'mysqladmin', 'ping', '-h', 'localhost']
      interval: 5s
      timeout: 10s
      retries: 10
      start_period: 15s
    networks:
      - group-buy-network

  # 3. Redis
  redis:
    image: redis:6.2
    container_name: redis
    restart: always
    hostname: redis
    ports:
      - '16379:6379'
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 3
    networks:
      - group-buy-network

  # 4. Java 后端
  group-buying-sys:
    build:
      context: ../../..         # 从 docs/tag/v2.0 回到项目根
      dockerfile: group-buying-sys-app/Dockerfile
    image: smile/group-buying-sys:latest
    container_name: group-buying-sys
    restart: on-failure
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - '8091:8091'
    environment:
      - TZ=Asia/Shanghai
      - SPRING_PROFILES_ACTIVE=prod
    volumes:
      - ./log:/data/log
    logging:
      driver: json-file
      options:
        max-size: '10m'
        max-file: '3'
    networks:
      - group-buy-network

  # 5. phpMyAdmin（只有在 webui profile 下才启动）
  phpmyadmin:
    image: phpmyadmin:5.2.1
    container_name: phpmyadmin
    hostname: phpmyadmin
    depends_on:
      mysql:
        condition: service_healthy
    ports:
      - '8899:80'
    environment:
      - PMA_HOST=mysql
      - PMA_PORT=3306
      - MYSQL_ROOT_PASSWORD=123456
    networks:
      - group-buy-network
    profiles: ["webui"]

  # 6. Redis Commander（只有在 webui profile 下才启动）
  redis-admin:
    image: spryker/redis-commander:0.8.0
    container_name: redis-admin
    hostname: redis-commander
    restart: always
    depends_on:
      redis:
        condition: service_healthy
    ports:
      - '8081:8081'
    environment:
      - REDIS_HOSTS=local:redis:6379
      - HTTP_USER=admin
      - HTTP_PASSWORD=admin
    networks:
      - group-buy-network
    profiles: ["webui"]
