This guide runs Apache Kafka using Docker Compose with KRaft mode (no ZooKeeper required).
Note: Kafka 4.0+ requires KRaft mode. ZooKeeper mode has been removed.
cat <<'YAML' > docker-compose.yml
services:
kafka:
image: apache/kafka:4.2.0
container_name: kafka
ports:
- "9092:9092"
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:
YAML
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
# Produce a message
docker exec kafka /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic
>Hello Kafka!
# Consume the message
docker exec kafka /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning --timeout-ms 5000
See Apache Kafka Configuration for configuration guidance.
Any questions?
Feel free to contact us. Find all contact information on our contact page.
Prefer automation? See Apache Kafka Ansible Setup for an example playbook.
Prefer containers? See Apache Kafka Docker Setup.
See Apache Kafka Security for hardening guidance.