Kafka containers with Docker Compose
July 18, 2023For Compose basics, see the Docker Compose overview. This post covers a single-node Kafka broker in KRaft mode with Kafka UI. Zookeeper is no longer required; it was removed in Kafka 4.0.
Prerequisites
- Docker Compose installed
Configuration
The following configuration spins up a single-node Kafka broker in KRaft mode with the Kafka UI tool.
The Kafka broker address is localhost:29092, and Kafka UI is available at http://localhost:8085.
# docker-compose.ymlservices:kafka:image: apache/kafka:4.3.0hostname: kafkaports:- '29092:29092'environment:KAFKA_NODE_ID: 1KAFKA_PROCESS_ROLES: broker,controllerKAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:29093KAFKA_LISTENERS: CONTROLLER://:29093,PLAINTEXT://:9092,PLAINTEXT_HOST://:29092KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXTKAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXTKAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLERKAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1CLUSTER_ID: MkU3OEVBNTcwNTJENDM2Qkkafka-ui:image: provectuslabs/kafka-ui:latestports:- 8085:8080depends_on:- kafkaenvironment:KAFKA_CLUSTERS_0_NAME: localKAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092
Run the following command to spin up the containers.
docker compose up
Demo
Docker Compose files for this post live in the kafka-docker-compose folder. Get access via code demos.