0

I have the following docker-compose.yml file

version: '2.1'

networks:
  app-tier:
    driver: bridge

services:

  zookeeper:
    image: 'bitnami/zookeeper:3'
    container_name: zookeeper
    ports:
      - '2181:2181'
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
    networks:
      - app-tier

  kafka:
    image: 'bitnami/kafka:2'
    container_name: kafkaContainer
    ports:
      - '9092:9092'
    environment:
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      - KAFKA_CFG_LISTENERS=PLAINTEXT://kafkaContainer:29092,PLAINTEXT_HOST://kafkaContainer:9092
      - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafkaContainer:29092,PLAINTEXT_HOST://localhost:9092
      - KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT
      - KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=true
    depends_on:
      - "zookeeper"
    links:
      - zookeeper
    healthcheck:
      test:
        ["CMD", "kafka-topics.sh", "--list", "--zookeeper", "zookeeper:2181"]
      interval: 30s
      timeout: 10s
      retries: 4
    networks:
      - app-tier

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: 'db'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'user'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'user123'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'user123'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3306:3306'
    expose:
      # Opens port 3306 on the container
      - '3306'
      # Where our data will be persisted
    volumes:
      - my-db:/var/lib/mysql
    depends_on:
      - "kafka"
    links:
      - kafka

# Names our volume
volumes:
  my-db:
    

I'm having a hard time interacting with Kafka from the command line. I can create and read topics without any issues like:

$ docker exec -it kafkaContainer kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 3 --topic test1_skh

WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both. Created topic test1_skh.

$ docker exec -it kafkaContainer kafka-topics.sh --list --zookeeper zookeeper:2181 12s

test1_skh

But when I interact with the producer, I get the following:

$ docker exec -it kafkaContainer kafka-console-producer.sh --topic test1_skh --bootstrap-server localhost:9092

[2022-07-28 22:25:34,609] WARN [Producer clientId=console-producer] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

But if I change the server to kafka:9092 then it lets me add a message, but after the first one it gives the same error again.

Similarly, with the consumer:

$ docker exec -it kafkaContainer kafka-console-consumer.sh --topic test1_skh --from-beginning --bootstrap-server localhost:9092

[2022-07-28 22:28:26,844] WARN [Consumer clientId=consumer-console-consumer-91144-1, groupId=console-consumer-91144] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

I would appreciate anyone's help.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
luismir15
  • 19
  • 4
  • 1
    You're using the wrong port. Should be `docker exec -it kafkaContainer ... --bootstrap-server localhost:29092`. Or you can use the Kafka CLI tools on your host, and **there** it is `localhost:9092` without using `docker exec`. Note: You can use `docker-compose exec kafka` instead. And `kafka-topics` accepts `--bootstrap-server` as well (zookeeper flag is deprecated) – OneCricketeer Jul 29 '22 at 18:23
  • @OneCricketeer thanks for the help and clarification. Unfortunately using localhost:29092 still doesn't work and I get the same error. Also, kafka-topics oddly does not run if I use the bootstrap-server flag but it does for the zookeeper. – luismir15 Aug 01 '22 at 19:22
  • 1
    I got it to work by using bootstrap-server kafkaContainer:29092 – luismir15 Aug 01 '22 at 19:54
  • `KAFKA_CFG_LISTENERS` is the problem for having `localhost` not work. You should use `KAFKA_CFG_LISTENERS=PLAINTEXT://:29092,PLAINTEXT_HOST://0.0.0.0:9092` – OneCricketeer Aug 02 '22 at 13:49

0 Answers0