title: Docker Mysql container root password [mysqld listens port 0]
body: <p>Today I'm trying to make my Docker environment working !</p>
  
  <p>In this matter I've encountered quite a problem : my MySQL container, extending the MySQL official Docker image, seems to fail to create the root account, despite the setting of the <code>MYSQL_ROOT_PASSWORD</code> environment variable in my <code>docker-compose`.yml</code>.</p>
  
  <p>I copy here my Docker files :</p>
  
  <p><strong>docker-compose.yml</strong></p>
  
  <p>Most of the environment variables are used in scripts and applications independantly from the MySQL server. Only <code>MYSQL_ROOT_PASSWORD</code> deserves interest (perhaps this statement is the cause of my failure to make this work..! ).</p>
  
  <pre><code>mysql:
      container_name: my_mysql
      build: mysql
      environment:
          - MYSQL_DATABASES=my-database
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_HOST=127.0.0.1
          - MYSQL_PORT=33306
          - MYSQL_USER=user
          - MYSQL_PASSWORD=password
          - MYSQL_MY_DATABASE=my-database
      ports:
          - "33306:3306"
      volumes:
          - "./volumes/mysql:/var/lib/mysql"
  </code></pre>
  
  <p><strong>mysql/Dockerfile</strong></p>
  
  <p>The dos2unix command is meant to convert Windows line endings to Unix.</p>
  
  <p>The custom entrypoint is named differently to avoid overidding the default mysql entrypoint script.</p>
  
  <pre><code>FROM mysql:5.7
  MAINTAINER Wonderful Dev &lt;wonderful.dev@wonderful-company.com&gt;
  
  RUN apt-get update &amp;&amp; apt-get install -y dos2unix
  
  COPY conf.d/custom.cnf /etc/mysql/conf.d/
  COPY docker-entrypoint-initdb.d/databases.sh /docker-entrypoint-initdb.d/databases.sh
  COPY my-entrypoint.sh /my-entrypoint.sh
  
  RUN dos2unix /docker-entrypoint-initdb.d/databases.sh &amp;&amp; dos2unix /my-entrypoint.sh &amp;&amp; dos2unix /etc/mysql/conf.d/custom.cnf &amp;&amp; apt-get --purge remove -y dos2unix &amp;&amp; rm -rf /var/lib/apt/lists/*
  
  RUN chmod a+x /docker-entrypoint-initdb.d/databases.sh &amp;&amp; chown root:root /docker-entrypoint-initdb.d/databases.sh
  RUN chmod a+x /my-entrypoint.sh &amp;&amp; chown root:root /my-entrypoint.sh
  
  ENTRYPOINT ["/entrypoint.sh", "/my-entrypoint.sh"]
  
  CMD ["mysqld"]
  </code></pre>
  
  <h3>mysql/my-entrypoint.sh</h3>
  
  <p>This chmod command is meant to avoid <a href="http://forum.ubuntu-fr.org/viewtopic.php?id=323455" rel="noreferrer">this kind of error</a></p>
  
  <pre><code>#!bin/bash
  
  chmod 664 /etc/mysql/conf.d/custom.cnf
  
  exec "$@"
  </code></pre>
  
  <h3>mysql/config/custom.cnf</h3>
  
  <pre><code>[mysqld]
  default-storage-engine=INNODB
  init-connect='SET NAMES utf8'
  character-set-server=utf8
  collation-server=utf8_unicode_ci
  bind-address        = 0.0.0.0
  skip-external-locking
  key_buffer      = 16M
  max_allowed_packet  = 16M
  thread_stack        = 192K
  thread_cache_size       = 8
  query_cache_limit   = 1M
  query_cache_size        = 16M
  expire_logs_days    = 10
  max_binlog_size         = 100M
  
  [mysqldump]
  quick
  quote-names
  max_allowed_packet  = 16M
  
  [isamchk]
  key_buffer      = 16M
  </code></pre>
  
  <p>All this stuff is working, except that I haven't any root access, blocking me from creating databases or mysql users for my apps !</p>
  
  <p>Thank you for any advice :D !</p>
  
  <p><strong>EDIT</strong>: After days of investigation, and an issue in <code>docker-library/mysql</code> repository, we found out that one of the problems was a deprecated configuratio key in <strong>custom.cnf</strong>.</p>
  
  <pre><code>key_buffer =&gt; key_buffer_size
  </code></pre>
  
  <p>Now the container runs a few seconds after building, then crash.</p>
  
  <p>The main clue in the logs is that line : </p>
  
  <pre><code>Version: '5.7.15'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server (GPL)
  </code></pre>
  
  <p>I already tried a proposed solution to configure manually the port in <strong>custom.cnf</strong> but it does not work. The container tries to connect to MySQL and crash due to the port.</p>
  
  <p>The problem was not pretty visible because when launching the container a second time, the initialization was skipped, the good port was configured, and the server worked fine.</p>
  
  <p>The thing is that due to the crash, the end of the initialization was not executed, including my scripts and the databases creation along with users.</p>
  
  <p>So I'd like to find out why this damn mysqld is listening on port 0 the first time I launch the container after building.</p>
  
  <p>If you have a clue, I'd be glad to here about it !</p>