Kafka Docker Compose Generator (KRaft)
A KRaft Docker Compose file with no ZooKeeper, and with the dual listener setup that makes it reachable from both another container and your own machine. That addressing is the single most common reason a Compose Kafka does not work.
docker-compose.yml
updates as you type Common mistakes
These are the ones that fail silently. The config is accepted, nothing raises an error, and the consequence arrives later.
Advertising a single listener
A broker reachable from inside the compose network is not reachable from the host, because the advertised host differs. Clients connect, get the advertised address and fail on the second call.
Instead:Define two listeners with different advertised addresses, one internal and one for the host.
Leaving internal topic replication at the default
__consumer_offsets defaults to a replication factor of three and cannot be created on a one-broker cluster.
Instead:Set the offsets and transaction log replication factors to 1.
Using the container name as the advertised host for external clients
It resolves inside the compose network and nowhere else, so anything on the host fails with an unresolvable address.
Instead:Advertise localhost with the mapped port for the external listener.
Why your Compose Kafka works from one place and not the other
One broker cannot advertise one address that works both inside and outside Docker. That single fact is behind almost every broken Kafka Compose file.
A container and your laptop need different addresses
A client discovers brokers through metadata, and the broker tells it whatever advertised.listeners says. From another container the right answer is kafka-1:9092, resolved on the Compose network. From your machine it is localhost:9092, because that is where the published port lands. There is no single string that is both, so a broker needs two listeners with two advertised addresses. With one listener the cluster works from exactly one of those two places and fails from the other, and the failure looks like a broker problem: the client connects to the bootstrap address, gets metadata pointing somewhere it cannot reach, and times out.
KAFKA_LISTENERS: INTERNAL://:9092,HOST://:9092
KAFKA_ADVERTISED_LISTENERS:
INTERNAL://kafka-1:9092,HOST://localhost:9092
# from a container: kafka-1:9092
# from your machine: localhost:9092
# both work, because the broker advertises both Internal topic replication has to fit the broker count
offsets.topic.replication.factor defaults to 3, so on a one or two broker Compose cluster __consumer_offsets cannot be created. The cluster starts, looks healthy, and the first consumer group fails to commit. This generator sets those factors to match the broker count, which means a config that works on one broker will not transfer to a real cluster unchanged, and the output says so.
No ZooKeeper, and no separate format step
The official apache/kafka image runs in KRaft mode and formats its log directory on first start when CLUSTER_ID is set, so there is no kafka-storage command to run. Confluent's cp-kafka uses different environment variable names and its own setup, so a compose file for one is not a compose file for the other. Every node in the cluster needs the same cluster id, and it should be a fresh one from kafka-storage.sh random-uuid rather than a copied example.
Without volumes, down means gone
KRaft keeps the cluster metadata in the log directory alongside the data, so losing that directory loses the formatted state as well as the messages. Without named volumes, docker compose down leaves you recreating the cluster rather than restarting it, which is fine for a throwaway and annoying in a test suite that expects to stop and start.
Two settings here are deliberately wrong for production
group.initial.rebalance.delay.ms is set to 0, which on a real cluster wastes rebalances during a fleet start and on a laptop just removes a three second wait from every test. Combined broker and controller mode puts the quorum on the nodes serving traffic, which is fine locally and not how a production cluster should be laid out. Both are marked in the output rather than left to be discovered when the file gets copied somewhere real.