0

I am trying to create a Kafka-Zookeeper network in Docker with the initial kafka topic.

I found different individual solutions and tried to mesh them into one docker-compose file.

In this sample Docker-compose file, zoo1, and kafka1 are the main container. init-kafka is trying to create an initial topic. But I get an error

Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available.

version: '3.7'
services:
  zoo1:
    image: confluentinc/cp-zookeeper:7.0.1
    hostname: zoo1
    container_name: zoo1
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_SERVER_ID: 1
      ZOOKEEPER_SERVERS: zoo1:2888:3888
    networks:
      - kafkanet_test
  kafka1:
    image: confluentinc/cp-kafka:7.0.1
    hostname: kafka1
    container_name: kafka1
    ports:
      - "9092:9092"
      - "9999:9999"
    environment:
      KAFKA_ADVERTISED_LISTENERS: LISTENER_DOCKER_INTERNAL://kafka1:19092,LISTENER_DOCKER_EXTERNAL://${DOCKER_HOST_IP:-127.0.0.1}:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: LISTENER_DOCKER_INTERNAL:PLAINTEXT,LISTENER_DOCKER_EXTERNAL:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: LISTENER_DOCKER_INTERNAL
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_BROKER_ID: 1
      KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO"
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_JMX_PORT: 9999
      KAFKA_JMX_HOSTNAME: ${DOCKER_HOST_IP:-127.0.0.1}
      KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.authorizer.AclAuthorizer
      KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: "true"
    depends_on:
      - zoo1
    networks:
      - kafkanet_test
  init-kafka:
    image: confluentinc/cp-kafka:7.0.1
    depends_on:
      - kafka1
    container_name: init-kafka
    entrypoint: [ '/bin/sh', '-c' ]
    command: |
      "
      # blocks until kafka is reachable
      kafka-topics --bootstrap-server kafka1:9092 --list

      echo -e 'Creating kafka topics'
      kafka-topics --bootstrap-server kafka1:9092 --create --if-not-exists --topic MY_INITIAL_TOPIC --replication-factor 1 --partitions 1

      echo -e 'Successfully created the following topics:'
      kafka-topics --bootstrap-server kafka1:9092 --list
      "
    networks:
      - kafkanet_test
networks:
  kafkanet_test:
    driver: "bridge"

Error:

init-kafka    | [2022-04-25 16:49:32,299] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
init-kafka    | [2022-04-25 16:49:33,321] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
init-kafka    | [2022-04-25 16:49:34,447] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
init-kafka    | [2022-04-25 16:49:35,348] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
init-kafka    | [2022-04-25 16:49:36,369] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
init-kafka    | [2022-04-25 16:49:37,321] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
init-kafka    | [2022-04-25 16:49:38,425] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

Can anyone please help me to solve, where exactly it is wrong here and how can I create a topic after initiating the kafka-broker.

TIA

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
Ariful Haque
  • 3,498
  • 4
  • 33
  • 58
  • 1
    You're using the wrong port. Look at `LISTENER_DOCKER_INTERNAL://kafka1:19092` and use that address in your other containers – OneCricketeer Apr 25 '22 at 19:14

0 Answers0