title: Internal error on MySQL from outside the container
body: <p>I built a MySQL server image and could run its container, I could also access it from another container just fine, for some days last week, all of this locally on my laptop running <code>Docker 17.05.0-ce</code> on <code>Ubuntu 16.04</code>. Then, yesterday I could not access MySQL from the outside. It started giving me the error: <code>ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 "Internal error/check (Not system error)"</code></p>
  
  <p>Here is the <code>Dockerfile</code> content:</p>
  
  <pre><code>$ cat mysql/Dockerfile 
  FROM ubuntu:16.04
  
  RUN apt-get update \
    &amp;&amp; apt-get install -y libncurses-dev \
    &amp;&amp; apt-get install -y build-essential \
    &amp;&amp; apt-get install -y cmake
  
  COPY mysql-5.6.30.tar.gz /usr/bin/
  WORKDIR /usr/bin/
  RUN gzip -d mysql-5.6.30.tar.gz \
    &amp;&amp; tar -xvf mysql-5.6.30.tar \
    &amp;&amp; ln -s mysql-5.6.30 mysql
  
  WORKDIR /usr/bin/mysql/
  RUN mkdir install &amp;&amp; mkdir install/data &amp;&amp; mkdir install/var &amp;&amp; mkdir install/etc &amp;&amp; mkdir install/tmp
  
  RUN cd /usr/bin/mysql/ \
    &amp;&amp; cmake \
    -DCMAKE_INSTALL_PREFIX=/usr/bin/mysql/install \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DMYSQL_DATADIR=/usr/bin/mysql/install/data \
    -DDOWNLOAD_BOOST=1 \
    -DWITH_BOOST=/usr/bin/mysql/install/boost \
    -DMYSQL_UNIX_ADDR=/usr/bin/mysql/install/tmp/mysql.sock \
    &amp;&amp; make \
    &amp;&amp; make install \
    &amp;&amp; make clean
  
  EXPOSE 3306
  
  COPY startup.sh install/startup.sh
  
  COPY my.cnf install/my.cnf
  
  ENTRYPOINT ["/bin/bash", "install/startup.sh"]
  </code></pre>
  
  <p>The <code>my.cnf</code> file content:</p>
  
  <pre><code>[mysqld]
  bind-address    = 0.0.0.0 # Allow client binding from any IP address instead of just 127.0.0.1
  port            = 3307
  sql_mode        = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION # This is strict mode: NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  socket          = /usr/bin/mysql/install/tmp/mysql.sock
  user            = root
  basedir         = /usr/bin/mysql/install
  datadir         = /usr/bin/mysql/install/data
  log-bin         = /usr/bin/mysql/install/mysql.bin.log
  log-error       = /usr/bin/mysql/install/mysql.error.log
  general-log-file     = /usr/bin/mysql/install/mysql.log
  slow-query-log-file  = /usr/bin/mysql/install/mysql.slow.queries.log
  innodb_file_per_table = 1
  innodb_flush_log_at_trx_commit = 1
  sync_binlog = 1
  innodb_flush_method = O_DIRECT
  character-set-server = utf8mb4
  collation-server = utf8mb4_unicode_ci
  init-connect = 'SET NAMES utf8mb4'
  character-set-client-handshake = FALSE
  wait_timeout = 28800 # amount of seconds during inactivity that MySQL will wait before it will close a connection on a non-interactive connection
  interactive_timeout = 28800 # same, but for interactive sessions
  [client]
  socket = /usr/bin/mysql/install/tmp/mysql.sock
  default-character-set = utf8mb4
  [mysql]
  default-character-set = utf8mb4
  </code></pre>
  
  <p>The <code>startup.sh</code> file content:</p>
  
  <pre><code>#!/bin/bash -x
  
  if [ ! -f /usr/bin/mysql/install/data/ibdata1 ]; then
    chmod 755 /usr/bin/mysql/install/scripts/mysql_install_db
    sleep 1 # Needed to avoid running the script when this mysql_install_db file has not yet been released by the permissions change
    /usr/bin/mysql/install/scripts/mysql_install_db \
      --no-defaults \
      --explicit_defaults_for_timestamp \
      --basedir=/usr/bin/mysql/install \
      --datadir=/usr/bin/mysql/install/data \
      --tmpdir=/usr/bin/mysql/install/tmp \
      --lc-messages-dir=/usr/bin/mysql/install/share
  
    /usr/bin/mysql/install/bin/mysqld_safe --defaults-file=/usr/bin/mysql/install/my.cnf &amp;
    sleep 10s
    echo "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES" | /usr/bin/mysql/install/bin/mysql
    tail -f /etc/hosts
  else
    /usr/bin/mysql/install/bin/mysqld_safe --defaults-file=/usr/bin/mysql/install/my.cnf
  fi
  </code></pre>
  
  <p>And I can run it with the command:</p>
  
  <pre><code>docker run -d -p 3307:3306 -v /home/stephane/dev/php/learnintouch/docker/mysql/data:/usr/bin/mysql/install/data --name mysql stephaneeybert/mysql:5.6.30
  </code></pre>
  
  <p>I can see the port has an open state:</p>
  
  <pre><code>nmap -p 3307 localhost
  </code></pre>
  
  <p>I can enter its bash shell:</p>
  
  <pre><code>docker exec -it mysql bash
  </code></pre>
  
  <p>And, from within the container, I log in just fine:</p>
  
  <pre><code>/usr/bin/mysql/install/bin/mysql -u root -p
  </code></pre>
  
  <p>But if I want to log in from the host:</p>
  
  <pre><code>cd programs/install/mariadb/; bin/mysql -h localhost -P 3307 --protocol=tcp -u root -p
  </code></pre>
  
  <p>it  gives me the message:</p>
  
  <pre><code>ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 "Internal error/check (Not system error)"
  </code></pre>