Run a SQL file when mysql's docker image first startup

Viewed 31

I want to run an init sql file at the first run of my MySQL:5.7.5 docker run.

I tired to run the following docker run command:

docker run -d -p 13306:3306 \
 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
 -e MYSQL_DATABASE=storagedb \
 -v storage.sql:/docker-entrypoint-initdb.d/storage.sql \
 mysql:5.7.5

Then, I run docker exec into this container and use the following command mysql -uroot -p into the mysql server.

Then I run use storagedb command to select the -e MYSQL_DATABASE=storagedb specified Database。

In the end, run show tables , I can't see the table I created in the SQL file。

The sql file's content is:

create database storagedb;
use storagedb;
CREATE TABLE `storage_tbl` (
                               `id` int(11) NOT NULL AUTO_INCREMENT,
                               `commodity_code` varchar(255) DEFAULT NULL,
                               `count` int(11) DEFAULT 0,
                               PRIMARY KEY (`id`),
                               UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I try again with a Dockerfile, TheDockerfile's content is:

FROM mysql:5.7
ADD storage.sql /docker-entrypoint-initdb.d/storage.sql
RUN chmod -R 775 /docker-entrypoint-initdb.d
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes

I build this Dockerfile to a new image tmpMySQL, Then i run this image and go into the container.

Now, i can see the database storagedb and the table storage_tbl. why?

I don't want to build a new docker image. I just want to startup a container by a mysql image and run an init .sql file with /docker-entrypoint-initdb.d!

Your guidance would be appreciated, Thank you very much!

0 Answers
Related