型材
使用配置文件,您可以定义一组活动配置文件,以便您的 Compose 应用程序模型可以针对各种用途和环境进行调整。
services
顶级元素支持定义命名配置文件列表的属性。profiles
没有属性的服务profiles
始终处于启用状态。
当列出的服务均不与活动服务匹配时,Compose 将忽略该服务profiles
,除非该服务是命令明确指定的目标。在这种情况下,其配置文件将添加到活动配置文件集中。
笔记
所有其他顶级元素不受其影响
profiles
并且始终处于活动状态。
对其他服务的引用(通过links
或extends
共享资源语法service:xxx
)不会自动启用原本会被活动配置文件忽略的组件。相反,Compose 返回错误。
说明性示例
services:
web:
image: web_image
test_lib:
image: test_lib_image
profiles:
- test
coverage_lib:
image: coverage_lib_image
depends_on:
- test_lib
profiles:
- test
debug_lib:
image: debug_lib_image
depends_on:
- test_lib
profiles:
- debug
在上面的例子中:
- 如果在未启用配置文件的情况下解析 Compose 应用程序模型,则它仅包含服务
web
。 - 如果启用配置文件
test
,则模型包含 servicestest_lib
和coverage_lib
,以及web
始终启用的 service 。 - 如果启用配置文件
debug
,则模型包含web
和debug_lib
服务,但不包含test_lib
和coverage_lib
,因此该模型对于depends_on
的约束无效debug_lib
。 - 如果启用了配置文件
debug
和test
,则模型包含所有服务;web
,test_lib
,coverage_lib
和debug_lib
。 - 如果 Compose 作为
test_lib
要运行的显式服务来执行,test_lib
则test
即使test
未启用配置文件,配置文件也会处于活动状态。 - 如果 Compose 作为
coverage_lib
要运行的显式服务来执行,则服务coverage_lib
和配置文件test
将处于活动状态并由约束test_lib
拉入depends_on
。 - 如果 Compose 作为要运行的显式服务来执行,则该模型对于的约束
debug_lib
再次无效,因为和没有 列出公共项。depends_on
debug_lib
debug_lib
test_lib
profiles
- 如果 Compose 作为
debug_lib
要运行的显式服务来执行并且test
启用了配置文件,则配置文件debug
将自动启用,并且服务将作为启动 services和test_lib
的依赖项拉入。debug_lib
test_lib
profiles
了解如何在
Docker Compose中使用。