Certainly! Below is a comprehensive docker-compose.yml file that sets up both Apache Zookeeper and Apache Kafka using Docker Compose. This setup is ideal for development and testing purposes. It leverages the official Confluent Docker images, which are widely used and well-supported.
docker-compose.yml
version: '3.8'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ZOOKEEPER_SYNC_LIMIT: 2
ports:
- "2181:2181"
kafka:
image: confluentinc/cp-kafka:latest
container_name: kafka
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
Explanation of the Configuration
1. Version
version: '3.8'
Specifies the version of the Docker Compose file format. Version 3.8 is compatible with Docker Engine 19.03.0+.
2. Services
a. Zookeeper
zookeeper:
image: confluentinc/cp-zookeeper:latest
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ZOOKEEPER_SYNC_LIMIT: 2
ports:
- "2181:2181"
- Image: Uses the latest Confluent Zookeeper image.
- Container Name: Names the container
zookeeper for easy reference.
- Environment Variables:
ZOOKEEPER_CLIENT_PORT: Port on which Zookeeper listens for client connections.
ZOOKEEPER_TICK_TIME: Basic time unit in milliseconds used by Zookeeper.
ZOOKEEPER_SYNC_LIMIT: Number of ticks that can pass between sending a request and getting an acknowledgement.
- Ports: Maps container port
2181 to host port 2181.
b. Kafka
kafka:
image: confluentinc/cp-kafka:latest
container_name: kafka
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
- Image: Uses the latest Confluent Kafka image.
- Container Name: Names the container
kafka.
- Depends On: Ensures that Zookeeper starts before Kafka.
- Ports: Maps container port
9092 to host port 9092.
- Environment Variables:
KAFKA_BROKER_ID: Unique identifier for the Kafka broker.
KAFKA_ZOOKEEPER_CONNECT: Connection string for Zookeeper (zookeeper:2181 refers to the Zookeeper service defined above).
KAFKA_ADVERTISED_LISTENERS: Advertised address for clients to connect (PLAINTEXT://localhost:9092 is suitable for local development).
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: Sets the replication factor for the offset topic. Since we're running a single broker, this is set to 1.
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR and KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: Configure transaction logs, set to 1 for single broker setups.
Steps to Run
-
Ensure Docker and Docker Compose Are Installed
-
Save the docker-compose.yml File
Create a new directory for your Kafka project and save the above docker-compose.yml file within it.
mkdir kafka-docker
cd kafka-docker
vim docker-compose.yml
# Paste the content and save
-
Start the Services
Run the following command to start Zookeeper and Kafka:
docker-compose up -d
The -d flag runs the containers in detached mode.
-
Verify the Setup
-
Check Running Containers
docker-compose ps
You should see both zookeeper and kafka containers up and running.
-
Test Kafka Functionality
You can use Kafka's command-line tools to produce and consume messages.
Produce a Message:
docker exec -it kafka kafka-console-producer --broker-list localhost:9092 --topic test
Type a message and press Enter.
Consume Messages:
In another terminal, run:
docker exec -it kafka kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
You should see the message you produced earlier.
-
Stopping the Services
When you're done, you can stop the containers with:
docker-compose down
Additional Tips
-
Persisting Data: For a production setup, consider adding volumes to persist Zookeeper and Kafka data.
volumes:
- zookeeper-data:/var/lib/zookeeper
- kafka-data:/var/lib/kafka/data
# At the bottom of the file
volumes:
zookeeper-data:
kafka-data:
-
Environment Variables: Adjust environment variables as needed for your specific use case, such as configuring security settings, topic configurations, etc.
-
Using KRaft Mode: Newer Kafka versions support running without Zookeeper (KRaft mode). If you prefer this setup, you'll need a different docker-compose.yml. Let me know if you need guidance on that!
Alternative: Using Bitnami's Kafka and Zookeeper Images
If you prefer using Bitnami's Docker images, here's an alternative docker-compose.yml:
version: '3.8'
services:
zookeeper:
image: bitnami/zookeeper:latest
container_name: zookeeper
ports:
- "2181:2181"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: bitnami/kafka:latest
container_name: kafka
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
- KAFKA_BROKER_ID=1
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_LISTENERS=PLAINTEXT://:9092
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092
- KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
- ALLOW_PLAINTEXT_LISTENER=yes
This configuration is similar but uses Bitnami's images, which are known for their ease of use and robust defaults.
Feel free to customize the docker-compose.yml according to your project's requirements. Using Docker Compose simplifies the orchestration of multi-container applications, making it easier to manage and scale your Kafka infrastructure.