1

I would like to create a Dockerfile in order to create a container that has already mysql installed and my databases created.

I have an sql folder that contains my *.sql files and a script folder that contains my db_builder.sh script that does all the work I need (create the databases, import the needed sql files, etc...).

The only thing I'm missing is to run the mysql server before the db_builder.sh script runs. Also I need to know what would be the default password of the root user.

FROM ubuntu:18.04

ADD sql src/sql
ADD scripts src/scripts

RUN apt-get update && apt-get install mysql-server -y

# somehow start mysql ???

RUN src/scripts/db_builder.sh
2
  • Why not use the official docker image (hub.docker.com/_/mysql)? You could then just include your seed scripts in the docker-entrypoint-initdb.d directory (mounted into the container) which will be automatically be run when the container initialises. Commented Jan 9, 2019 at 14:27
  • @mtt_g I felt like that was a better idea, actually was the first thing I've tried but I failed. I'm gonna try it again soon. Commented Jan 9, 2019 at 16:13

1 Answer 1

3

I solved my issue by:

1) creating the Dockerfile FROM MySQL image instead of Ubuntu image

2) splitting my db_builder.sh into two scripts: - prepare_sql_files.sh -> which prepares the needed sql files to be imported - db_import.sh -> which actually does the import

3) set RUN the prepare_sql_files.sh in the Dockerfile, while just placing (ADD) the db_import.sh in /docker-entrypoint-initdb.d because of this feature of the mysql docker image:

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.

So my dockerfile now looks like this:

FROM mysql:latest

ADD sql /src/sql
ADD scripts /src/scripts

RUN /src/scripts/prepare_sql_files.sh

ADD /src/scripts /docker-entrypoint-initdb.d
Sign up to request clarification or add additional context in comments.

Comments

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.