为Dockerized Django应用播种MySQL数据库 [英] Seeding a MySQL DB for a Dockerized Django App

查看:56
本文介绍了为Dockerized Django应用播种MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是为Django应用程序的开发人员创建使用Docker的单击按钮样式,以进行本地开发.

I am tasked with creating a click-button style use of Docker for developers of a Django app to do local development.

我正在将 docker-compose 与我的Docker Hub上的私有存储库结合使用,并且运行良好.

I am using docker-compose in combination with private repos on my Docker Hub and it's working well.

该应用没有默认数据.开发人员已要求使用完整的产品转储,而不是使用带有 loaddata 的固定装置.

Except that the app has no default data. The developers have requested to use a full prod dump rather than fixtures with loaddata.

因此,我基于此Dockerfile构建了一个容器

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"

一切正常,除了在容器启动后花很长时间才能看到 localhost:5000 .

All works well except that it takes a very long time to be able to see localhost:5000 after the containers are up.

因此,我开始调查 仅数据容器,但是我想知道将容器推入Docker Hub会发生什么情况?可以将它与从 docker-compose 烘焙到的mysql数据一起拉吗?

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?

如果没有,那我该如何修复上面的 Dockerfile

If not, then how can I fix the above Dockerfile

推荐答案

MySQL(以及Percona Server之类的变体)提供了一种工具,用于在 docker枢纽页面.

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.

首次启动容器时,将使用指定的名称将被创建并使用提供的名称进行初始化配置变量.此外,它将使用以下命令执行文件在以下位置找到扩展名.sh,.sql和.sql.gz/docker-entrypoint-initdb.d.文件将按字母顺序执行命令.您可以通过安装SQL轻松地填充mysql服务转储到该目录并提供具有贡献的自定义图像数据.默认情况下,SQL文件将导入到指定的数据库中通过MYSQL_DATABASE变量.

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.

使用此技术的Dockerfile看起来像

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运行该映像时,docker将创建容器,

When you 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.

您可以重新使用容器以加快启动时间-后续容器启动将不需要初始化数据库或为数据添加种子.当您需要原始数据时,请删除容器,然后再次 docker run 镜像.

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.

删除容器时,请记住删除包含db数据的docker卷: docker rm -v $ CONTAINER_NAME .

When you delete the container, remember to delete the docker volume containing the db data: docker rm -v $CONTAINER_NAME.

我使用此方法为语言/框架POC和CI提供标准数据/光盘.初始化新数据库并播种大量数据可能需要花费几分钟,因此您将需要一些逻辑来暂停自动操作,直到成功建立数据库连接为止.

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.

希望这对您有所帮助.

这篇关于为Dockerized Django应用播种MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆