Starting a Kafka topics using Docker Compose with spotify/kafka?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a powerful distributed streaming platform that enables you to build real-time data pipelines and streaming applications. Deploying Kafka using Docker can simplify the setup and maintenance process. In this article, we'll discuss how to start Kafka topics using Docker Compose with the Spotify Kafka image.
Overview of Apache Kafka and Docker Compose
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. It is designed for high throughput and scalability. Docker, on the other hand, is a platform for developing, shipping, and running applications inside lightweight containers.
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you create a docker-compose.yml file to configure your application's services, networks, and volumes. Then, using a single command, you create and start all the services defined in your configuration.
Using spotify/kafka
The Spotify Kafka Docker image (spotify/kafka) bundles Kafka and Zookeeper in a single container. This configuration suits development and testing but may not be ideal for production environments, where Zookeeper and Kafka typically run on separate systems or clusters for performance and reliability.
Prerequisites
To follow this setup, you need:
- Docker installed on your machine
- Docker Compose installed on your machine
Configuration Details
First, you need to create a docker-compose.yml file that describes the Kafka service you want to run. Here is an example of such a configuration:
Explanation:
ADVERTISED_HOSTandADVERTISED_PORTare the host and port that Kafka advertises to producers and consumers.TOPICSinitializes a Kafka topic — in this case,my-topic.PARTITIONSsets the number of partitions within the topic.REPLICATION_FACTORsets the replication factor of the topic (1 means no replication).
Starting the Container
You can start the Kafka container via Docker Compose with the below command:
By running this command, Docker Compose reads the docker-compose.yml, pulls the necessary Docker images, and starts the defined services.
Creating Additional Kafka Topics
If you need to create additional topics after your container is up, you can use the Kafka topic command:
Here, <kafka-container-id> should be replaced with the actual ID of the Kafka container, which can be found using docker ps.
Interaction with Kafka
To produce messages:
To consume messages:
Summary Table
| Component | Description | Configuration Example |
| Kafka | Distributed streaming platform | "my-topic", 1 partition, no replication |
| Docker | Platform for containerization | Container running Kafka and Zookeeper |
| Docker Compose | Tool for defining and running multi-container applications | Defined in docker-compose.yml |
spotify/kafka | Docker image containing both Kafka and Zookeeper | Used in the example provided above |
| Topics | Categories or feed names where records are stored | Can be created on startup or using command line |
This table provides an overview of the key components and settings related to our Kafka deployment using Docker.
Conclusion
Setting up Kafka using Docker Compose with spotify/kafka streamlines the development and testing of Kafka applications. This consolidated approach can also aid in educational environments or when prototyping. However, for production environments, it's recommended to use separate, scalable configurations for Kafka and Zookeeper to ensure better reliability and performance.
Related reading
- Starting Kafka Server Permanently
- Stateful and Stateless consumer on Kafka
- Stop a Kafka Streams app
- stopping spark streaming after reading first batch of data
- Starting a shell in the Docker Alpine container
- STL way to access more elements at the same time in a loop over a container
- Static outgoing IP in Kubernetes
- Static outgoing IP in Kubernetes

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.