Docker多种环境 [英] Docker multiple environments
问题描述
我想把头放在Docker上,但是很难弄清楚。我试图在我的小项目(MERN堆栈)中实现它,并且我在思考如何区分开发(可能是登台)和生产环境。
I'm trying to wrap my head around Docker, but I'm having a hard time figuring it out. I tried to implement it in my small project (MERN stack), and I was thinking how do you distinct between development, (maybe staging), and production environments.
I看到一个示例,其中他们使用了2个Docker文件和2个docker-compose文件,(每个对一个环境,所以Dockerfile + docker-compose.yml用于生产,Dockerfile-dev + docker-compose-dev.yml用于开发)。
I saw one example where they used 2 Docker files, and 2 docker-compose files, (each pair for one env, so Dockerfile + docker-compose.yml for prod, Dockerfile-dev + docker-compose-dev.yml for dev).
对我来说有点矫kill过正。我宁愿只在两个文件中使用它。
But this just seems like a bit of an overkill for me. I would prefer to have it only in two files.
另外一个问题是例如为了开发,我想在全球范围内安装nodemon,而不是为了安装。
Also one of the problem is that e.g. for development I want to install nodemon globally, but not for poduction.
在完美的解决方案中,我想运行这样的东西
In perfect solution I imagine running something like that
docker-compose -e ENV=dev build
docker-compose -e ENV=dev up
请记住,我仍然没有完全使用docker,因此,如果您对docker有一些我的误解,则可以指出。
Keep in mind, that I still don't fully get docker, so if you caught some of mine misconceptions about docker, you can point them out.
推荐答案
您可以从 在生产环境中使用Compose
您几乎肯定会想要对应用程序配置进行更合适的更改居住环境。这些更改可能包括:
You’ll almost certainly want to make changes to your app configuration that are more appropriate to a live environment. These changes may include:
- 删除应用程序代码的所有卷绑定,以使代码保留在容器内,并且不能从外部更改
- 绑定到主机上的不同端口
- 以不同方式设置环境变量(例如,减少日志记录的冗长性或启用电子邮件发送功能)
- 指定重启策略(例如,重启:始终)以避免停机
- 添加额外的服务(例如,日志聚合器)
- Removing any volume bindings for application code, so that code stays inside the container and can’t be changed from outside
- Binding to different ports on the host
- Setting environment variables differently (e.g., to decrease the verbosity of logging, or to enable email sending)
- Specifying a restart policy (e.g., restart: always) to avoid downtime
- Adding extra services (e.g., a log aggregator)
然后,该建议与您提到的示例不太相似:
The advice is then not quite similar to the example you mention:
由于这个原因,您可能想定义一个附加的Compose文件,例如
production.yml
,该文件指定了适合生产的文件。组态。此配置文件只需要包含您想要对原始Compose文件进行的更改。
For this reason, you’ll probably want to define an additional Compose file, say
production.yml
, which specifies production-appropriate configuration. This configuration file only needs to include the changes you’d like to make from the original Compose file.
docker-compose -f docker-compose.yml -f production.yml up -d
这种 覆盖机制 比尝试混合更好
This overriding mechanism is better than trying to mix dev and prod logic in one compose file, with environment variable to try and select one.
注意:如果您命名第二个dockerfile docker-compose,则dev和prod逻辑在一个撰写文件中,并使用环境变量尝试选择一个。 .override.yml
,一个简单的 docker-compose up
会自动读取覆盖。
但是,在您的情况下,基于环境的名称更清晰。
Note: If you name your second dockerfile docker-compose.override.yml
, a simple docker-compose up
would read the overrides automatically.
But in your case, a name based on the environment is clearer.
这篇关于Docker多种环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!