In the previous tutorial, I showed you how to install Apache Kafka on macOS. Installing Apache Kafka using Docker Compose will help us quickly start up an Apache Kafka server without much effort, just install Docker and a docker-compose.yml file. How is it in detail? In this tutorial, I will show you how to install Apache Kafka using Docker Compose!
I will create a new docker-compose.yml file and declare 2 services, one for Apache Zookeeper and one for Apache Kafka, as follows:
1 2 3 4 |
services: zookeeper: kafka: |
Because Apache Kafka wants to run, it must have Apache Zookeeper running, so I will declare the zookeeper service first.
We will use the Apache Kafka and Apache Zookeeper Docker images from the Confluent Platform https://www.confluent.io/ with the Docker Hub address https://hub.docker.com/u/confluentinc. In a nutshell, Confluent Platform is an extension of Apache Kafka, built from Apache Kafka along with other useful tools and services that make it easy for us to run and use Apache Kafka! There is no Docker Official Image from Apache Kafka so we can use Docker Images from this Confluent Platform.
The content of the zookeeper service is as follows:
1 2 3 4 5 6 |
zookeeper: image: confluentinc/cp-zookeeper:latest environment: ZOOKEEPER_CLIENT_PORT: 2181 ports: - 2181:2181 |
ZOOKEEPER_CLIENT_PORT is a required environment variable to define the port that clients can connect to Apache Zookeeper. In our case, it’s Apache Kafka! Here, I also expose port 2181 which is the default port of Apache Zookeeper.
And the kafka service has the following content:
1 2 3 4 5 6 7 8 9 |
kafka: image: confluentinc/cp-kafka:latest depends_on: - zookeeper environment: KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:29092 ports: - 29092:29092 |
KAFKA_ZOOKEEPER_CONNECT is used to define the Apache ZooKeeper server that Apache Kafka will connect to, and KAFKA_ADVERTISED_LISTENERS is used to expose Apache Kafka outside the container so that clients can connect to it. These are the required environment variables that you must declare when starting a Docker Container from cp-kafka Docker Image!
We also declare networks like this:
1 2 3 |
networks: huongdanjava: driver: bridge |
The entire content of the docker-compose.yml file is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
version: '3.8' services: zookeeper: image: confluentinc/cp-zookeeper:latest environment: ZOOKEEPER_CLIENT_PORT: 2181 ports: - 2181:2181 kafka: image: confluentinc/cp-kafka:latest depends_on: - zookeeper environment: KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:29092 ports: - 29092:29092 networks: huongdanjava: driver: bridge |
If now, you run the command “docker compose up” in the directory containing this docker-compose.yml file, you will see the following results:
Now you can connect to this Apache Kafka server to use it.