title: Docker proxy for MySQL: "Lost connection" message from host machine
body: <h1>What I want</h1>
  
  <p>There is the <a href="https://github.com/jwilder/nginx-proxy" rel="nofollow noreferrer">https://github.com/jwilder/nginx-proxy</a> package. This is very useful for "HTTP", but I want to use it to TCP too. (MySQL)</p>
  
  <p>Here are my changes: <a href="https://github.com/fchris82/nginx-proxy/commit/33d80ea4d4be5d511e4dab0413d516770aa15262" rel="nofollow noreferrer">https://github.com/fchris82/nginx-proxy/commit/33d80ea4d4be5d511e4dab0413d516770aa15262</a></p>
  
  <p>As you can see, I have added <code>stream {}</code> block to <code>nginx.conf</code> and the <code>/etc/nginx/stream.conf.d</code> directory. Here is the generated <code>default.conf</code> for stream block:</p>
  
  <pre><code>access_log off;
  error_log /var/log/nginx/debug.log debug;
  resolver 127.0.0.11;
  # whoami.loc
  upstream whoami.loc {
      ## Can be connect with "nginxproxy_default" network
      # nginxproxy_mysql_1
      server 192.168.32.2:3306;
  }
  server {
      listen whoami.loc:81;
      proxy_pass whoami.loc;
  }
  </code></pre>
  
  <h1>What I did, how can you reproduce the error</h1>
  
  <pre class="lang-sh prettyprint-override"><code># Set host
  &gt; sudo echo "127.0.0.1   whoami.loc" &gt;&gt; /etc/hosts
  # Start containers
  &gt; docker-compose up -d
  # "Login" the proxy container
  &gt; docker-compose exec nginx-proxy /bin/bash
  # Test connect to MySQL from proxy container
  root&gt; mysql -u root -proot -h whoami.loc -P 81
  # --&gt; OK, it works! Let's exit.
  mariadb&gt; \q
  # Exit from container
  root&gt; exit
  # Check host
  &gt; ping whoami.loc
  # --&gt; OK, 127.0.0.1
  # Check docker ports 
  &gt; docker-compose ps
            Name                        Command               State                    Ports                  
  -----------------------------------------------------------------------------------------------------------
  nginxproxy_mysql_1         docker-entrypoint.sh mysqld      Up      3306/tcp                                
  nginxproxy_nginx-proxy_1   /app/docker-entrypoint.sh  ...   Up      0.0.0.0:180-&gt;80/tcp, 0.0.0.0:81-&gt;81/tcp 
  nginxproxy_whoami_1        /app/http                        Up      8000/tcp                                
  # --&gt; OK
  # Try to direct connection from host (You can read the IP from the /etc/nginx/stream.conf.d/default.conf file)
  &gt; mysql -u root -proot -h 192.168.32.2
  # --&gt; OK, exit
  mysql&gt; \q
  # Try to connect from host with "domain" through docker proxy
  &gt; mysql -u root -proot -H whoami.loc -P 81 --protocol=tcp
  # ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
  </code></pre>
  
  <p>So, has anybody idea why works from container but why doesn't work from host?</p>
  
  <h1>The "solution"</h1>
  
  <p>There <strong>isn't solution</strong> for what I realy wanted. I wanted something like this: <a href="https://stackoverflow.com/questions/34741571/nginx-tcp-forwarding-based-on-hostname">Nginx TCP forwarding based on hostname</a> . <a href="https://stackoverflow.com/questions/46688433/docker-proxy-for-mysql-lost-connection-message-from-host-machine/46696768#46696768">Tarun's answer</a> solved the error message, thank you.</p>