title: Unable to connect from Java to kafka running inside docker container
body: <p>I am very new to kafka and docker, and I want to connect from Java to Kafka running inside the docker container, but I get an error, and it seems to be a lack of networking knowledge from my side. </p>
  
  <p>I have defined an env variable: <code>KAFKA_HOSTNAME=kafka</code></p>
  
  <p>Here is my <code>docker-compose.yml</code> file:</p>
  
  <pre><code>version: '1.1'
  
  networks:
    sb:
      driver: bridge
  
  services:
    zookeeper:
      image: confluentinc/cp-zookeeper:latest
      container_name: zookeeper
      hostname: zookeeper
      networks:
       - sb
      ports:
        - "2181:2181"
      environment:
        ZOOKEEPER_CLIENT_PORT: 2181
        ZOOKEEPER_TICK_TIME: 2000
  
    kafka:
      image: confluentinc/cp-kafka:latest
      container_name: kafka
      hostname: ${KAFKA_HOSTNAME:-kafka}
      depends_on:
        - zookeeper
      networks:
       - sb
      ports:
        - "9092:9092"
      environment:
        KAFKA_BROKER_ID: 1
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
        KAFKA_ADVERTISED_HOST_NAME: ${KAFKA_HOSTNAME:-kafka}
        KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://${KAFKA_HOSTNAME:-kafka}:9092
        KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  </code></pre>
  
  <p>When I run the <code>docker-compose up</code>, I can see that the zookeeper and the Kafka broker are started successfully:</p>
  
  <pre><code>CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                        NAMES
  60460c26ef86        confluentinc/cp-kafka:latest       "/etc/confluent/dock…"   19 minutes ago      Up 19 minutes       0.0.0.0:9092-&gt;9092/tcp                       kafka
  0d1fd4000a83        confluentinc/cp-zookeeper:latest   "/etc/confluent/dock…"   19 minutes ago      Up 19 minutes       2888/tcp, 0.0.0.0:2181-&gt;2181/tcp, 3888/tcp   zookeeper
  </code></pre>
  
  <p>Here is my simple java program I try to run:</p>
  
  <pre><code>private static final String bootstrapServers = "192.168.0.102:9092";
  
  //create producer properties
  Properties properties = new Properties();
  properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
  properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
  properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
  
  //create producer
  KafkaProducer&lt;String, String&gt; producer = new KafkaProducer&lt;String, String&gt;(properties);
  
  //producer record
  ProducerRecord&lt;String, String&gt; record = new ProducerRecord("first_topic", "hello from java");
  
  //send data
  producer.send(record);
  
  
  producer.flush();
  producer.close();
  </code></pre>
  
  <p>In the docker logs I can see that it is creating the <code>first-topic</code> inside the kafka container, but in the Java program I get the following error:</p>
  
  <p><code>[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Error connecting to node kafka:9092 (id: 1 rack: null)
  java.net.UnknownHostException: kafka</code></p>
  
  <p>I have a few questions:
  1. What am I missing?
  2. How should I correctly set the <code>KAFKA_ADVERTISED_HOST_NAME</code> property in docker-compose file? Is it correct to hardcode the IP?
  3. How should I correctly set the <code>KAFKA_ADVERTISED_LISTENERS:</code> property?</p>
  
  <p>Thank you very much!</p>