Install Apache Kafka using Docker Compose

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:

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:

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:

The KAFKA_ZOOKEEPER_CONNECT environment variable is used to define the Apache ZooKeeper server that Apache Kafka will connect to, and the variable KAFKA_ADVERTISED_LISTENERS here, I define 2 types of listeners:

  • PLAINTEXT://kafka:29092 to expose the port of Apache Kafka for Docker Containers in the same network to connect to.
  • PLAINTEXT_HOST://localhost:9092 is used to expose Apache Kafka outside the container so that clients can connect to it.

For each listener, you need to define the security protocol https://kafka.apache.org/38/javadoc/org/apache/kafka/common/security/auth/SecurityProtocol.html, using the KAFKA_LISTENER_SECURITY_PROTOCOL_MAP environment variable. In my example, I use PLAINTEXT for both listeners, which means no need to authenticate, no need to encrypt!

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:

The entire content of the docker-compose.yml file is as follows:

If you now 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.

Add Comment