title: Connect to Kafka running in Docker
body: <p>I setup a single node Kafka Docker container on my local machine like it is described in <a href="https://docs.confluent.io/current/installation/docker/docs/installation/recipes/single-node-client.html" rel="noreferrer">the Confluent documentation</a> (steps 2-3).</p>
  
  <p>In addition, I also exposed Zookeeper's port 2181 and Kafka's port 9092 so that I'll be able to connect to them from a client running on local machine:</p>
  
  <pre><code>$ docker run -d \
      -p 2181:2181 \
      --net=confluent \
      --name=zookeeper \
      -e ZOOKEEPER_CLIENT_PORT=2181 \
      confluentinc/cp-zookeeper:4.1.0
  
  $ docker run -d \
      --net=confluent \
      --name=kafka \
      -p 9092:9092 \
      -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
      -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092 \
      -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
      confluentinc/cp-kafka:4.1.0
  </code></pre>
  
  <p><strong>Problem:</strong> When I try to connect to Kafka from the host machine, the connection fails because it <code>can't resolve address: kafka:9092</code>.</p>
  
  <p>Here is my Java code:</p>
  
  <pre><code>Properties props = new Properties();
  props.put("bootstrap.servers", "localhost:9092");
  props.put("client.id", "KafkaExampleProducer");
  props.put("key.serializer", LongSerializer.class.getName());
  props.put("value.serializer", StringSerializer.class.getName());
  KafkaProducer&lt;Long, String&gt; producer = new KafkaProducer&lt;&gt;(props);
  ProducerRecord&lt;Long, String&gt; record = new ProducerRecord&lt;&gt;("foo", 1L, "Test 1");
  producer.send(record).get();
  producer.flush();
  </code></pre>
  
  <p>The exception:</p>
  
  <pre><code>java.io.IOException: Can't resolve address: kafka:9092
      at org.apache.kafka.common.network.Selector.doConnect(Selector.java:235) ~[kafka-clients-2.0.0.jar:na]
      at org.apache.kafka.common.network.Selector.connect(Selector.java:214) ~[kafka-clients-2.0.0.jar:na]
      at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:864) [kafka-clients-2.0.0.jar:na]
      at org.apache.kafka.clients.NetworkClient.ready(NetworkClient.java:265) [kafka-clients-2.0.0.jar:na]
      at org.apache.kafka.clients.producer.internals.Sender.sendProducerData(Sender.java:266) [kafka-clients-2.0.0.jar:na]
      at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:238) [kafka-clients-2.0.0.jar:na]
      at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:176) [kafka-clients-2.0.0.jar:na]
      at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144]
  Caused by: java.nio.channels.UnresolvedAddressException: null
      at sun.nio.ch.Net.checkAddress(Net.java:101) ~[na:1.8.0_144]
      at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622) ~[na:1.8.0_144]
      at org.apache.kafka.common.network.Selector.doConnect(Selector.java:233) ~[kafka-clients-2.0.0.jar:na]
      ... 7 common frames omitted
  </code></pre>
  
  <p><strong>Question:</strong> How to connect to Kafka running in Docker? My code is running from host machine, not Docker. </p>
  
  <p>Note: I know that I could theoretically play around with DNS setup and <code>/etc/hosts</code> but it is a workaround - it shouldn't be like that.</p>
  
  <p>There is also similar question <a href="https://stackoverflow.com/q/32302444/2065796">here</a>, however it is based on <code>ches/kafka</code> image. I use <code>confluentinc</code> based image which is not the same.</p>