1

I am creating an image in the docker to the mysql install mode, but it is giving error to start mysql.

My Dockerfile

The error occurs when processing and line 25 of Dockerfile:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

I found that the error occurs because mysql is not running. The docker print below to rotate the line 22:

bin boot dev etc home lib lib64 media mnt opt proc root run sbin scripts srv sys tmp usr var MySQL is stopped.

Stack trace complete

Any suggestion?

Tiago Rolim
  • 65
  • 1
  • 11

2 Answers2

1

The docker daemon will execute RUN command one by one and commit the result, and seems your mysql service status is not committed to the image. To solve this problem, you may try these ways

  • Put all commands into one RUN command
    RUN echo $(service mysql restart) && echo $(service mysql status) && sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;" && sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'@'%';" && sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'@'%'= PASSWORD('${mysql_pass}');" && sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'%'; FLUSH PRIVILEGES;"

  • initialize your mysql in entrypoint with a bash script.

init_mysql.sh

#!/bin/sh
sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;"
sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'@'%';"
sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'@'%'= PASSWORD('${mysql_pass}');"
sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'%'; FLUSH PRIVILEGES;"

and add following lines to dockerfile

ADD init_mysql.sh /
RUN \
  cd / \
  chmod 777 init_mysql.sh

CMD ./init_mysql.sh
Freeznet
  • 537
  • 2
  • 6
  • @Tiago you should try to use `CMD /opt/init_mysql.sh` or `CMD ["/bin/bash", "-c", "/opt/init_mysql.sh"]` – Freeznet Jul 09 '15 at 05:24
  • I changed my Dockerfile for "CMD /opt/init_mysql.sh" yet he does not run my script "init_mysql.sh". Logging in vm and running, "-lha ls/opt/" show: -rwxr-xr-x 1 root root 39 Jul 9 22:20 init_mysql.sh. That is, the script file is being created, but is not running because the directory "/opt/teste001" is not being created. – Tiago Rolim Jul 09 '15 at 23:00
0

You need to figure out why MySQL isn't starting. Log into the environment and manually try to start MySQL and see what the error is.

Have you considered basing your image on one of the official MySQL Docker images? https://registry.hub.docker.com/_/mysql/

Trent Lloyd
  • 1,832
  • 1
  • 15
  • 13