Seeding a MySQL DB for a Dockerized Django App
I am tasked with creating a click-button style use of Docker for developers of a Django app to do local development.
I am using
docker-compose in combination with private repos on my Docker Hub and it’s working well.
Except that the app has no default data. The developers have requested to use a full prod dump rather than fixtures with
Therefore I built a container based on this Dockerfile
FROM python:2.7 COPY . /opt/mysite.com WORKDIR /opt/mysite.com ENV WAITFORIT_VERSION="v1.3.1" RUN wget -q -O /usr/local/bin/waitforit https://github.com/maxcnunes/waitforit/releases/download/$WAITFORIT_VERSION/waitforit-linux_amd64 \ && chmod +x /usr/local/bin/waitforit RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - RUN apt-get -y update && apt-get -y install mysql-client nodejs RUN pip install -r requirements/local.txt RUN npm install RUN ./node_modules/.bin/babel-node ./node_modules/.bin/webpack CMD waitforit -full-connection=tcp://mysql:3306 -timeout=60 -debug && mysql -u root -h mysql --password=**** mysite_develop < prod.sql && /bin/bash -c "source /opt/mysite.com/env.local && python manage.py collectstatic --noinput && python /opt/mysite.com/manage.py runserver 0.0.0.0:5000"
All works well except that it takes a very long time to be able to see
localhost:5000 after the containers are up.
Therefore I started to investigate
data-only containers, but I am wondering what happens when I push that container to Docker hub? Can it be pulled with the mysql data baked onto from
If not, then how can I fix the above
One Solution collect form web for “Seeding a MySQL DB for a Dockerized Django App”
MySQL (and variants like Percona Server) provide a facility for seeding a database on first container start described in the “Initializing a fresh instance” section in the 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.
A Dockerfile using this technique will look like
FROM mysql:8 COPY seed-data.sql /docker-entrypoint-initdb.d/
Note: the directory trailing slash is important.
docker run this image, docker will create the container, the docker-entrypoint.sh will execute the sql script, and then the container will be ready to serve data.
Data will persist through container restarts – the seed data and subsequent modifications.
You can reuse the container for faster startup times – subsequent container starts will not need to initialize the database or seed the data.
When you need pristine data, delete the container and
docker run the image again.
When you delete the container, remember to delete the docker volume containing the db data:
docker rm -v $CONTAINER_NAME.
I use this method to provide standard data for language / framework POCs as well as CI/CD. Initializing a new database and seeding large amounts of data can take a couple of minutes so you will want some logic to pause automated operations until you can successfully make a db connection.
Hope this helps you on your way.