Docker & Setup
A 3-node KRaft cluster, listeners, and the tools around it
As with every module, there's a minimal compose file and a
production-shaped one in kafka/.
The minimal stack
services:
kafka:
image: apache/kafka:4.3.1
ports:
- "9092:9092"
volumes:
- kafka_data:/var/lib/kafka/data
volumes:
kafka_data:The official image's defaults start a single node that's both broker and
controller, listening on localhost:9092. Good enough to try the CLI:
cd kafka
docker compose up -d
docker compose exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --listA single node has no replication, so nothing about durability or failover can be learned on it. Stop it before starting the cluster, since both use port 9092.
The cluster
docker compose down
docker compose -f compose_prod.yaml up -d --buildThe brokers are built from broker/: the official image plus the
Prometheus JMX exporter agent, so each broker serves its internal
metrics on port 7071 (published as 7071-7073).
Cluster-wide defaults
Every broker gets the same settings, from a YAML anchor in the compose
file. The apache/kafka image turns each KAFKA_FOO_BAR variable into the
server property foo.bar:
Listeners: the part everyone gets wrong
Each broker has three listeners:
| Listener | Address | Used by |
|---|---|---|
INTERNAL | kafka-N:19092 | Other containers, and broker-to-broker replication |
EXTERNAL | localhost:9092 / 9094 / 9096 | Clients on your Mac |
CONTROLLER | kafka-N:29093 | KRaft quorum traffic, never exposed |
A client connects to the bootstrap address only to fetch metadata.
Then it connects to each partition leader at the address that broker
advertises. If a broker advertises kafka-2:19092 to a client on
your Mac, the bootstrap succeeds and then every produce fails, because
kafka-2 doesn't resolve outside Docker. That's why each listener has
its own KAFKA_ADVERTISED_LISTENERS entry: containers get Docker
hostnames, your Mac gets localhost.
So:
# from your Mac
--bootstrap-server localhost:9092,localhost:9094,localhost:9096
# from another container (the labs, Connect, Schema Registry)
--bootstrap-server kafka-1:19092,kafka-2:19092,kafka-3:19092The CLI
The Kafka tools are shell scripts inside every broker, in /opt/kafka/bin.
A shell function saves typing:
K() { docker exec kafka-1 /opt/kafka/bin/"$@"; }
K kafka-topics.sh --bootstrap-server localhost:19092 --list
K kafka-metadata-quorum.sh --bootstrap-server localhost:19092 describe --statusThe JMX agent is attached by broker/start-with-jmx.sh, only to the
broker process. An earlier version of this setup put it in KAFKA_OPTS
for the whole container, and every CLI tool then also tried to start the
agent on port 7071, failed, and exited. The brokers' healthchecks, which
use a CLI tool, failed with it. Worth knowing if you ever add Java
agents to Kafka.
The labs
docker compose -f compose_prod.yaml --profile labs build labs
docker compose -f compose_prod.yaml run --rm labs python producer_basics.pyEveryday commands
docker compose -f compose_prod.yaml ps
docker logs -f kafka-1
docker compose -f compose_prod.yaml --profile connect stop # stop, keep data
docker compose -f compose_prod.yaml --profile connect down -v # delete everythingOpen http://localhost:8086 for the UI. It's handy for browsing messages and groups; the lessons use the CLI so you learn what the UI is doing.