version: '3'
services:
  mysql:
    image: mysql:5.7
    container_name: mysql
    environment:
      # root 密码
      MYSQL_ROOT_PASSWORD: 123456789
      # 初始化数据库(后续的初始化sql会在这个库执行)
      MYSQL_DATABASE: 'campus_imaotai'
      TZ: Asia/Shanghai
    volumes:
      # 数据挂载
      - ./mysql/data/:/var/lib/mysql/
      # 配置挂载
      - ./mysql/conf/:/etc/mysql/conf.d/
      # 日志
      - ./mysql/logs:/logs
      # 直接导入数据库
      - ./mysql/campus_imaotai.sql:/docker-entrypoint-initdb.d/campus_imaotai.sql
    command: [
      'mysqld',
      '--innodb-buffer-pool-size=80M',
      '--character-set-server=utf8mb4',
      '--collation-server=utf8mb4_unicode_ci',
      '--default-time-zone=+8:00',
      '--lower-case-table-names=1'
    ]
    #为了不影响宿主机使用mysql端口，所以使用campus-net网络，只允许内部网络访问
    networks:
      - campus-net

  redis:
    image: redis:6.2.12
    container_name: redis
    environment:
      # 时区上海
      TZ: Asia/Shanghai
    volumes:
      # 配置文件
      - ./redis/conf:/redis/config
      # 数据文件
      - ./redis/data/:/redis/data/
    command: "redis-server /redis/config/redis.conf"
    #为了不影响宿主机使用redis端口，所以使用campus-net网络，只允许内部网络访问
    networks:
      - campus-net

  nginx-web:
    image: nginx:1.23.4
    container_name: nginx-web
    environment:
      # 时区上海
      TZ: Asia/Shanghai
    #为了不影响宿主机使用nginx端口，所以启用端口映射
    ports:
      - "880:80"
    volumes:
      # 页面目录
      - ./nginx/html:/usr/share/nginx/html
      - ./nginx/cert:/etc/nginx/cert
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/log:/var/log/nginx
    networks:
      - campus-net # 连接内部网络访问后端
      - imaotai    # 容器内也可以访问外部网络

  campus-server:
    image: oddfar/campus-imaotai
    container_name: campus-imaotai
    environment:
      TZ: Asia/Shanghai
      SERVER_PORT: 8160
      spring.config.additional-location: /home/campus/conf/application-prod.yml
    #为了不影响宿主机使用campus-server端口，所以启用端口映射
    ports:
     - "8160:8160"
    volumes:
      - ./server/conf:/home/campus/conf
    depends_on:
      - mysql
      - redis
    networks:
      - campus-net # 连接内部网络访问后端
      - imaotai    # 容器内也可以访问外部网络

networks:
  campus-net:
    driver: bridge
  imaotai:
    external: true    # 确保已创建的外部网络