version: "3.8"

services:
  postgres:
    image: postgres:16-alpine
    container_name: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: flower
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
    restart: unless-stopped

  pgbouncer:
    image: edoburu/pgbouncer:latest
    container_name: pgbouncer
    ports:
      - "6432:6432"
    volumes:
      - ./deploy/pgbouncer/pgbouncer.ini:/etc/pgbouncer/pgbouncer.ini
      - ./deploy/pgbouncer/userlist.txt:/etc/pgbouncer/userlist.txt
    depends_on:
      - postgres
    restart: unless-stopped

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: admin
      PGADMIN_CONFIG_SERVER_MODE: "False"
    ports:
      - "5050:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      - postgres
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    restart: unless-stopped

  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    restart: unless-stopped

  web:
    build:
      context: .
      target: base
    container_name: flower_web
    command: python manage.py runserver 0.0.0.0:8100
    ports:
      - "8100:8100"
    volumes:
      - .:/app
    environment:
      DJANGO_SETTINGS_MODULE: flower.settings
      PYTHONPATH: /app
      DEBUG: "1"
      ALLOWED_HOSTS: "localhost,127.0.0.1,0.0.0.0,8.148.215.233,yuwenerp.yuwen.cloud"
      DB_HOST: pgbouncer
      DB_PORT: "6432"
      DB_NAME: flower
      DB_USER: postgres
      DB_PASSWORD: postgres
      CACHE_HOST: redis
      CACHE_PORT: "6379"
      # Celery Broker（RabbitMQ）连接：容器内用服务名访问；宿主机运行时默认 localhost:5672
      CELERY_BROKER_HOST: rabbitmq
      # Celery Result Backend（Redis）连接：容器内用服务名访问；宿主机运行时默认 localhost:6379
      CELERY_RESULT_HOST: redis
    depends_on:
      - pgbouncer
      - redis
      - rabbitmq
    restart: unless-stopped

  celery_worker:
    build:
      context: .
      target: base
    container_name: celery_worker
    command:
      - celery
      - -A
      - flower
      - worker
      - -l
      - info
      - --concurrency=2
      - --pool=solo
    volumes:
      - .:/app
    depends_on:
      - pgbouncer
      - redis
      - rabbitmq
    environment:
      PYTHONPATH: /app
      DB_HOST: pgbouncer
      DB_PORT: "6432"
      DB_NAME: flower
      DB_USER: postgres
      DB_PASSWORD: postgres
      # 缓存（Redis）连接：容器内用服务名访问
      CACHE_HOST: redis
      CACHE_PORT: "6379"
      # Celery 连接配置：容器内用服务名访问
      CELERY_BROKER_HOST: rabbitmq
      CELERY_RESULT_HOST: redis
    restart: unless-stopped

  celery_beat:
    build:
      context: .
      target: base
    container_name: celery_beat
    command:
      - celery
      - -A
      - flower
      - beat
      - -l
      - info
      - --schedule
      - /var/lib/celery/celerybeat-schedule
    volumes:
      - .:/app
      - celery_beat_data:/var/lib/celery
    depends_on:
      - pgbouncer
      - redis
      - rabbitmq
    environment:
      DJANGO_SETTINGS_MODULE: flower.settings
      PYTHONPATH: /app
      DB_HOST: pgbouncer
      DB_PORT: "6432"
      DB_NAME: flower
      DB_USER: postgres
      DB_PASSWORD: postgres
      # 缓存（Redis）连接：容器内用服务名访问
      CACHE_HOST: redis
      CACHE_PORT: "6379"
      # Celery 连接配置：容器内用服务名访问
      CELERY_BROKER_HOST: rabbitmq
      CELERY_RESULT_HOST: redis
    restart: unless-stopped

volumes:
  db_data:
  redis_data:
  pgadmin_data:
  rabbitmq_data:
  celery_beat_data:

