1

What is the optimal way to load in a sql dump when using docker-compose + docker automated builds?

Have been ignoring docker-compose for a moment and trying to understand docker and it's automated builds at first but have come to realize that i will probably need docker-compose if i want to accomplish my project goal that is to use one 1 command and from that have a fully working 3 site Docker cluster

1xHAProxy
3xUbuntu/wp
3xMysqld

In my Dockerfile i can just include the db.sql from my Github repo like

ADD db.sql /tmp/db.sql

Failing to find a best practise how i should load my DB without writing any commands outside of build.

Want to know your solution to this using Dockerfile or Compose

By just executing one of the commands below a mysql FROM mysql with ADD db.sql db.sql should be build / run while loading db.sql in to mysql db wp

Dockerfile

$docker run -d user/repo:tag

docker-compose.yml

$docker-compose up

If am totally on the wrong path here please give me some references. Could also mention that am planning to use CoreOS once i feel OK with Docker. So if best practices on a CoreOS > Docker setup is something else, let me know!

5
  • Why don't you want to just have another RUN command in the Dockerfile to load the database dump? Commented Jan 17, 2016 at 22:36
  • @AndyShinn How would that RUN look like? Using suggestion here will give me The command xxx returned a non-zero code: Commented Jan 17, 2016 at 22:40
  • Which suggestion specifically? The answer that question has an example for starting mysqld_safe and then importing the database in one compound RUN command. You will need to have mysqld started before you can import the database, which is specifically the problem that question is asking. Commented Jan 17, 2016 at 23:04
  • So @AndyShinn maybe you can give me an example how it should be done the right way. Commented Jan 17, 2016 at 23:13
  • Have a look at the "Initializing a fresh instance" section of the official MySQL image; hub.docker.com/_/mysql, that explains how to provide a SQL dump (or a shell script) that is automatically executed to initialize the database. You can mount those files as a volume, or bake them in an image for distribution Commented Jan 17, 2016 at 23:19

1 Answer 1

1

There are two options for initializing a SQL file during build or run time:

The first would be to just base your MySQL image on the official image and place your SQL file in /docker-entrypoint-initdb.d (using something like ADD my.sql /docker-entrypoint-initdb.d/ in the Dockerfile). The official image has a fairly complex entrypoint script (https://github.com/docker-library/mysql/blob/master/5.7/docker-entrypoint.sh) which starts MySQL, initializes a username and password, and scripts from the /docker-entrypoint-initdb.d folder.

The other option would be to do something like the answer at https://stackoverflow.com/a/25920875/684908 and just add a command such as:

COPY dump.sql /tmp/
RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
  sleep 5 && \
  mysql -u root -e "CREATE DATABASE mydb" && \
  mysql -u root mydb < /tmp/dump.sql
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure the second option would work, given that the MySQL image uses aVOLUME for the data-dir; so the database would not persist after build
Using following in Dockerfile did load my db.sql in to ENV MYSQL_DATABASE so there is some progress. FROM mysql ADD db.sql /docker-entrypoint-initdb.d/ ENV MYSQL_DATABASE mydb ENV MYSQL_ROOT_PASSWORD 123 ENV MYSQL_USER user ENV MYSQL_PASSWORD 123

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.