title: Docker networking cannot connect to local IP in Container
body: <p>Bit of a newb when it comes to networking...</p>
  <ul>
  <li>I have a Kafka cluster (1 master and 1 worker) running on 2 Vbox VMs on my laptop using Docker Swarm. This VM has a bridge adapter that is set to single IP address. The exact YAML is</li>
  </ul>
  <pre><code>enp0s8:
        dhcp4: no
        renderer: networkd
        addresses: [192.168.0.50/24]
        gateway4: 192.168.1.1
        nameservers:
          addresses: [8.8.8.8, 8.8.4.4]
  </code></pre>
  <ul>
  <li>I try to run a local Python script which connects to my Kafka cluster. This works when I do it on my laptop, however when I run the same Python script in a Docker container, it does not work!</li>
  </ul>
  <p>This pretty much narrows it down to a networking issue. I have tried running the container using <code>--net=host</code> and it still doesn't work. Confused as to why my Docker container cannot connect to the IP address of the VM but locally on my macbook it runs fine.</p>
  <p>Edit:</p>
  <p>To give further clarifications on the Python script...
  The entire script is here:</p>
  <pre><code>from kafka.consumer import KafkaConsumer
  
  def create_kafka_consumer(topic, group_id, brokers):
      return KafkaConsumer(topic,
                           bootstrap_servers=brokers,
                           auto_offset_reset='earliest',
                           enable_auto_commit=True,
                           group_id=group_id,
                           value_deserializer=lambda x: x.decode('utf-8'))
  
  GROUP_ID = 'test'
  def main():
      topic ='airbnb'
      c = create_kafka_consumer(topic, 'tester', '192.168.0.51:9093')
      for msg in c:
          print(msg)
  
  if __name__ == '__main__':
      main()
  </code></pre>
  <p>The IP address and port is for the Kafka WORKER node (which is running in the Virtual Box), not master node (IP address listed above). I set the IP in the VM and the port was manually set in the docker-compose file.</p>
  <p>Successful output example (just prints all the kafka logs):</p>
  <blockquote>
  <p>ConsumerRecord(topic='airbnb', partition=0, offset=169, timestamp=1598650807052, timestamp_type=0, key=None, value='adivinen quien cogio un airbnb para salir de la rutina y tiene que trabajar el weekend? ', headers=[], checksum=None, serialized_key_size=-1, serialized_value_size=111, serialized_header_size=-1)
  ConsumerRecord(topic='airbnb', partition=0, offset=170, timestamp=1598650821359, timestamp_type=0, key=None, value='This x100%', headers=[], checksum=None, serialized_key_size=-1, serialized_value_size=10, serialized_header_size=-1)</p>
  </blockquote>
  <p>Unsuccessful output:</p>
  <blockquote>
  <p>Traceback (most recent call last):
  File &quot;tmp/consumer-kafka.py&quot;, line 21, in 
  main()
  File &quot;tmp/consumer-kafka.py&quot;, line 16, in main
  c = create_kafka_consumer(topic, 'tester', '192.168.0.51:9093')
  File &quot;tmp/consumer-kafka.py&quot;, line 11, in create_kafka_consumer
  value_deserializer=lambda x: x.decode('utf-8'))
  File &quot;/usr/local/lib/python3.6/site-packages/kafka/consumer/group.py&quot;, line 355, in <strong>init</strong>
  self._client = KafkaClient(metrics=self._metrics, **self.config)
  File &quot;/usr/local/lib/python3.6/site-packages/kafka/client_async.py&quot;, line 242, in <strong>init</strong>
  self.config['api_version'] = self.check_version(timeout=check_timeout)
  File &quot;/usr/local/lib/python3.6/site-packages/kafka/client_async.py&quot;, line 925, in check_version
  raise Errors.NoBrokersAvailable()
  kafka.errors.NoBrokersAvailable: NoBrokersAvailable</p>
  </blockquote>
  <p>The Kafka worker <code>netstat -ntl</code> command output:
  <a href="https://i.stack.imgur.com/QS2NO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QS2NO.png" alt="enter image description here" /></a></p>