Search code examples
dockerapache-kafkadocker-composeapache-zookeeper

Creating Kafka Topic with docker-compose


I am starting my first docker proyect, trying to setup zookeeper and Kafka.

I have the following docker-compose.yml:

version: "3.8"

networks:
    mynet:

services:

  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
   networks:
      - mynet
      
  kafka:
    image: wurstmeister/kafka:2.12-2.4.0
    ports:
      - "9092:9092"
    expose:
      - "9093"
    environment:
      KAFKA_LISTENERS: INSIDE://0.0.0.0:9093,OUTSIDE://0.0.0.0:9092
      KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka:9093,OUTSIDE://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      # Create a topic NAME:PARTITION:REPLICAS
      KAFKA_CREATE_TOPICS: "example-topic:1:1"
   networks:
      - mynet
      
  kafka-manager:
    image: sheepkiller/kafka-manager:latest
    environment:
      ZK_HOSTS: "zookeeper:2181"
    ports:
      - 9000:9000
    networks:
      - mynet

I execute the docker-compose.yml: sudo docker-compose up -d

To check if I have the topic created, I access the Kafka shell through: sudo docker exec -it <<CONTAINER ID>> sh

In this shell I go to: cd opt/kafka

And execute: bin/kafka-topics.sh --list --zookeeper localhost:2181

And the output is a timeout error:

Exception in thread "main" kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
    at kafka.zookeeper.ZooKeeperClient.$anonfun$waitUntilConnected$3(ZooKeeperClient.scala:259)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:253)
    at kafka.zookeeper.ZooKeeperClient.waitUntilConnected(ZooKeeperClient.scala:255)
    at kafka.zookeeper.ZooKeeperClient.<init>(ZooKeeperClient.scala:113)
    at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1858)
    at kafka.admin.TopicCommand$ZookeeperTopicService$.apply(TopicCommand.scala:321)
    at kafka.admin.TopicCommand$.main(TopicCommand.scala:54)
    at kafka.admin.TopicCommand.main(TopicCommand.scala)

Solution

  • Zookeeper isn't running in the Kafka container.

    You should use --bootstrap-server localhost:9092 anyway since the Zookeeper argument is deprecated