title: Getting "Broker may not be available" error when spring boot container tries to connect kafka container
body: <p>I am running into a problem with spring boot kafka docker, tried all the things mentioned in the below link but the issues still remain.</p>
  <p><a href="https://stackoverflow.com/questions/48245398/spring-boot-containers-can-not-connect-to-the-kafka-container">Spring Boot containers can not connect to the Kafka container</a></p>
  <p>Attaching my docker-compose.yml</p>
  <pre><code>version: '2'
  services:
    zookeeper:
      image: wurstmeister/zookeeper
      container_name: zookeeper
      ports:
      - &quot;2181:2181&quot;
    kafka:
      image: wurstmeister/kafka
      container_name: kafka
      ports:
      - &quot;9092:9092&quot;
      environment:
        KAFKA_ADVERTISED_HOST_NAME: kafka
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    my-app:
      image: my-app
      container_name: my-app
      ports:
        - &quot;8081:8081&quot;
      depends_on:
        - zookeeper
        - kafka
  </code></pre>
  <p>The error log</p>
  <pre><code>-1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
  my-app     | 2020-11-03 08:10:21.444  WARN 1 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
  </code></pre>
  <p>I am running wrustmiester/kafka and wrustmiester/zookeeper.
  If I run locally the spring boot app, everything runs smoothly.<br />
  The issue comes when I try to build an image of my spring boot app and run that image. Then the error is Broker may not be available.<br />
  Please if anyone can guide me.</p>
  <p>Adding my application.yml</p>
  <pre><code>spring:
    kafka:
      consumer:
        bootstrap-servers: localhost:9092
        group-id: group_id
        auto-offset-reset: earliest
        key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
        value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
        #org.apache.kafka.common.serialization.StringDeserializer
        properties.spring.json.trusted.packages: com.myapp.pojo
  
      producer:
        bootstrap-servers: localhost:9092
        key-serializer: org.apache.kafka.common.serialization.StringSerializer
        value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
        #org.apache.kafka.common.serialization.StringSerializer
  </code></pre>
  <p><strong>Updated my docker-compose.yml as</strong></p>
  <pre><code>version: '3'
  services:
    zookeeper:
      image: wurstmeister/zookeeper
      container_name: zookeeper
      network_mode: bridge
      ports:
      - &quot;2181:2181&quot;
    kafka:
      image: wurstmeister/kafka
      container_name: kafka
      network_mode: bridge
      ports:
      - &quot;9092:9092&quot;
      hostname: kafka
      environment:
        KAFKA_ADVERTISED_HOST_NAME: kafka
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
       # KAFKA_LISTENERS: INSIDE://:9092,OUTSIDE://:9094
       # KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
       # KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
        KAFKA_ADVERTISED_PORT: 9092
      links:
        - zookeeper
    my-app:
      image: my-app
      container_name: my-app
      network_mode: bridge
      ports:
        - &quot;8081:8081&quot;
      depends_on:
        - zookeeper
        - kafka
  </code></pre>
  <p><strong>Updated my application.yml as</strong></p>
  <pre><code>spring:
    kafka:
      consumer:
        bootstrap-servers: kafka:9092
        group-id: group_id
        auto-offset-reset: earliest
        key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
        value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
        #org.apache.kafka.common.serialization.StringDeserializer
        properties.spring.json.trusted.packages: com.myapp.pojo
  
      producer:
        bootstrap-servers: kafka:9092
        key-serializer: org.apache.kafka.common.serialization.StringSerializer
        value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
        #org.apache.kafka.common.serialization.StringSerializer
  </code></pre>
  <p><strong>Error log is same</strong></p>
  <pre><code>WARN 1 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
  my-app     | 2020-11-03 17:05:39.585  WARN 1 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
  </code></pre>