services:
  db:
    image: mysql:8
    ports:
      - "43306:3306"
    volumes:
      - ./sql/django_vue.sql:/docker-entrypoint-initdb.d/django_vue.sql
      - ./data/mysql:/var/lib/mysql   # <-- 加这一行
    networks:
      - dj_admin_network
    env_file:
      - ./docker/.env.dev
      - ./docker/.env.local
    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost", "-pmy-secret-pw" ]
      interval: 5s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7
    restart: always
    ports:
      - "46379:6379"
    networks:
      - dj_admin_network
    env_file:
      - ./docker/.env.dev
      - ./docker/.env.local
  
  
  celery_worker:
    build:
      context: ./backend
      dockerfile: Dockerfile
    command: celery -A backend worker -l info
    env_file:
      - ./docker/.env.dev
      - ./docker/.env.local
    depends_on:
      - backend
      - redis
    networks:
      - dj_admin_network

  celery_beat:
    build:
      context: ./backend
      dockerfile: Dockerfile
    command: celery -A backend beat -l info
    env_file:
      - ./docker/.env.dev
      - ./docker/.env.local
    depends_on:
      - backend
      - redis
    networks:
      - dj_admin_network

  flower:
    build:
      context: ./backend
      dockerfile: Dockerfile
    command: celery -A backend flower --port=5555
    env_file:
      - ./docker/.env.dev
      - ./docker/.env.local
    depends_on:
      - backend
      - redis
    ports:
      - "45555:5555"
    networks:
      - dj_admin_network

  backend:
    build:
      context: ./backend
      target: dev
    volumes:
      - ./backend:/app
    ports:
      - "48000:8000"
    networks:
      - dj_admin_network
    env_file:
      - ./docker/.env.dev
      - ./docker/.env.local
    environment:
      DATABASE_HOST: "db"
    depends_on:
      db:
        condition: service_healthy
    command: >
      sh -c "python manage.py makemigrations &&
             python manage.py migrate &&
             daphne -b 0.0.0.0 backend.asgi:application"

  web:
    build:
      context: ./web
      target: dev
    volumes:
      - ./web:/app
    ports:
      - "45678:5678"
    depends_on:
      - backend
    networks:
      - dj_admin_network
    env_file:
      - ./docker/.env.dev
      - ./docker/.env.local

networks:
  dj_admin_network:
    driver: bridge