version: "3.8"

services:
  ### 1. 前端 -------------------------------------------------
  smile-picture-front:
    image: nginx:alpine
    container_name: smile-picture-front
    restart: unless-stopped
    ports:
      - "18096:80"
    volumes:
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
    networks: [smile-picture-network]

  ### 2. MySQL ------------------------------------------------
  mysql:
    image: mysql:8.0
    container_name: picture-mysql
    hostname: mysql
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: 123456
    ports:
      - "13307:3306"
    volumes:
      # 配置文件只读挂载 ✅
      - ./mysql/my.cnf:/etc/mysql/conf.d/mysql.cnf:ro
      # 初始化脚本只读也没问题 ✅
      - ./mysql/sql:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 5s
      timeout: 10s
      retries: 10
      start_period: 15s
    networks: [smile-picture-network]

  ### 3. Redis -----------------------------------------------
  redis:
    image: redis:6.2
    container_name: picture-redis
    hostname: redis
    restart: unless-stopped
    ports:
      - "36379:6379"
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf:ro
    command: redis-server /usr/local/etc/redis/redis.conf
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3
    networks: [smile-picture-network]

  ### 4. Java 后端 -------------------------------------------
  smile-picture-backend:
    build:
      context: ../../..           # 建议把 compose 放到项目根；这里就用 `.`
      dockerfile: Dockerfile
    image: smile/smile-picture-backend:latest
    container_name: smile-picture-backend
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:           # ← 推荐通过环境变量注入容器名而非 localhost
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/smile-picture?useSSL=false&serverTimezone=Asia/Shanghai
    ports:
      - "8096:8096"
    networks: [smile-picture-network]

networks:
  smile-picture-network:
    driver: bridge
