How to configure docker-compose.yml for Kafka local development?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
For local Kafka development today, the main shift is that you usually want KRaft mode, not the older ZooKeeper-based layout. A good docker-compose.yml for local use should solve two practical problems: start a single broker easily, and advertise listener addresses correctly for both containers and tools running on your host machine.
Use a Single-Broker KRaft Setup for Local Work
For local experimentation, one broker is usually enough. Confluent's current Docker documentation shows KRaft combined mode examples for local experimentation, which is much simpler than teaching a new project to juggle ZooKeeper as well.
Here is a useful single-broker compose file:
This is not production topology. It is the right level of complexity for local development.
Understand the Listener Split
The most important part of local Kafka-in-Docker setup is the listener configuration.
In the example above:
- '
PLAINTEXT://broker:29092is for other containers on the Docker network' - '
PLAINTEXT_HOST://localhost:9092is for clients running on your host machine'
That split matters because Kafka does not just accept a connection. It also tells clients where to reconnect. If advertised.listeners is wrong, the broker may start successfully while every client still fails in confusing ways.
Generate a Real Cluster ID
The CLUSTER_ID in examples is a placeholder. Generate your own before starting the stack.
Copy the output into CLUSTER_ID. If you reuse the same one locally, that is fine. The important part is not leaving it blank.
Start and Test the Broker
Bring the stack up:
Then create a topic and produce test data from inside the container:
This validates both the broker startup and the host-exposed listener path.
Prefer Local Simplicity over False Production Imitation
A common mistake is building an overly "realistic" local stack too early. For day-to-day development, you usually do not need multiple brokers, replication factors above 1, or a full platform stack unless the feature under test depends on them.
If you later need Connect, Schema Registry, or more brokers, add them intentionally. Do not make the basic local path harder than necessary.
Confluent also documents a confluent-local image optimized for local development. That is worth considering if you want an even faster start, but the explicit cp-kafka example above is helpful when you want to understand and control the listeners directly.
Common Pitfalls
- Using an old ZooKeeper-based compose file for a new local setup.
- Setting
advertised.listenersincorrectly so clients receive the wrong hostname. - Forgetting that one-broker local setups need replication-related settings such as
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1. - Copying a sample
CLUSTER_IDwithout understanding that it should be generated and managed intentionally. - Overcomplicating local development with a production-like topology before it is needed.
Summary
- For local Kafka development, prefer a single-broker KRaft setup.
- Listener configuration is the most important part of a working Docker-based setup.
- Use separate advertised listeners for Docker-network clients and host-machine clients.
- Set replication-related internal topics to
1in a one-broker environment. - Keep the local compose file simple until your use case truly requires more services.
Related reading
- How to configure kafka consumer with sasl mechanism PLAIN and with security protocol SASL_SSL in java?
- How to configure Kafka to behave like a FiFo queue?
- How to configure kafka topic retention policy during creation with Spring?
- How to configure logging for Kafka producers?
- How to connect ASP.Net Core to a SQL Server Docker container on Mac
- How to connect local kafka in docker container?
- How to configure RabbitMQ connection with spring-rabbit?
- How to configure RabbitMQ connection with spring-rabbit?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.