title: How to reach docker container `localhost` from Mac?
body: <p>Note this is different from <a href="https://stackoverflow.com/questions/40383804">How to expose a service running inside a docker container, bound to localhost</a>, which can be addressed in multiple ways in Docker for Linux, say through <code>--net host</code> or even <code>-v</code> to bind my Linux-flavor client in etc. My problem is specific for Docker for Mac, so it's not as straightforward.</p>
  
  <p>I have a TCP server binding to <code>localhost:5005</code> running inside Docker for Mac. (For security reason, I must not bind to <code>0.0.0.0:5005</code>.)</p>
  
  <p>I have a TCP client sending request to this server from my Mac (not inside the docker container).</p>
  
  <p>My question is, how do I make it work?</p>
  
  <p>In Linux Docker, I would simply use <code>--net=host</code> so the server binds to my host <code>lo</code> interface, but it seems that Docker for Mac runs on a managed VM, so the <code>host</code> network behavior is different behavior.</p>
  
  <p>To illustrate my point:</p>
  
  <p><strong>On MacBook</strong></p>
  
  <p>It simply would not work</p>
  
  <pre><code>[me@MacBook App]$ docker run -v `pwd`:/App -p 127.0.0.1:5005:5005 nitincypher/docker-ubuntu-python-pip /App/server.py
  [me@MacBook App]$ ./client.py 
  Client received data: 
  </code></pre>
  
  <p><strong>On Linux</strong></p>
  
  <p>In comparison, it would be trivial to do on Linux by using <code>host</code> network mode. Since I'm using my Linux's <code>lo</code> interface as my container <code>lo</code> interface.</p>
  
  <pre><code>[me@Linux App]$ docker run -v `pwd`:/App --net=host nitincypher/docker-ubuntu-python-pip /App/server.py
  Server Connection address: ('127.0.0.1', 52172)
  Server received data: Hello, World!
  [me@Linux App]$ ./client.py 
  Client received data: Hello, World!
  </code></pre>
  
  <p><strong>My Simulated Server Code</strong></p>
  
  <p>Requirement: It MUST bind to <code>localhost</code>, and nothing else. So I cannot change it to <code>0.0.0.0</code>.</p>
  
  <pre><code>#!/usr/bin/env python
  
  import socket
  
  TCP_IP = 'localhost'
  TCP_PORT = 5005
  BUFFER_SIZE = 20  # Normally 1024, but we want fast response
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((TCP_IP, TCP_PORT))
  s.listen(1)
  
  conn, addr = s.accept()
  print 'Server Connection address:', addr
  while 1:
      data = conn.recv(BUFFER_SIZE)
      if not data: break
      print "Server received data:", data
      conn.send(data)  # echo
  conn.close()
  </code></pre>
  
  <p><strong>My Simulated Client Code</strong></p>
  
  <p>Requirement: It MUST be ran on MacBook, since the real client is written in CPP and compiled to run only on MacBook.</p>
  
  <pre><code>#!/usr/bin/env python
  
  import socket
  
  
  TCP_IP = 'localhost'
  TCP_PORT = 5005
  BUFFER_SIZE = 1024
  MESSAGE = "Hello, World!"
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((TCP_IP, TCP_PORT))
  s.send(MESSAGE)
  data = s.recv(BUFFER_SIZE)
  s.close()
  
  print "Client received data:", data
  </code></pre>