多个 docker compose 文件,指定 env_file [英] Multiple docker compose files, with env_file specified
问题描述
我正在开发一个项目,我正在使用 docker 创建基本配置并对其进行扩展.我的基础 docker-compose.yml 文件有 links
,所以我不能在扩展 compose 文件中使用 extends
参数.相反,我为 docker-compose
工具指定了多个 docker-compose 文件.
I'm working on a project where I'm using docker to create a base configuration, and extending it. My base docker-compose.yml file has links
, so I can't use the extends
param in the extending compose file. Instead, I'm specifying multiple docker-compose files to the docker-compose
tool.
这很好用,只是 .env
文件的路径似乎总是相对于指定的第一个 docker-compose.yml 文件,这破坏了我的预期功能.
This is working well, except that it seems that the path to the .env
file is always relative to the first docker-compose.yml file specified, which breaks my intended functionality.
例子:
docker-compose.yml(生活在一个共同的位置,比如说 ~/base-app
)
docker-compose.yml (lives in a common location, let's say ~/base-app
)
web:
build: .
volumes:
- .:/usr/src/app
ports:
- "80:3000"
links:
- redis
redis:
image: redis:latest
ports:
- "6379:6379"
现在在子应用程序中,我想指定一个 command
和 env_file
:
Now in a child app, I'd like to specify a command
and env_file
:
docker-compose.override.yml
web:
environment:
NODE_ENV: development
env_file: .env
command: node server.js
当我运行此设置时,我会使用以下命令:
When I run this setup, I'd use the following command:
docker-compose -f ~/base-app/docker-compose.yml -f docker-compose.override.yml up
只要我不指定 env_file
就可以了.但是指定 env_file
后,我会收到如下错误:
This works well, as long as I don't specify an env_file
. But with the env_file
specified, I'll get an error like:
ERROR: Couldn't find env file: ~/base-app/.env
阅读 docker-compose docs,它说使用 -f
标志时,env 文件路径是相对于 docker-compose 文件的.
Reading the docker-compose docs, it says that the env file path is relative to the docker-compose file when using the -f
flag.
我错过了什么,还是这是一个错误?相对于覆盖 docker-compose 文件,使用 .env
文件的干净方式是什么?
Am I missing something, or is this a bug? What would be a clean way of using an .env
file relative to an override docker-compose file?
推荐答案
就我个人而言,对我有用并且我很高兴的是我在所有项目的根目录中有一个空的 docker-compose 文件,我需要在它们之间进行交叉引用,并始终将其设置为所有撰写命令中的第一个 -f
,并将我的所有撰写文件更改为使用第一个撰写文件所在的解决方案/源的根目录的相对路径.
Personally what worked for me and am quiet happy is i have an empty docker-compose file at the root of all my projects that I need to cross reference between, and always set that as the first -f
in all compose commands, and have changed all my compose files to use paths relative from the root of my solution/source where the first compose file lives.
/src <- root
/src/docker-compose.yml <- empty docker-compose file only specifying version
/src/api/docker-compose.yml <- docker-compose file using paths relative from /src/
/src/web/docker-compose.yml <- docker-compose file using paths relative from /src/
然后我有一些调用 docker-compose 的批处理/powershell 文件,例如:
I then have some batch/powershell files that invoke docker-compose such as:
/** docker-compose-re-up.bat **/
docker-compose -f ../docker-compose.yml -f docker-compose.yml down
docker-compose -f ../docker-compose.yml -f docker-compose.yml build
docker-compose -f ../docker-compose.yml -f docker-compose.yml up -d
这篇关于多个 docker compose 文件,指定 env_file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!