Skip to content

Kafka on Docker

We will use the following file in a new directory. Call the file docker-compose.yaml

Next we can create the Docker container by using the following command which calls docker compose to create a new container based on the confluent.io image of Kafka. The -d tells Docker to run it in the background

Next we create a topic to write and read messages to and from

Once have a topic lets set up a listener to read messages that are published on the topic

This will create a consumer, however all you will see is a ‘>’ which is the Kafka prompt waiting for messages

Now we can start writing reading using a producer with the following command, In a separate terminal use the following commands

Go back to the producer in the first terminal and start writing messages, each line is a new message and appears in the consumer which as follows

Use Ctrl+C to exit both the consumer and the producer. Then to shutdown the container use