docker-compose-数据库迁移和其他前/后脚本 [英] docker-compose - database migrations and other pre/post scripts

查看:140
本文介绍了docker-compose-数据库迁移和其他前/后脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例django应用程序,正在尝试使用docker启动并运行。

I have a sample django app that I am trying to get up and running using docker.

docker-compose up 调出Web,db和其他容器以及它们之间的链接。但是有一些前脚本和后脚本可能需要运行。.

docker-compose up brings up the web, db and other containers along with links between them. But there are pre and post scripts that might need to be run..

我的场景中的前脚本示例:

git
pip
docker
docker-compose
wget

后记示例:

数据库迁移,通常在容器启动并运行后使用 docker run web ... 手动完成。

Database migrations, usually done manually using docker run web... after the containers are up and running.

当前我在应用程序的根目录下有一个deploy.sh,它遵循如下逻辑。(我在启动时选择ubuntu映像)

Currently I have a deploy.sh at the root of app which follows logic like this..(I choose a ubuntu image when launching)

#assuming I always choose ubuntu base image
sudo apt-get install x
sudo apt-get install y
sudo apt-get install z
docker-compose build .; docker-compose up -d;
docker-compose run web "python manage.py makemigrations"

我的问题:

1)运行这些命令的最佳方法是什么?

1) what is the best way to run these commands?

2)每次部署时是否都在运行数据库迁移(从头开始?)-还是按卷解决此问题?

2) Are database migrations run each time you deploy (from scratch?) - or is this issue taken care of by volumes?

推荐答案

您有两个选择:


  1. 您可以在dockerfile中为图像运行以下命令;由于每个dockerfile在compose运行时都运行-您的映像将具有这些命令的结果。当您进行操作系统级别的升级和配置引导时(例如您的 apt-get 命令),这特别有用。

  1. You can run these commands in the dockerfile for your images; as each dockerfile is run when compose is running - your images will have the results of these commands. This is particularly useful when you are doing os-level upgrades and configuration bootstrapping (like your apt-get commands).

对于运行时级别配置(系统启动后需要执行的操作),请在docker-compose.yml文件中使用 command 指令。这些将是您的迁移(如果您需要每次运行它们)。

For runtime-level configuration (things you need to do once the system is up), use the command directive in your docker-compose.yml file. These would be your migrations (if you need to run them each time).

如果您要保留数据跨docker compose运行(也就是说,当您重新启动容器时,您的数据应保留);那么您需要针对主机的持久映射或共享的数据卷-您也可以在docker-compose.yml中进行配置。

If you want to persist your data across runs of docker compose (that is, your data should remain when you restart the container); then you need either persistent mapping against your host or a data volume that's shared - which you can configure in your docker-compose.yml as well.

docker-compose会很高兴运行您提供的任何脚本-它不知道是否需要运行它,而只是执行命令。确保您的前,后,引导脚本足够智能,即使已经应用了有效的结果,它们也可以重复执行。

docker-compose will happily run whatever script you provide - it doesn't know if it needs to run it, its just executing commands. It is up to you to make sure your pre, post, bootstrap scripts are intelligent enough that they can be repeated even if their effective results are already applied.

这篇关于docker-compose-数据库迁移和其他前/后脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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