The following commands will start a container with Kafka and Zookeeper running on mapped ports 2181 (Zookeeper) and 9092 (Kafka).
docker pull spotify/kafka
docker run -d -p 2181:2181 -p 9092:9092 --env ADVERTISED_HOST=kafka --env ADVERTISED_PORT=9092 --name kafka spotify/kafka
ADVERTISTED_HOST
was set to kafka
, which will allow other containers to be able to run Producers and Consumers.
Setting ADVERTISED_HOST
to localhost
, 127.0.0.1
, or 0.0.0.0
will work great only if Producers and Consumers are started within the kafka
container itself, or if you are using DockerForMac (like me) and you want to run Producers and Consumers from OSX.
These are far less interesting use cases though, so we'll start Producers and Consumers from other containers.
We need to use an IP address or hostname in order for the kafka
service to be reachable from another container. IP address is not known before the container is started, so we have to choose a hostname, and I chose kafka
in this example.
docker exec kafka /opt/kafka_2.11-0.10.1.0/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
output:
Created topic "test".
docker exec kafka /opt/kafka_2.11-0.10.1.0/bin/kafka-topics.sh --list --zookeeper localhost:2181
output:
test
This command will run an unnamed instance of spotify/kafka
linked to the kafka
service, start a producer, and wait for newline-delimited input until you quit (which destroys the container):
docker run -it --rm --link kafka spotify/kafka /opt/kafka_2.11-0.10.1.0/bin/kafka-console-producer.sh --broker-list kafka:9092 --topic test
This command will start an unnamed instance of spotify/kafka
linked to the kafka
service, start a consumer, display existing messages from the test
topic, and wait for new messages until you quit (which destroys the container):
docker run -it --rm --link kafka spotify/kafka /opt/kafka_2.11-0.10.1.0/bin/kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic test --from-beginning
Send some newline-delimited messages in the Producer terminal window. The messages appear in the Consumer terminal window.
Thanks for sharing this one it really helps a lot.
One thing I noticed is when running consumer, my terminal window was flood with this message
WARN Error while fetching metadata with correlation id 6 : {testing=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
and typing something in producer window also emit this kind of message.
I'm able to resolve the issue by adding
--hostname
when starting the kafka service. [stackoverflow]