I am using Docker 19.03.8 and want to setup two containers:
- The first is a MySQL container which bind mounts the mysqld socket from the host.
- The second one is a PHP container which needs to access the database via the MySQL container.
Both containers share the same network via the networks
statement. (I can not bind mount the mysqld socket directly into the PHP container, as the MySQL versions are different, therefore the MySQL container).
This is my docker-compose.yml file:
...
database:
build: .
volumes:
- /var/run/mysqld/mysqld.sock:/tmp/mysql.sock
...
The Dockerfile for MySQL:
FROM mysql:8.0.20
COPY my.cnf /etc/mysql/my.cnf
And the my.cnf file contains:
...
[mysql]
socket = /tmp/mysql.sock
[mysqld]
socket = /tmp/mysql.sock
...
Unfortunately I get this error message from the MySQL container:
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
The MySQL container starts with no errors, if I leave out socket = /tmp/mysql.sock
from the [mysqld]
section in my.cnf. But if I then try to connect to the database from the PHP container (mysql -h <what I specified in the networks statement> -u <user> -p <password>
) I just get an empty database. So I assume it uses the default socket (/var/run/mysqld/mysqld.sock).
I don't understand why this is not working, I would be really grateful if anyone could give me a hint.