title: Spring boot container cannot find kafka broker in another container
body: <p>I have a simple springboot app running in docker container with following configuration.</p>
  
  <pre><code>@Configuration
  public class ProducerConfiguration {
  
      private static final String KAFKA_BROKER = "kafka:9092";
  
      @Bean
      public ProducerFactory&lt;String, String&gt; producerFactory() {
          return new DefaultKafkaProducerFactory&lt;&gt;(producerConfigurations());
      }
  
      @Bean
      public Map&lt;String, Object&gt; producerConfigurations() {
          Map&lt;String, Object&gt; configurations = new HashMap&lt;&gt;();
  
          configurations.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_BROKER);
          configurations.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
          configurations.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
  
          return configurations;
      }
  
      @Bean
      public KafkaTemplate&lt;String, String&gt; kafkaTemplate() {
          return new KafkaTemplate&lt;&gt;(producerFactory());
      }
  
  }
  </code></pre>
  
  <p>Controller class</p>
  
  <pre><code>@RestController
  public class KafkaController {
  
      private static final Logger LOGGER= LoggerFactory.getLogger(KafkaController.class);
  
      private KafkaTemplate&lt;String, String&gt; template;
  
      public KafkaController(KafkaTemplate&lt;String, String&gt; template) {
          this.template = template;
      }
  
      @GetMapping("/kafka/produce")
      public void produce(@RequestParam String message) {
          template.send("test_topic", message);
  
  
      }
  }
  </code></pre>
  
  <p>In the same project, following is the docker-compose.yml for kafka</p>
  
  <pre><code>version: '2.1'
  
  services:
    app:
      restart: always
      build:
        context: ./
        dockerfile: Dockerfile
      image: app
      container_name: app
  
      ports:
        - 8080
      depends_on:
        - kafka
        - zookeeper
      networks:
        - network1
  
    kafka:
      image: wurstmeister/kafka
      container_name: 'kafka'
      environment:
        JVM_OPTS: -Xmx1g -Xms1g -XX:MaxPermSize=1024m
        KAFKA_ADVERTISED_HOST_NAME: kafka
        KAFKA_ADVERTISED_PORT: 9092
        KAFKA_CREATE_TOPICS: test_topic:1:1
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
        KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
        KAFKA_LISTENERS: INSIDE://kafka:9093,OUTSIDE://0.0.0.0:9092
        KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
        KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka:9093,OUTSIDE://localhost:9092
  
      depends_on:
        - zookeeper
      ports:
        - 9092:9092
        - 9093:9093
      networks:
        - network1
  
  
    zookeeper:
      container_name: 'zookeeper'
      image: wurstmeister/zookeeper
  
      networks:
        - network1
  
  networks:
    network1:
  
  </code></pre>
  
  <p>The problem:
  When I run the springboot application locally and try to send the message to topic, it works fine.but, when I dockerize the springboot application using <code>docker build -t my/app</code> and then <code>docker run --publish=8080:8080  my/app</code> and then try to send the message on topic I get the following error</p>
  
  <p><code>[Producer clientId=producer-1] Connection to node -1 (kafka/127.0.0.1:9092) could not be established. Broker may not be available.</code></p>
  
  <p>I have tried various suggestion around KAFKA_ADVERTISED_HOST_NAME but still its not working.
  <em>Appreciate any help on this</em>.</p>
  
  <p>EDITS : my dockerfile is </p>
  
  <pre><code>FROM maven:3.5.2-jdk-8-alpine AS MAVEN_BUILD
  COPY pom.xml /build/
  COPY src /build/src/
  WORKDIR /build/
  RUN mvn package
  FROM openjdk:8-jre-alpine
  WORKDIR /app
  COPY --from=MAVEN_BUILD /build/target/app-0.0.1-SNAPSHOT.jar /app/
  ENTRYPOINT ["java", "-jar", "app-0.0.1-SNAPSHOT.jar"]
  </code></pre>