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.

  • Cant connect to Mysql docker container from host
  • Filebeat multiline parsing of Java exception in docker container not working
  • docker build failing with Could not resolve ''
  • Golang binary built inside Docker container, still Mach-O executable format?
  • .dockerignore all subfolders but not files in the folder itself
  • Unable to login to private docker registry
  • Except that the app has no default data. The developers have requested to use a full prod dump rather than fixtures with loaddata.

    Therefore I built a container based on this Dockerfile

    FROM python:2.7
    COPY . /opt/
    WORKDIR /opt/
    RUN wget -q -O /usr/local/bin/waitforit$WAITFORIT_VERSION/waitforit-linux_amd64 \
        && chmod +x /usr/local/bin/waitforit
    RUN curl -sL | 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/ && python collectstatic --noinput && python /opt/ runserver"

    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 docker-compose?

    If not, then how can I fix the above Dockerfile

  • Tomcat remote deploy with Intellj IDEA
  • After installing docker on centos7,Failed to start docker.“Job for docker.service failed.”
  • Docker image container - where can I store my files?
  • Continuous Delivery on Wordpress using Docker
  • Build custom Docker microsoft dotnet image with NuGet packages already downloaded
  • Run a maven webapp in a docker container
  • 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.

    When you docker run this image, docker will create the container, the 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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.