services:

  # ============================================================
  # HADOOP NAMENODE
  # ============================================================
  namenode:
    image: apache/hadoop:3.3.6
    container_name: namenode
    hostname: namenode
    user: root
    environment:
      - HADOOP_HOME=/opt/hadoop
      - CLUSTER_NAME=ids-cluster
    command: >
      bash -c "
        hdfs namenode -format -force -nonInteractive 2>/dev/null || true &&
        hdfs namenode
      "
    ports:
      - "9870:9870"
      - "9000:9000"
    volumes:
      - ./hadoop/config/core-site.xml:/opt/hadoop/etc/hadoop/core-site.xml
      - ./hadoop/config/hdfs-site.xml:/opt/hadoop/etc/hadoop/hdfs-site.xml
      - ./hadoop/config/yarn-site.xml:/opt/hadoop/etc/hadoop/yarn-site.xml
      - namenode-data:/hadoop/dfs/name
    networks: [ids-net]
    mem_limit: 2g
    restart: unless-stopped

  # ============================================================
  # ZOOKEEPER
  # ============================================================
  zookeeper:
    image: confluentinc/cp-zookeeper:7.5.0
    container_name: zookeeper
    hostname: zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    networks: [ids-net]
    mem_limit: 512m
    restart: unless-stopped

  # ============================================================
  # KAFKA
  # ============================================================
  kafka:
    image: confluentinc/cp-kafka:7.5.0
    container_name: kafka
    hostname: kafka
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
      - "9093:9093"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,EXTERNAL://0.0.0.0:9093
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,EXTERNAL://172.29.5.1:9093
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,EXTERNAL:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
    networks: [ids-net]
    mem_limit: 2g
    restart: unless-stopped

  # ============================================================
  # SPARK MASTER
  # ============================================================
  spark-master:
    image: spark-with-numpy:3.5.0
    container_name: spark-master
    hostname: spark-master
    command: /opt/spark/bin/spark-class org.apache.spark.deploy.master.Master
    dns:
      - 8.8.8.8
      - 8.8.4.4
    environment:
      - SPARK_PUBLIC_DNS=10.0.0.15
    ports:
      - "8080:8080"
      - "7077:7077"
      - "37100:37100"
      - "37101:37101"
    volumes:
      - ./models:/models
      - ./spark:/opt/spark-apps
      - ./spark-jars:/opt/spark-jars
    networks: [ids-net]
    mem_limit: 2g
    restart: unless-stopped

networks:
  ids-net:
    driver: bridge

volumes:
  namenode-data:
