0

With the Docker image of mysql you can put in mysqldump scripts into a specific folder and it will use those scripts to load up an empty data on first start up.

What I was wondering is how to create an image where that execution had already taken place as part of the build so we don't have to wait for the initial data load.

The purpose of the image is to have a developer copy of a database for local testing.

If possible I would like it to be done as a multistage Docker build so I can build it without having MySQL installed on the build server.

My general thought process though I haven't tried it out yet is as follows:

FROM mysql as builder COPY *.sql /somefolder COPY buildscript.sh /buildscript.sh FROM mysql COPY --from build /var/lib/mysql /data # to avoid using the volume folder 

And buildscript.sh does something like

  1. start up mysqld
  2. wait for the mysqld to fully start up (??)
  3. run the SQLs used to build the data
  4. shut down mysqld
  5. sync (make sure all the files are written)

2 Answers 2

0

I think is possible doing it in to Dockerfile instead perform the operation at the entry point. You should copy the database data in to image and configure the my.cnf to point the directory you had store that data and skip the dump operation in to entry point. Anyway as the personal consideration which advantage you get from this? I mean, yes you so not have to wait to create and populate the database in the run time, but it will cost you time every time you build again the image, and also make the image bigger because some data is included in it. You have to consider if you plan to re-build a lot that image it will not change a lot in time cost, if you will not plan to re-build image frequently could be an advantage.

2
  • The advantage of it would be the start up costs for the developers. The image I am building is an anonymized data dump of the production database ideally daily though I should be able to trigger it on demand. Commented Sep 9, 2018 at 20:11
  • It would be nice if you had a more detailed solution though. Commented Sep 9, 2018 at 20:12
0

Here's my current working solution.

I start with the Dockerfile

FROM mysql:5.7.23 as prep COPY *.sql /docker-entrypoint-initdb.d/ COPY scripts/build-db.sh / ARG MYSQL_ROOT_PASSWORD=my-very-secret ARG MYSQL_USER=sampleuser ARG MYSQL_PASSWORD=password123 RUN chmod u+x /build-db.sh RUN /build-db.sh FROM mysql:5.7.23 # use /data in my case so that it uses a directory that is not a VOLUME COPY --from=prep /data /data 

Then I add a build-db.sh which sets takes the existing entrypoint.sh and take out the last exec line out to make it stop once it finishes the initialization. The data is put in /data because the default /var/lib/mysql will not contain any data on the next stage of the build.

#!/bin/bash head -n -1 /usr/local/bin/docker-entrypoint.sh > /d2.sh chmod +x /d2.sh exec /d2.sh --datadir=/data 

To start it you need to have --datadir=/data to use the non-volume directory.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.