I'm trying to setup a Dockerfile for my LAMP project, but i'm having a few problems when starting MySQL. I have the folowing lines on my Dockerfile:
VOLUME ["/etc/mysql", "/var/lib/mysql"]
ADD dump.sql /tmp/dump.sql
RUN /usr/bin/mysqld_safe & sleep 5s
RUN mysql -u root -e "CREATE DATABASE mydb"
RUN mysql -u root mydb < /tmp/dump.sql
But I keep getting this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
Any ideas on how to setup database creation and dump import during a Dockerfile build?
Here is a working version using
v3
ofdocker-compose.yml
. The key is the volumes directive:In the directory that I have my
docker-compose.yml
I have adata
dir that contains.sql
dump files. This is nice because you can have a.sql
dump file per table.I simply run
docker-compose up
and I'm good to go. Data automatically persists between stops. If you want remove the data and "suck in" new.sql
files rundocker-compose down
thendocker-compose up
.If anyone knows how to get the
mysql
docker to re-process files in/docker-entrypoint-initdb.d
without removing the volume, please leave a comment and I will update this answer.I used docker-entrypoint-initdb.d approach (Thanks to @Kuhess) But in my case I want to create my DB based on some parameters I defined in .env file so I did these
1) First I define .env file something like this in my docker root project directory
2) Then I define my docker-compose.yml file. So I used the args directive to define my environment variables and I set them from .env file
3) Then I define a mysql folder that includes a Dockerfile. So the Dockerfile is this
4) Now I use mysqldump to dump my db and put the data.sql inside mysql folder
The file is just a normal sql dump file but I add 2 lines at the beginning so the file would look like this
So what happening is that I used "RUN sed -i 's/MYSQL_DATABASE/'$MYSQL_DATABASE'/g' /etc/mysql/data.sql" command to replace the
MYSQL_DATABASE
placeholder with the name of my DB that I have set it in .env file.Now you are ready to build and run your container
The latest version of the official mysql docker image allows you to import data on startup. Here is my docker-compose.yml
Here, I have my data-dump.sql under
docker/data
which is relative to the folder the docker-compose is running from. I am mounting that sql file into this directory/docker-entrypoint-initdb.d
on the container.If you are interested to see how this works, have a look at their
docker-entrypoint.sh
in GitHub. They have added this block to allow importing dataAn additional note, if you want the data to be persisted even after the mysql container is stopped and removed, you need to have a separate data container as you see in the docker-compose.yml. The contents of the data container Dockerfile are very simple.
The data container doesn't even have to be in start state for persistence.
What I did was download my sql dump in a "db-dump" folder, and mounted it:
When I run
docker-compose up
for the first time, the dump is restored in the db.Each
RUN
instruction in aDockerfile
is executed in a different layer (as explained in the documentation ofRUN
).In your
Dockerfile
, you have threeRUN
instructions. The problem is that MySQL server is only started in the first. In the others, no MySQL are running, that is why you get your connection error withmysql
client.To solve this problem you have 2 solutions.
Solution 1: use a one-line
RUN
Solution 2: use a script
Create an executable script
init_db.sh
:Add these lines to your
Dockerfile
: