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.
我看到了一个 示例,他们使用了 2 个 Docker 文件和 2 个 docker-compose 文件,(每一对对应一个环境,所以 Dockerfile + docker-compose.yml 对应 prod,Dockerfile-dev + docker-compose-dev.yml 对应 dev).
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).
但这对我来说似乎有点矫枉过正.我宁愿只将它放在两个文件中.
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:
- 移除应用程序代码的所有卷绑定,以便代码保留在容器内且无法从外部更改
- 绑定到主机上的不同端口
- 以不同方式设置环境变量(例如,减少日志记录的详细程度,或启用电子邮件发送)
- 指定重启策略(例如,重启:始终)以避免停机
- 添加额外的服务(例如,日志聚合器)
建议与您提到的示例不太相似:
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
这个覆盖机制比尝试将 dev 和 prod 逻辑混合在一个 compose 文件中,并尝试选择一个环境变量.
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.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屋!