Install Apache Kafka using Docker Compose

In the previous tutorial, I showed you how to install Apache Kafka on macOS, installing using Docker Compose will help us quickly start up an Apache Kafka server without much effort, just install Docker and the 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 with Docker Compose version as follows:

We will declare 2 services, one for Apache Zookeeper and one for Apache Kafka:

Because Apache Kafka wants to run, it must have Apache Zookeeper running, so I will declare the zookeeper service first.

We will be using 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:

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 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 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.

Add Comment