Setup Kafka Zookeeper in Docker

Here's a quick tutorial on how to setup Apache Kafka and Zookeeper in docker.



Setting up Apache Kafka and Zookeeper in docker is easy thanks to popular DockerFiles like wurstmeister/kafka-docker. Let’s go through a quick setup of Kafka and Zookeeper in docker on a single node system.

Prerequisites

  • Linux System
  • Git
  • Docker
  • Docker-compose

Kafka and Zookeeper

Start by cloning the repo:

git clone https://github.com/wurstmeister/kafka-docker

Then change to the kafka-docker directory:

cd kafka-docker

Now let’s edit the docker-compose.yml file. Change the Kafka Advertised Hostname to localhost:

KAFKA_ADVERTISED_HOST_NAME: localhost

Expose the Kafka port to the system so that apps outside of the docker network can talk to it:

  kafka:
    build: .
    ports:
      - "9092:9092"

Finally, under the Kafka environment variables, add any topics that you want Kafka to automatically create:

KAFKA_CREATE_TOPICS: "cat:1:1"

The first number after the topic refers to the partition, and the second number refers to the replica set. Save that file and build the containers with docker-compose.

sudo docker-compose up --build -d

You should now see both containers running by using the docker ps command.

docker ps

That’s it! You now have Apache kafka and zookeeper in docker running on your system.