2

I'm trying to run the bitnami/kafka Docker Compose multi-container application and create topics with bin/kafka_topics.sh as described in https://kafka.apache.org/quickstart. I'm running the following docker-compose.yml:

version: "2"

services:
  zookeeper:
    image: docker.io/bitnami/zookeeper:3.7
    ports:
      - "2181:2181"
    volumes:
      - "zookeeper_data:/bitnami"
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
  kafka:
    image: docker.io/bitnami/kafka:3
    ports:
      - "9092:9092"
    volumes:
      - "kafka_data:/bitnami"
    environment:
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    depends_on:
      - zookeeper

volumes:
  zookeeper_data:
    driver: local
  kafka_data:
    driver: local

but when I try to create a topic, I get a java.net.UnknownHostException error:

~/D/kafka_2.13-3.0.0> bin/kafka-topics.sh --create --partitions 1 --replication-factor 1 --topic quickstart-events --bootstrap-server localhost:9092
[2022-01-03 07:05:15,990] WARN [AdminClient clientId=adminclient-1] Error connecting to node 3d6fe6f02854:9092 (id: 1001 rack: null) (org.apache.kafka.clients.NetworkClient)
java.net.UnknownHostException: 3d6fe6f02854: nodename nor servname provided, or not known
    at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:933)
    at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519)
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:852)
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301)
    at org.apache.kafka.clients.DefaultHostResolver.resolve(DefaultHostResolver.java:27)
    at org.apache.kafka.clients.ClientUtils.resolve(ClientUtils.java:109)
    at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.currentAddress(ClusterConnectionStates.java:508)
    at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.access$200(ClusterConnectionStates.java:465)
    at org.apache.kafka.clients.ClusterConnectionStates.currentAddress(ClusterConnectionStates.java:170)
    at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:975)
    at org.apache.kafka.clients.NetworkClient.ready(NetworkClient.java:301)
    at org.apache.kafka.clients.admin.KafkaAdminClient$AdminClientRunnable.sendEligibleCalls(KafkaAdminClient.java:1117)
    at org.apache.kafka.clients.admin.KafkaAdminClient$AdminClientRunnable.processRequests(KafkaAdminClient.java:1377)
    at org.apache.kafka.clients.admin.KafkaAdminClient$AdminClientRunnable.run(KafkaAdminClient.java:1320)
    at java.base/java.lang.Thread.run(Thread.java:833)

It seems that Kafka is not resolving the host name, which I don't yet understand because I'm specifying it as localhost:9092 in both applications. Any idea how to get this to work?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • Can you try using `kafka:9092` as `--bootstrap-server`? – Turing85 Jan 03 '22 at 15:21
  • @Turing85 If I try to specify the `--bootstrap-server` as `kafka:9092` I get an `org.apache.kafka.common.errors.TimeoutException: Timed out waiting for a node assignment.`. Trying that also doesn't seem right to me because `kafka` is a host name on the Docker Compose network which `bin/kafka_topics.sh` doesn't know about. – Kurt Peek Jan 03 '22 at 15:28
  • 2
    There is something fishy with the setup... can you try defining `KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092` as env var for the kafka container, [as shown on the `dockerhub` page](https://hub.docker.com/r/bitnami/kafka/) under "*Apache Kafka development setup example*"? – Turing85 Jan 03 '22 at 15:41
  • It's not resolving the _advertised hostname_ that is returned by Zookeeper (which defaults to the Kafka container's imageId) – OneCricketeer Jan 03 '22 at 22:40

1 Answers1

0

I worked around this problem by instead connecting to Kafka from another contain as described in the "Connecting to other containers" section of https://hub.docker.com/r/bitnami/kafka/. I ran the following docker-compose.yml:

version: '2'

networks:
  app-tier:
    driver: bridge

services:
  zookeeper:
    image: 'bitnami/zookeeper:latest'
    environment:
      - 'ALLOW_ANONYMOUS_LOGIN=yes'
    networks:
      - app-tier
  kafka:
    image: 'bitnami/kafka:latest'
    environment:
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    networks:
      - app-tier
  myapp:
    image: 'bitnami/kafka:latest'
    environment:
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    networks:
      - app-tier

in a directory named bitnami-docker-kafka. Then I exec'd into the container and ran kafka-topics.sh --create specifying the --bootstrap-server as kafka:9092:

> docker exec -it bitnami-docker-kafka-myapp-1 /bin/bash
I have no name!@af986a978cae:/$ kafka-topics.sh --create --partitions 1 --replication-factor 1 --topic quickstart-events --bootstrap-server kafka:9092
Created topic quickstart-events.
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • Why do you need two Kafka containers? Just `docker exec -ti kafka` to create your topics and connect to `localhost:9092`. Your original problem was that you didn't set `KAFKA_CFG_LISTENERS=PLAINTEXT://0.0.0.0:9092` or expose any Docker container ports. Refer other README section that I wrote titled "Accessing Apache Kafka with internal and external clients" – OneCricketeer Jan 03 '22 at 22:37