'Unable to connect to kafka in docker from Spring boot in host machine

I have already gone though Robin Moffat's blog, and several SO posts on the same subject, but still my configuration doesnt work.

My docker-compose.yml:

  kafka:
    image: confluentinc/cp-kafka:6.1.2
    container_name: kafka
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
#      KAFKA_LISTENERS: INSIDEDOCKER://kafka:9092,HOSTMACHINE://kafka:29092 # doesnt work
      KAFKA_LISTENERS: INSIDEDOCKER://kafka:9092,HOSTMACHINE://0.0.0.0:29092 # also doesnt work 
      KAFKA_ADVERTISED_LISTENERS: INSIDEDOCKER://kafka:9092,HOSTMACHINE://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDEDOCKER:PLAINTEXT,HOSTMACHINE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDEDOCKER
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "false"
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1

And my bootstrap servers value in Springboot app:

kafka.bootstrap.servers=localhost:29092

I still get errors like:

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

Can someone point out what I am missing?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source