# ksqlDB Server LOCAL single-node stack, using confluentinc images.
# This Compose is mostly taken from https://ksqldb.io/quickstart.html#quickstart-content it includes 
#   - Zookeper and a Kafka broker for the basic Kafka setup
#   - Schema Registry for AVRO/Protobuff schema support
#   - ksqldb-server for the ksqlDB server, using the local Dockerfile
#   - akhq for the Kafka UI 
#   - ksqlDB-cli integrated, you can use it with `docker exec -it ksqldb-cli ksql http://ksqldb-server:8088`
# 
# if you want to use ksqldb-cli outside of compose, use `host.docker.internal:8088` as the server address.
services:
  zookeeper:
    image: confluentinc/cp-zookeeper
    platform: linux/amd64
    hostname: zookeeper
    container_name: zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    restart: unless-stopped

  broker:
    image: confluentinc/cp-kafka
    platform: linux/amd64
    hostname: broker
    container_name: broker
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 100
      KAFKA_JMX_PORT: 9101
      KAFKA_JMX_HOSTNAME: localhost
    restart: unless-stopped

  schema-registry:
    image: confluentinc/cp-schema-registry
    platform: linux/amd64
    hostname: schema-registry
    container_name: schema-registry
    depends_on:
      - broker
    ports:
      - "8081:8081"
    environment:
      SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: "broker:29092"
      SCHEMA_REGISTRY_HOST_NAME: schema-registry
      SCHEMA_REGISTRY_LISTENERS: "http://0.0.0.0:8081"
      SCHEMA_REGISTRY_COMPATIBILITY_LEVEL: backward
    restart: unless-stopped

  ksqldb-server:
    build:
      context: .
    hostname: ksqldb-server
    container_name: ksqldb-server
    depends_on:
      - broker
      - schema-registry
    ports:
      - "8088:8088"
      - "9095:9095"
    healthcheck:
        test: ["CMD-SHELL", "/usr/local/bin/healthcheck.sh"]
        interval: 5s
        timeout: 5s
        retries: 60
    environment:
      Environment: local
      KSQL_BOOTSTRAP_SERVERS: "broker:29092"
      KSQL_LISTENERS: http://0.0.0.0:8088
      KSQL_SECURITY_PROTOCOL: PLAINTEXT
      KSQL_KSQL_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
      KSQL_USE_QUERY_FILE: "false"
      KSQL_KSQL_STREAMS_REPLICATION_FACTOR: 1
      KSQL_KSQL_STREAMS_NUM_STREAM_THREADS: 1
      KSQL_KSQL_INTERNAL_TOPIC_REPLICAS: 1
      KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
      KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
      KSQL_HOST_NAME: localhost
      KSQL_JMX_OPTS: >
        -Djava.rmi.server.hostname=localhost
        -Dcom.sun.management.jmxremote
        -Dcom.sun.management.jmxremote.port=9095
        -Dcom.sun.management.jmxremote.authenticate=false
        -Dcom.sun.management.jmxremote.ssl=false
        -Dcom.sun.management.jmxremote.rmi.port=9095
    volumes:
      - ./connector_config.properties:/etc/kafka-connect/connector_config.properties

  akhq:
    image: tchiotludo/akhq
    platform: linux/amd64
    hostname: akhq
    container_name: akhq
    depends_on:
      - broker
    ports:
      - "8080:8080"
    environment:
      JVM_OPTS: -Xms64M -Xmx128M
      AKHQ_CONFIGURATION: |
        akhq:
          connections:
            local:
              properties:
                bootstrap.servers: "broker:29092"
              schema-registry:
                url: "http://schema-registry:8081"
                type: confluent
              ksqldb:
                - name: ksqldb-server
                  url: "http://ksqldb-server:8088"

  ksqldb-cli:
    image: confluentinc/ksqldb-cli:0.28.2
    container_name: ksqldb-cli
    depends_on:
      - broker
      - ksqldb-server
    entrypoint: /bin/sh
    tty: true
