简体   繁体   English

Docker compose 用于生产和开发

[英]Docker compose for production and development

So I use Python+Django (but it does not really matter for this question)所以我使用 Python+Django(但对于这个问题并不重要)

When I write my code I simply run当我编写代码时,我只需运行

./manage.py runserver 

which does the webserver, static files, automatic reload, etc.它负责网络服务器、静态文件、自动重新加载等。

and and to put it on production I use series of commands like并且为了将其投入生产,我使用了一系列命令,例如

./manage.py collectstatic
./manage.py migrate
uwsgi --http 127.0.0.1:8000 -w wsgi --processes=4

also I have few other services like postgres, redis (which are common for both production and dev)我还有一些其他服务,如 postgres、redis(生产和开发都很常见)

So I'm trying to adapt here docker(+ -compose) and I cannot understand how to split prod/dev with it.所以我试图在这里适应 docker(+ -compose) 但我无法理解如何用它拆分 prod/dev。

basically in docker-compose.yml you define your services and images - but in my case image in production should run one CMD and in dev another..基本上在docker-compose.yml你定义你的服务和图像 - 但在我的例子中,生产中的图像应该运行一个 CMD 而在开发中运行另一个..

what are the best practices to achieve that ?实现这一目标的最佳做法是什么?

You should create additional docker-compose.yml files like docker-compose-dev.yml or docker-compose-pro.yml and override some of the original docker-compose.yml configuration with -f command:您应该创建额外的 docker-compose.yml 文件,如 docker-compose-dev.yml 或 docker-compose-pro.yml 并使用 -f 命令覆盖一些原始的 docker-compose.yml 配置:

docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d

Sometimes, I also use different Dockerfile for different environments and specify dockerfile parameter in docker-compose-pro.yml build section, but I didn't recommend it because you will end with duplicated Dockerfiles.有时,我也会针对不同的环境使用不同的 Dockerfile 并在dockerfile -compose-pro.yml 构建部分中指定dockerfile参数,但我不推荐这样做,因为您将以重复的 Dockerfile 结束。

Update更新

Docker has introduced multi-stage builds feature https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds which allow to create a Dockerfile for different environments. Docker 引入了多阶段构建功能https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds ,它允许为不同的环境创建 Dockerfile。

Usually having a different production and dev starting workflow is a bad idea.通常拥有不同的生产和开发启动工作流程是一个坏主意。 You should always try to keep both dev and prod environments very similar, even in the way you launch your applications.您应该始终尝试使开发环境和生产环境非常相似,即使在启动应用程序的方式上也是如此。 You should always externalize the configuration that is different between the different environments.您应该始终将不同环境之间不同的配置具体化。

Having different startup sequence is maybe acceptable, however having multiple docker images (or dockerfiles) for each environment is a very bad idea.使用不同的启动顺序可能是可以接受的,但是为每个环境使用多个 docker 镜像(或 dockerfiles)是一个非常糟糕的主意。 Docker images should be immutable and portable. Docker 镜像应该是不可变的和可移植的。

However, you might have some constraints.但是,您可能有一些限制。 Docker-compose allows you to override the command that is specified in the image. Docker-compose 允许您覆盖图像中指定的命令。 There is the command property that will override the default command in the image.有一个命令属性将覆盖图像中的默认命令。 I would recommend that you keep the image production ready, ie use something like CMD ./manage.py collectstatic && ./manage.py migrate && uwsgi --http 127.0.0.1:8000 -w wsgi --processes=4 in the Dockerfile.我建议您准备好映像制作,即在CMD ./manage.py collectstatic && ./manage.py migrate && uwsgi --http 127.0.0.1:8000 -w wsgi --processes=4使用类似CMD ./manage.py collectstatic && ./manage.py migrate && uwsgi --http 127.0.0.1:8000 -w wsgi --processes=4之类的东西.

In the compose file just override the CMD by specifying:在撰写文件中,只需通过指定覆盖 CMD:

command: ./manage.py runserver 

Having multiple compose file is usually not a big issue.拥有多个撰写文件通常不是什么大问题。 You can keep your compose files clean and manageable by using some nice compose file features such as extends, where once compose file can extend another one.您可以通过使用一些不错的撰写文件功能(例如扩展)来保持撰写文件的清洁和可管理性,其中一次撰写文件可以扩展另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM