Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seeding mysql data in a docker build

Tags:

docker

mysql

I'm attempting to build a docker image that will include MySQL and some seed data, and I'm trying to figure out how to insert the data into the database during the docker build phase.

It seems I need to start the MySQL engine, invoke a command to run some SQL statements, and then shut down the MySQL engine. Any good ideas on how to best do that?

like image 228
user1864118 Avatar asked Sep 30 '13 02:09

user1864118


2 Answers

When the image is built, the folder /docker-entrypoint-initdb.d is checked for files to seed the DB with. Below the corresponding paragraph from the section Initializing a fresh instance on the official MySQL Docker Hub page.

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

like image 133
Uchendu Avatar answered Oct 09 '22 20:10

Uchendu


This blog post might help you. Essentially, the steps to be followed are:
1. create a file (say seed_data.sh) and put it in the same directory as your Dockerfile
2. in the dockerfile add the following lines

ADD resources/seed_data.sh /tmp/
RUN chmod +x /tmp/seed_data.sh
RUN /tmp/seed_data.sh
RUN rm /tmp/seed_data.sh

The file seed_data.sh contains the code for running the mysql server, logging into it and then inserting the data.

like image 45
ric03uec Avatar answered Oct 09 '22 18:10

ric03uec