title: Sprint boot kafka Consumer can not connect to the kafka container
body: <p>I'm trying to deploy 2 Spring boot Apps (kafka Producer and Consumer). When I deploy the Producer to docker is all ok but when I deploy my Consumer doesn't work because doesn't have the connection with kafka container.</p>
  
  <p>The log show me this error</p>
  
  <pre><code>2019-11-17 05:32:22.644  WARN 1 --- [main] o.a.k.c.NetworkClient: [Consumer clientId=consumer-1, groupId=exampleGroup] Connection to node -1 could not be established. Broker may not be available.
  </code></pre>
  
  <p>my docker-compose.yml is</p>
  
  <pre><code>version: '3'
  
  services:
  
    zookeeper:
      image: wurstmeister/zookeeper
      container_name: zookeeper
      restart: always
      ports:
        - 2181:2181
  
    kafka:
      image: wurstmeister/kafka
      container_name: kafka
      restart: always
      ports:
        - 9092:9092
      depends_on:
        - zookeeper
      links:
        - zookeeper:zookeeper
      environment:
        KAFKA_ADVERTISED_HOST_NAME: localhost
        KAFKA_ADVERTISED_PORT: 9092
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
        KAFKA_CREATE_TOPICS: "topic1:1:1"
  </code></pre>
  
  <p>On my KafkaConfig class:</p>
  
  <pre><code>@EnableKafka
  @Configuration
  public class KafkaConfig {
  
      @Bean
      public ConsumerFactory&lt;String, String&gt; consumerFactory(){
          Map&lt;String, Object&gt; config = new HashMap&lt;&gt;();
  
          config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaConstants.KAFKA_BROKERS);
          config.put(ConsumerConfig.GROUP_ID_CONFIG, "exampleGroup");
          config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
          config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        //  config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, KafkaConstants.ENABLE_AUTO_COMMIT_CONFIG);
          config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, KafkaConstants.OFFSET_RESET_EARLIER);
         // config.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, KafkaConstants.SESSION_TIMEOUT_MS);
  
          return new DefaultKafkaConsumerFactory&lt;&gt;(config);
      }
  </code></pre>
  
  <p>And the constants class</p>
  
  <pre><code>public class KafkaConstants {
  
      public static String KAFKA_BROKERS = "localhost:9092";
      public static Integer MESSAGE_COUNT=1000;
      public static String TOPIC_NAME="demo";
      public static String GROUP_ID_CONFIG="exampleGroup";
      public static Integer MAX_NO_MESSAGE_FOUND_COUNT=100;
      public static String OFFSET_RESET_LATEST="latest";
      public static String OFFSET_RESET_EARLIER="earliest";
      public static Integer MAX_POLL_RECORDS=1;
      public static Integer SESSION_TIMEOUT_MS = 180000;
      public static Integer REQUEST_TIMEOUT_MS_CONFIG = 181000;
      public static String ENABLE_AUTO_COMMIT_CONFIG = "false";
      public static Integer AUTO_COMMIT_INTERVAL_MS_CONFIG = 8000;
  }
  </code></pre>
  
  <p>When I install zookepper and kafka on my computer and run this 2 spring boot apps with intellij works fine. the problem is when I deploy to my local docker.</p>
  
  <p>Can you please help me?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Updating my docker-compose:</p>
  
  <pre><code>version: '3'
  
  services:
  
    zookeeper:
      image: wurstmeister/zookeeper
      container_name: zookeeper
      restart: always
      ports:
        - 2181:2181
  
    kafka:
      image: wurstmeister/kafka
      container_name: kafka
      restart: always
      ports:
        - 9092:9092
      depends_on:
        - zookeeper
      links:
        - zookeeper:zookeeper
      environment:
        KAFKA_ADVERTISED_HOST_NAME: kafka
        KAFKA_ADVERTISED_PORT: 9092
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
        KAFKA_CREATE_TOPICS: "ACC_GROUP_CREATE:1:1"
  
    consumer:
      image: micro1
      container_name: micro1
      depends_on:
        - kafka
      restart: always
      ports:
        - 8088:8088
      depends_on:
        - kafka
      links:
        - kafka:kafka
  
    producer:
      image: micro2
      container_name: micro2
      depends_on:
        - kafka
      restart: always
      ports:
        - 8087:8087
      depends_on:
        - kafka
      links:
        - kafka:kafka
  </code></pre>
  
  <p>Works fine! based on the response of @hqt but I don't know why I need to add these lines of Consumer/producer</p>