I am trying to create multiple databases when a MySQL container starts up.
According to https://github.com/docker-library/mysql/pull/18, I can mount or copy my scripts in the /docker-entrypoint-initdb.d
of the image and they will be executed automatically on start up.
However my scripts are not at all executed. Seems like the docker-entrypoint.sh
doesn't see files in the /docker-entrypoint-initdb.d
directory.
This is my Dockerfile:
FROM mysql
ADD script.sql /docker-entrypoint-initdb.d/script.sql
RUN chmod -R 775 /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD mypass
This is my script.sql
:
CREATE DATABASE mydb;
CREATE DATABASE mydb2;
I build and run the container:
$ docker build -t mysql .
$ docker run -v data_volume:/var/lib/mysql --name somedb -d mysql
When I access the container in tty I can see that the script.sql
is in the /docker-entrypoint-initdb.d
but is not executed.
I have seen the docker logs -f somedb
output but there is no error.
I have tried with .sh
file, I also have tried with Maria-db, the result is the same.
What could be the problem?
So I had the same issue for hours, and then decided to look into docker-entrypoint.sh. It turns out that the script checks for $DATADIR/mysql, typical /var/lib/mysql and skips the rest of the code if the datadir exists, incl. docker-entrypoint-initdb.d
So what I did was make a simple init.sh file to remove the datadir then start docker.
docker-compose.yml:
init.sh:
And of course add
-d
todocker-compose
once I see it works as expected.I had the exact same issue with the mariadb image (version: 10.4) and I solved it by making sure my container data volume is empty from any files or directories when I create the container from scratch.
This is my docker compose file:
For me I just had to make sure this path: './storage/db' is empty from files. Please notice that the directory has to exists but be empty.
not really an answer to that behavior but i usually do it like this:
don't think there's anything wrong with your code but who knows. i suppose you checked 775 is the correct owner? btw im using FROM mysql:5
Please make data_volume empty before run your container. And your sql files will be executed for sure.
Problems here:
In docker-entrypoint.sh, the check the first running by using exist of mysql directory in /var/lib/mysql
You can get more details at Dockerfile source
Here is my docker file working absolutely fine. /sql-scripts/ contains a sql file which is executed once the container runs.
Have you solved the issue yet?
A simple workaround is to use absolute path when reading files in /docker-entrypoint-initdb.d/
I made my test using the default config of: https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile#L70
The docker-entrypoint.sh is first moved to /usr/local/bin before execution.
Then it comes to reading files in /docker-entrypoint-initdb.d/, but the following:
doesn't seem to find anything.
If you replace it with:
and then make sure the folder is populated by the Dockerfile command:
From there, build a test statement in /docker-entrypoint-initdb.d/test.sql
Finally build and run the image and you should see your database created.