2

For testing purposes I want to build a MySQL container that has a dataset bootstrapped into it. I know that mounting is possible and that you can place sql scripts into the init directory to run the import at start, but thats not what we want. So I tried to build one myself that just copies the datafiles into the docker container.

I use the following dockerfile to build my container.

FROM mysql:8.0.21
EXPOSE 3306

ARG BUILD_ID
LABEL stage=builder
LABEL build=$BUILD_ID
ENV TZ=Europe/Amsterdam

RUN apt-get update && apt-get install -y tzdata
ADD http://server.example.com/data/datafiles_2020_11_02.tar.gz /var/lib/datafile.tar.gz
RUN tar -xvzf /var/lib/datafile.tar.gz -C /var/lib/mysql && chown -R mysql.root /var/lib/mysql
#ADD initieel/* /docker-entrypoint-initdb.d/
ADD cnf/* /etc/mysql/mysql.conf.d/

This runs succesfully so afterwards I spin up my freshly build container and during the startup something in the container will just initialize a fresh database and throw everything that I placed into /var/lib/mysql away. I checked the README that comes with the mysql container but there is nothing I can find that relates to this.

thanks in advance for taking the time to read. I will update if my post is lacking any information.

1 Answers1

0

The common approach is to specifiy the initialization only when you need to.

Instead of putting RUN tar -xvzf /var/lib/datafile.tar.gz directly in your Dockerfile, place it into a shell script that you add to your container, for example as /opt/mysql-bootstrap.sh.

Then, when you create your container the first time on a host, run docker with that script.

docker run --rm -v /path/to/var/lib/mysql:/var/lib/mysql \
    --name my-mysql-bootstrap my-mysql:latest /opt/mysql-bootstrap.sh

The --rm will remove the container after the initialization script ran. Afterwards, create your container that stays:

docker run -v /path/to/var/lib/mysql:/var/lib/mysql \
    --name my-mysql my-mysql:latest
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
  • thanks for the suggestion but the idea is that the container is ready to go when started. I don't want it to run scripts when the container starts. So that my developer/tester friends spend less time waiting for the database to become ready. – Koen van der Rijt Nov 05 '20 at 08:42