This guide uses Docker to run Apache Kafka with KRaft mode (no ZooKeeper required).
For Docker installation, see Docker.
Create a directory to store your configuration and compose files.
mkdir -p /opt/kafka
cd /opt/kafka
Define a container for Apache Kafka using the official image with KRaft mode.
Note: Kafka 4.0+ requires KRaft mode. ZooKeeper mode has been removed.
services:
kafka:
image: apache/kafka:4.2.0
container_name: kafka
ports:
- "9092:9092" # Client connections
environment:
- KAFKA_CFG_NODE_ID=1
- KAFKA_CFG_PROCESS_ROLES=broker,controller
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka:9093
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXT
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=true
volumes:
- kafka_data:/var/lib/kafka/data
restart: unless-stopped
volumes:
kafka_data:
Start the container in the background.
docker compose up -d
Check container status:
docker compose ps
Test the broker:
# Create a test topic
docker exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic test-topic --partitions 1 --replication-factor 1
# List topics
docker exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
apache/kafka is the official Apache Kafka Docker imagekafka_data volume persists broker data and metadataAny questions?
Feel free to contact us. Find all contact information on our contact page.