title: Docker: MySQL container bind host mysqld socket
body: <p>I am using Docker 19.03.8 and want to setup two containers:</p>
  
  <ul>
  <li>The first is a MySQL container which bind mounts the mysqld socket from the host.</li>
  <li>The second one is a PHP container which needs to access the database via the MySQL container.</li>
  </ul>
  
  <p>Both containers share the same network via the <code>networks</code> statement. (I can not bind mount the mysqld socket directly into the PHP container, as the MySQL versions are different, therefore the MySQL container).</p>
  
  <p>This is my <em>docker-compose.yml</em> file:</p>
  
  <pre><code>...
  database:
    build: .
    volumes:
      - /var/run/mysqld/mysqld.sock:/tmp/mysql.sock
  ...
  </code></pre>
  
  <p>The <em>Dockerfile</em> for MySQL:</p>
  
  <pre><code>FROM mysql:8.0.20
  COPY my.cnf /etc/mysql/my.cnf
  </code></pre>
  
  <p>And the <em>my.cnf</em> file contains:</p>
  
  <pre><code>...
  [mysql]
  socket = /tmp/mysql.sock
  
  [mysqld]
  socket = /tmp/mysql.sock
  ...
  </code></pre>
  
  <p>Unfortunately I get this error message from the MySQL container:</p>
  
  <pre><code>2020-05-20T11:30:04.708067Z 0 [ERROR] [MY-010270] [Server] Can't start server : Bind on unix socket: Address already in use
  2020-05-20T11:30:04.708406Z 0 [ERROR] [MY-010258] [Server] Do you already have another mysqld server running on socket: /tmp/mysql.sock ?
  2020-05-20T11:30:04.709083Z 0 [ERROR] [MY-010119] [Server] Aborting
  </code></pre>
  
  <p>The MySQL container starts with no errors, if I leave out <code>socket = /tmp/mysql.sock</code> from the <code>[mysqld]</code> section in <em>my.cnf</em>. But if I then try to connect to the database from the PHP container (<code>mysql -h &lt;what I specified in the networks statement&gt; -u &lt;user&gt; -p &lt;password&gt;</code>) I just get an empty database. So I assume it uses the default socket (<em>/var/run/mysqld/mysqld.sock</em>).</p>
  
  <p>I don't understand why this is not working, I would be really grateful if anyone could give me a hint.</p>