如何使配置文件默认为 docker-compose?

mer*_*hoo 9 docker docker-compose

我有一个 docker compose 文件,其中包含 2 个专有配置文件localdev,我们使用它进行本地测试并拉下开发环境进行调试。

有没有一种方法可以默认为,local以便我们可以docker-compose build在不指定的情况下使用--profile,并且 docker 将默认为local

cod*_*onk 9

您需要使用COMPOSE_PROFILES环境变量。要始终设置它,您可以输入:

COMPOSE_PROFILES=local/etc/环境中

或者...

export COMPOSE_PROFILES=local~/.bashrc~/.zshrc中

然后重新启动你的外壳。您可以输入以下内容以确保您的 shell 已获取环境变量:

echo $COMPOSE_PROFILES

  • 是的,要切换到 dev 配置文件,您可以临时更改 COMPOSE_PROFILES 变量:“COMPOSE_PROFILES=dev docker-compose up”:这将为该单个命令启用“dev”配置文件,而无需更改默认的“local”配置文件。 (2认同)
  • 很好的答案 - 仍然遗憾的是无法在文件中执行此操作,并且设置“COMPOSE_PROFILES”变量将需要用户预先执行操作。如果能够拥有这个或那个配置文件的默认值,而不是当前的默认配置文件,因为当前的默认配置文件是空的并且不能轻易关闭,那就太棒了。 (2认同)

And*_*ore 8

我只是在玩这个;事实证明,使用空字符串作为配置文件的服务将在未指定配置文件时启动,但与没有指定配置文件的服务不同,在指定另一个配置文件时不会启动。

\n

虽然应该指出的是,这没有记录在案,因此功能将来可能会发生变化,但我认为在变量不足的情况下实现一个不错的“默认”配置文件功能应该足够了COMPOSE_PROFILES

\n

例如

\n
services:\n    a:\n        image: busybox\n        profiles:\n            - \'\'\n    b:\n        image: busybox\n        profiles:\n            - \'\'\n            - a\n    c:\n        image: busybox\n        profiles:\n            - a\n
Run Code Online (Sandbox Code Playgroud)\n
aj@laptop:~/test$ docker compose up\n[+] Running 2/0\n \xe2\x9c\x94 Container test-b-1  Created                          0.0s \n \xe2\x9c\x94 Container test-a-1  Created                          0.0s \nAttaching to test-a-1, test-b-1\ntest-a-1 exited with code 0\ntest-b-1 exited with code 0\n\naj@laptop:~/test$ docker compose --profile a up\n[+] Running 2/0\n \xe2\x9c\x94 Container test-c-1  Created                          0.0s \n \xe2\x9c\x94 Container test-b-1  Created                          0.0s \nAttaching to test-b-1, test-c-1\ntest-b-1 exited with code 0\ntest-c-1 exited with code 0\n
Run Code Online (Sandbox Code Playgroud)\n