4

I am using Docker to create a dockerfile with mysql as the base image:

FROM mysql
#set root pass
ENV MYSQL_ROOT_PASSWORD password
#update linux
RUN apt-get update
#create database
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
#install vim
RUN apt-get install vim -y

The dockerfile fails on the step where I try to create a database, it doesn't finish building and i receive this error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

When I remove the #create database run command the dockerfile will build and I am able to run a container from that image. I know that it isn't a problem with the mysql server as I can enter the container and run the mysql command manually with success and the service status is running.

Using an environment variable i.e MYSQL_ROOT_PASSWORD within the file also allows me to create a database successfully but this will only work with a single database, I need to be able to use the mysql command to make queries, such as creating additional databases / assigning users etc.

This may be because I need to specify the host and port of the docker container but this still does not allow me to connect

RUN mysql -u root -ppassword -h 127.0.0.1 -P 3308 -e "CREATE DATABASE dbname"

Strangely, doing this also often crashes the container and puts it in a state where it will crash again on start-up every time that I try to restart it again.

5 Answers 5

5

I think the issue might be that in the service hasn't started within the container used to build your Dockerfile.

3
  • I tried this solution by copying the contents of the MySQL Dockerfile and adding my query to the end of the RUN command, this builds successfully but any container that I create with this image crashes on startup also. Feb 18, 2016 at 14:36
  • The containers you build do not run all services by default. So once you've built the container, you should start mysqld again (in that container). Feb 18, 2016 at 15:26
  • the mysqld was already running as it was set to run at startup Mar 1, 2016 at 10:39
4

Use below-given commands in your Dockerfile:

RUN service mysql restart && echo 'CREATE DATABASE db_name;' | mysql -uroot -
pYOUR_ROOT_PASSWORD
2

Had the very same problem: When starting the container and running a set of RUN instructions, or .sh or .sql scripts in /docker-entrypoint-initdb.d/ no connection to the database server could be established.

I found the solution by a comment of @wpalmer on the mysql-image:

The init scripts run by the entrypoint, internally, use the variable "${mysql[@]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).

So what this means for you, instead of providing the plain mysql command with user, pass etc. as in

RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"

use the placeholder instead:

RUN "${mysql[@]}" -e "CREATE DATABASE dbname"
1

You can try to build other image and run the create DB from there. Example of docker-compose.yml

web:
  build: web
  links:
  - "db:db.local"
  entrypoint: entrypoint.sh

db:
  build: db
  environment:
     MYSQL_ROOT_PASSWORD: password
  command: mysqld

For entrypoint.sh you put something like this:

#!/bin/sh

#this is a hack to wait until the DB image is up and the port is open
until mysqladmin -u root -ppassword -e -h db.local ping; do
   echo "$(date) - waiting for mysql"
   sleep 3
done

if ! mysql -u root -ppassword -e -h db -e 'use dbname'; then
   mysql -u root -ppassword -e "CREATE DATABASE dbname"
fi

exec "$@"
1

You can copy your queries as .sql file into "/docker-entrypoint-initdb.d" container directory. mysql will execute them after starting container

COPY ./init/db.sql /docker-entrypoint-initdb.d/

read official doc https://hub.docker.com/_/mysql?tab=description&page=1 Initializing a fresh instance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.