homeresume
 
   
🔍

Kafka containers with Docker Compose

July 18, 2023

For 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.yml
services:
kafka:
image: apache/kafka:4.3.0
hostname: kafka
ports:
- '29092:29092'
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:29093
KAFKA_LISTENERS: CONTROLLER://:29093,PLAINTEXT://:9092,PLAINTEXT_HOST://:29092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
CLUSTER_ID: MkU3OEVBNTcwNTJENDM2Qk
kafka-ui:
image: provectuslabs/kafka-ui:latest
ports:
- 8085:8080
depends_on:
- kafka
environment:
KAFKA_CLUSTERS_0_NAME: local
KAFKA_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.