In docker-compose, my init-kafka image is failing to list topics on my kafka image. How do I fix this? (With KRaft, not Zookeeper!)

(I know this question is similar to this other one, but the other one uses Zookeeper which is deprecated.)

I have this simple docker-compose:

services:
  kafka:
    container_name: dotnetbus-kafka
    image: apache/kafka:3.8.0
    ports:
      - 9092:9092

  init-kafka:
    container_name: dotnetbus-init-kafka
    image: apache/kafka:3.8.0
    depends_on:
      - kafka
    command: sleep 1000

NOTE: I’m looking for a solution that uses KRaft, not Zookeeper (because the latter is deprecated).

Eventually, I want to put a create-topic command in the init-kafka images instead of the sleep, but first I need to get those commands to actually work.

When I shell into the init-kafka image and try to list topics, this happens:

$ /opt/kafka/bin/kafka-topics.sh --bootstrap-server dotnetbus-kafka:9092 --list
[2024-09-10 03:17:03,924] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (localhost/127.0.0.1:9092) could not be established. Node may not be available. (org.apache.kafka.clients.NetworkClient)
[2024-09-10 03:17:04,027] WARN [AdminClient clientId=adminclient-1] Connection to node 1 (localhost/127.0.0.1:9092) could not be established. Node may not be available. (org.apache.kafka.clients.NetworkClient)
...
(repeats infinitely)

I’ve tried a bunch of variations of that command. I must be understanding docker wrong, or Kafka wrong, or both.

However, when I run a similar command from my host machine (to localhost:9092) it works fine.

What am I missing? Why can’t I get init-kafka to reach kafka?

9

With some help from the comments and this example, I’ve got it working.

Things I learned:

  • For KRaft, the compose environment needs to configure a “controller” listener, in addition to any other listeners.

  • Kafka will treat the config as Zookeeper unless it sees KAFKA_CONTROLLER_QUORUM_VOTERS.

  • These last 4 vars on my broker image are absolutely necessary. I honestly don’t know what they do right now, but the default values didn’t work (subscriptions always failed), and the error messages I was getting were 100% not-helpful for me with my present knowledge:

    KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
    KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
    KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
    

My working compose config

The following compose config configures 3 listeners:

  • a controller listener on docker-internal port 29092
  • a broker listener on docker-internal port 19092
  • a broker listener on host-exposed port 9092

The kafka1 service is the broker, and kafka-init creates topics on it.

services:
  kafka1:
    container_name: dotnetbus-kafka
    image: apache/kafka:3.8.0
    ports:
      - 9092:9092
    environment:
      KAFKA_NODE_ID: 1
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: 'CONTROLLER:PLAINTEXT,INTERBROKER:PLAINTEXT,HOST:PLAINTEXT'
      KAFKA_ADVERTISED_LISTENERS: 'HOST://localhost:9092,INTERBROKER://kafka1:19092'
      KAFKA_PROCESS_ROLES: 'broker,controller'
      KAFKA_CONTROLLER_QUORUM_VOTERS: '1@kafka1:29092'
      KAFKA_LISTENERS: 'HOST://:9092,INTERBROKER://:19092,CONTROLLER://:29092'
      KAFKA_INTER_BROKER_LISTENER_NAME: 'INTERBROKER'
      KAFKA_CONTROLLER_LISTENER_NAMES: 'CONTROLLER'
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1

  kafka-init:
    container_name: dotnetbus-init-kafka
    image: apache/kafka:3.8.0
    depends_on:
      - kafka1
    command:
      - /bin/sh
      - -c
      - |
        sh /opt/kafka/bin/kafka-topics.sh --bootstrap-server dotnetbus-kafka:19092 --list
        echo -e 'Creating kafka topics'
        sh /opt/kafka/bin/kafka-topics.sh --bootstrap-server dotnetbus-kafka:19092 --create --if-not-exists --topic trade-request --replication-factor 1 --partitions 1
        sh /opt/kafka/bin/kafka-topics.sh --bootstrap-server dotnetbus-kafka:19092 --create --if-not-exists --topic trade-status --replication-factor 1 --partitions 1
        echo -e 'Successfully created the following topics:'
        sh /opt/kafka/bin/kafka-topics.sh --bootstrap-server dotnetbus-kafka:19092 --list

(Note: The depends_on ought to accompanied by a good health check, which I clearly haven’t done in this config.)

Your kafka listener needs configuring. See here for a working example.

Something like

  kafka:
    container_name: dotnetbus-kafka
    image: apache/kafka:3.8.0
    ports:
      - 9092:9092
    environment:
      KAFKA_LISTENERS: PLAIN://0.0.0.0:9092
      KAFKA_ADVERTISED_LISTENERS: PLAIN://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAIN:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAIN
      KAFKA_ZOOKEEPER_CONNECT: zookeeper
      KAFKA_BROKER_ID: 1
      KAFKA_SECURITY_PROTOCOL: PLAINTEXT

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật