How to Resolve Docker Local MySQL “Warning: Error in connection_create: Failed to connect” Issue
Most of the developers building the docker images to reduce the setup time for development testing environment. Developer’s use the production equal docker container to do the testing for a developed product. Also, it enables better continuous integration and continuous deployment strategies.
As a development testing setups, most of the people run the MySQL as a service inside the docker container rather than using the cloud database service or separate database server.
Please find the below docker file for example -
Use the Ubuntu OS for the docker image
FROM ubuntu:16.04
Installing the MYSQL using the apt commands
RUN apt-get updateRUN apt-get install -y mysql-server
Can start the MySQL server
RUN service mysql start
Most of the time when building the docker image process is stopped and throwing an error by saying
Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2 “No such file or directory”)
This is basically happening because of the MySQL file directory permission issue.
To resolve this issue, you want to set the correct permissions for MySQL directories.
RUN chown -R mysql:mysql /var/lib/mysql && service mysql start
If you use the CMD command and start the MySQL server when the container launching in build image.
Set the MySQL directories permissions as below,
CMD chown -R mysql:mysql /var/lib/mysql /var/run/mysqld && service mysql restart
Reference —
https://support.openanalytics.eu/t/docker-cant-connect-to-local-mysql-server-through-socket/1408