无法通过Docker Image中的套接字错误连接到本地MySQL服务器

我正在创build一些示例数据的MySQL图像。 我正在尝试在图像创build期间启动mysql进程时出现错误,以便可以将数据放入图像中。 以下是我的文件结构

[DockerFile]

FROM mysql:5.6.29 # MYSQL volumes VOLUME ["/etc/mysql", "/var/lib/mysql"] COPY data_dump.sql /tmp/ COPY init_db.sh /tmp/ RUN chmod -R 777 /tmp/init_db.sh RUN chmod -R 777 /tmp/data_dump.sql RUN /tmp/init_db.sh EXPOSE 3306 

[init_db.sh]

 #!/bin/bash echo "starting the bash command" /usr/bin/mysqld_safe & echo "started the mysqld_safe process" sleep 15 mysql -u root -h localhost -e "CREATE DATABASE test" echo "Done creating the test schema" mysql -u root -h localhost test < /tmp/data_dump.sql echo "Done creating the test data" 

我收到以下错误:

 started the mysqld_safe process 170906 13:56:49 mysqld_safe Logging to '/var/lib/mysql/bcad5a346f31.err'. 170906 13:56:49 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 170906 13:56:49 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Done creating the test schema Done creating the test data ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 

我find了一个解决办法,事实certificate我做得比我需要的多。 下面是我的DockerFile看起来像现在

[DockerFile]

 FROM mysql:5.6.29 # 1. Set up the environment variables ENV MYSQL_ALLOW_EMPTY_PASSWORD yes ENV MYSQL_DATABASE testDB ENV MYSQL_USER root # 2. copy the data dump file in the /docker-entrypoint-initdb.d/ location so that it gets executed by default COPY data-dump.sql /docker-entrypoint-initdb.d/data-dump.sql # 3. change the data location in the my.conf file in the docker, it is needed to create the data from the /docker-entrypoint-initdb.d/data-dump.sql dump file # for mysql 5.7 if the below line doesn't work try: RUN sed -i 's|/var/lib/mysql|/var/lib/mysql-data|g' /etc/mysql/mysql.conf.d/mysqld.cnf RUN sed -i 's|/var/lib/mysql|/var/lib/mysql-data|g' /etc/mysql/my.cnf # 4. start the mysql process so that it creates the required data RUN /entrypoint.sh mysqld & sleep 30 && killall mysqld # 5. remove the dump file from the image to save image space RUN rm /docker-entrypoint-initdb.d/data-dump.sql