Kafka Tutorial Docker Mastery: Run Kafka in Containers with Ease

Logan Jul 13, 2026

Embarking on your journey to understand Apache Kafka, a robust streaming platform, involves navigating through its complexities. This is where Docker comes into play, providing a lightweight and portable environment for running Kafka, making it easier to set up, manage, and deploy. Let's dive in and explore how to leverage Docker for Kafka in this comprehensive guide.

Alex Xu (@alexxubyte) on X
Alex Xu (@alexxubyte) on X

First, let's understand why Docker is an asset when working with Kafka. Docker allows you to package your application along with its dependencies, ensuring a consistent environment across different systems.

the top 5 kafka use cases
the top 5 kafka use cases

Setting Up Kafka with Docker

Before we start, ensure you have Docker installed on your system. For Windows and macOS users, Docker Desktop provides an easy installation process. For Linux users, follow the official Docker documentation to set up Docker.

the diagram shows how kafka works and what it is like to work with them
the diagram shows how kafka works and what it is like to work with them

Now, let's pull the official Kafka Docker image and set up a single-node Kafka cluster.

Pulling the Kafka Image

the docker containers info sheet
the docker containers info sheet

Run the following command to pull the latest Kafka image from Docker Hub:

<code>docker pull confluentinc/cp-kafka</code>

Running a Single-Node Kafka Cluster

Now, let's run a single-node Kafka cluster using Docker. Execute the following command:

Kafka intro
Kafka intro

<code>docker run -d --name kafka -e KAFKA-Advertised.Listeners=PLAINTEXT://localhost:9092 -p 29092:29092 -p 9092:9092 confluentinc/cp-kafka</code>

Here, we expose ports 29092 and 9092 for external access. The Kafka broker is configured to listen on localhost:9092. We also advertise the listener as being on localhost:9092 to allow the ZooKeeper to connect.

Producing and Consuming Data

With our Kafka broker set up, let's explore producing and consuming data using Docker.

Docker
Docker

Producing Data

We'll use `kafka-console-producer` to produce data. Run the following command in a new terminal:

Working with Docker Images
Working with Docker Images
Docker concepts
Docker concepts
the docker components flow chart
the docker components flow chart
Top 7 Udemy Courses to Learn Docker and Kubernetes in 2025 - Best of Lot [UPDATED]
Top 7 Udemy Courses to Learn Docker and Kubernetes in 2025 - Best of Lot [UPDATED]
the diagram shows how to use kafka for data processing and storage in an application
the diagram shows how to use kafka for data processing and storage in an application
Activity | Nitin Gupta | LinkedIn
Activity | Nitin Gupta | LinkedIn
a diagram showing how to use the kaka fast appliance for an application
a diagram showing how to use the kaka fast appliance for an application
Understanding Kafka Topics and Partitions
Understanding Kafka Topics and Partitions
the docker registration process is shown in this diagram, which shows how to use it
the docker registration process is shown in this diagram, which shows how to use it
A fast and easy Docker tutorial for beginners (video series)
A fast and easy Docker tutorial for beginners (video series)
System Design Basics - Apache Kafka vs RabbitMQ vs ActiveMQ
System Design Basics - Apache Kafka vs RabbitMQ vs ActiveMQ
Docker container mastery roadmap
Docker container mastery roadmap
Kafka Internals - Apache Kafka
Kafka Internals - Apache Kafka
Apache Kafka Infographic
Apache Kafka Infographic
Dockerfile Essentials - Building Your First Images
Dockerfile Essentials - Building Your First Images
a poster with instructions on how to use docker basics
a poster with instructions on how to use docker basics
DevSecOps Guides on LinkedIn: #docker #cotainer #hardening #devops #devsecops #appsec #infrastructure
DevSecOps Guides on LinkedIn: #docker #cotainer #hardening #devops #devsecops #appsec #infrastructure
the top 6 kaka use cases are shown in this graphic above it is an image of
the top 6 kaka use cases are shown in this graphic above it is an image of
the diagram shows how to use docker workflow for web pages and other projects
the diagram shows how to use docker workflow for web pages and other projects
the differences between kafka and buffers are shown in this graphic above it
the differences between kafka and buffers are shown in this graphic above it

<code>docker run -it --net host confluentinc/cp-kafka:latest kafka-console-producer --bootstrap-server localhost:29092 --topic my-topic</code>

Consuming Data

Now, let's consume the data using `kafka-console-consumer`. Open another terminal and run:

<code>docker run -it --net host confluentinc/cp-kafka:latest kafka-console-consumer --bootstrap-server localhost:29092 --topic my-topic --from-beginning</code>

Hit enter in the producer's terminal to start sending messages. You'll notice these messages being received by the consumer.

Scaling Kafka with Docker

Docker allows you to easily scale your Kafka cluster. Here's how you can bring up additional brokers:

Create a docker-compose.yml file in your project directory:

<code>version: '3'
services:
  kafka1:
    image: 'confluentinc/cp-kafka'
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA demokratized.HEAP_OPTS: -Xmx100M -Xms100M
    expose:
      - "9092"
  kafka2:
    image: 'confluentinc/cp-kafka'
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9093
      KAFKA_BROKER_ID: 2
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKAHeAP_OPTS: -Xmx100M -Xms100M
    expose:
      - "9093"</code>

Then, start your scaled Kafka cluster with:

<code>docker-compose up -d</code>

Mastering Kafka with Docker opens up a world of possibilities, offering a consistent and portable environment for testing, developing, and deploying your Kafka applications.

Stay curious, and Happy Kafka-ing! 🐳📦📜