[英]How to set Docker Compose `env_file` relative to `.yml` file when multiple `--file` option is used?
I am trying to set my env_file
configuration to be relative to each of the multiple docker-compose.yml
file locations instead of relative to the first docker-compose.yml
.我正在尝试将我的
env_file
配置设置为相对于多个docker-compose.yml
文件位置中的每一个,而不是相对于第一个docker-compose.yml
。
The documentation ( https://docs.docker.com/compose/compose-file/compose-file-v3/#env_file ) suggests this should be possible:文档( https://docs.docker.com/compose/compose-file/compose-file-v3/#env_file )表明这应该是可能的:
If you have specified a Compose file with docker-compose -f FILE, paths in env_file are relative to the directory that file is in.
如果您使用 docker-compose -f FILE 指定了 Compose 文件,则 env_file 中的路径相对于该文件所在的目录。
For example, when I issue例如,当我发出
docker compose \
--file docker-compose.yml \
--file backend/docker-compose.yml \
--file docker-compose.override.yml up
all of the env_file
paths in the second (ie backend/docker-compose.yml
) and third (ie docker-compose.override.yml
) are relative to the location of the first file (ie docker-compose.yml
)第二个(即
backend/docker-compose.yml
)和第三个(即docker-compose.override.yml
)中的所有env_file
路径都相对于第一个文件的位置(即docker-compose.yml
)
I would like to have the env_file
settings in each docker-compose.yml
file to be relative to the file that it is defined in.我希望每个
docker-compose.yml
文件中的env_file
设置都与定义它的文件相关。
Is this possible?这可能吗?
Thank you for your time感谢您的时间
In case you are curious about the context:如果您对上下文感到好奇:
I would like to have a backend repo that is self-contained and the backend developer can just work on it without needing the frontend container.我想要一个自包含的后端存储库,后端开发人员可以在不需要前端容器的情况下对其进行处理。 The frontend repo will pull in the backend repo as a Git submodule, because the frontend container needs the backend container as a dependency.
前端 repo 将作为 Git 子模块拉入后端 repo,因为前端容器需要后端容器作为依赖项。 Here are my 2 repo's:
这是我的 2 个回购:
The backend is organized like this:后端是这样组织的:
/docker-compose.yml
/docker-compose.override.yml
The frontend is organized like this:前端是这样组织的:
/docker-compose.yml
/docker-compose.override.yml
/backend/ # pulled in as a Git submodule
/backend/docker-compose.yml
/backend/docker-compose.override.yml
Everything works if I place my env_file
inside the docker-compose.override.yml
file.如果我将
env_file
放在docker-compose.override.yml
文件中,一切正常。 The backend's override env_file
will be relative to the backend docker-compose.yml
.后端的覆盖
env_file
将相对于后端docker-compose.yml
。 And the frontend's override env_file
will be relative to the frontend docker-compose.yml
.并且前端的覆盖
env_file
将相对于前端docker-compose.yml
。 The frontend will never use the backend's docker-compose.override.yml
.前端永远不会使用后端的
docker-compose.override.yml
。
But I wanted to put the backend's env_file
setting in to the backend's docker-compose.yml
instead, so that projects needing the backend container can inherit and just use it's defaults.但我想将后端的
env_file
设置放入后端的docker-compose.yml
中,以便需要后端容器的项目可以继承并使用它的默认值。 If the depender project wants to override backend's env_file
, then it can do so in the depender project's docker-compose.override.yml
.如果依赖项目想要覆盖后端的
env_file
,那么它可以在依赖项目的docker-compose.override.yml
中这样做。
I hope that makes sense.我希望这是有道理的。
If there's another pattern to organizing Docker-Compose projects that handles this scenario, please let me know.如果有另一种模式来组织处理这种情况的 Docker-Compose 项目,请告诉我。
It turns out that there's already an issue and discussion regarding this:事实证明,已经有一个关于这个的问题和讨论:
The thread points out that this is the expected behavior and is documented here: https://docs.docker.com/compose/extends/#understanding-multiple-compose-files该线程指出这是预期的行为,并在此处记录: https://docs.docker.com/compose/extends/#understanding-multiple-compose-files
When you use multiple configuration files, you must make sure all paths in the files are relative to the base Compose file (the first Compose file specified with -f).
当您使用多个配置文件时,您必须确保文件中的所有路径都相对于基本 Compose 文件(使用 -f 指定的第一个 Compose 文件)。 This is required because override files need not be valid Compose files.
这是必需的,因为覆盖文件不必是有效的 Compose 文件。 Override files can contain small fragments of configuration.
覆盖文件可以包含配置的小片段。 Tracking which fragment of a service is relative to which path is difficult and confusing, so to keep paths easier to understand, all paths must be defined relative to the base file.
跟踪服务的哪个片段与哪个路径相关是困难且令人困惑的,因此为了使路径更易于理解,必须相对于基本文件定义所有路径。
There's a workaround within that discussion that works fairly well: https://github.com/docker/compose/issues/3874#issuecomment-470311052该讨论中有一个非常有效的解决方法: https://github.com/docker/compose/issues/3874#issuecomment-470311052
The workaround is to use a ENV var that has a default:解决方法是使用具有默认值的 ENV var:
- ${PROXY:-.}/haproxy/conf:/usr/local/etc/haproxy
${PROXY:-.}/haproxy/conf:/usr/local/etc/haproxy
Or in my case:或者在我的情况下:
env_file:
- ${BACKEND_BASE:-.}/.env
Hope that can be helpful for others希望对其他人有帮助
In case anyone is interested in the full code: backend
's docker-compose.yml
: https://gitlab.com/starting-spark/porter/backend/-/blob/3.4.3/docker-compose.yml#L13-14如果有人对完整代码感兴趣:
backend
的docker-compose.yml
: https://gitlab.com/starting-spark/porter/backend/-/blob/3.4.3/docker-compose.yml#L13-14
backend
's docker-compose.override.yml
: https://gitlab.com/starting-spark/porter/backend/-/blob/3.4.3/docker-compose.override.yml#L3-4 backend
的docker-compose.override.yml
: https://gitlab.com/starting-spark/porter/backend/-/blob/3.4.3/docker-compose.override.yml#L3-4
backend
's .env
: https://gitlab.com/starting-spark/porter/backend/-/blob/3.4.3/.env backend
的.env
: https://gitlab.com/starting-spark/porter/backend/-/blob/3.4.3/.env
frontend
's docker-compose.yml
: https://gitlab.com/starting-spark/porter/frontend/-/blob/3.2.2/docker-compose.yml#L5-6 frontend
的docker-compose.yml
: https://gitlab.com/starting-spark/porter/frontend/-/blob/3.2.2/docker-compose.yml#L5-6
frontend
's docker-compose.override.yml
: https://gitlab.com/starting-spark/porter/frontend/-/blob/3.2.2/docker-compose.override.yml#L3-4 frontend
的docker-compose.override.yml
: https://gitlab.com/starting-spark/porter/frontend/-/blob/3.2.2/docker-compose.override.yml#L3-4
frontend
's .env
: https://gitlab.com/starting-spark/porter/frontend/-/blob/3.2.2/.env#L16 frontend
的.env
: https://gitlab.com/starting-spark/porter/frontend/-/blob/3.2.2/.env#L16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.