Profiles
目录
通过 Profiles,您可以定义一组活动 Profiles,以便根据各种用途和环境调整您的 Compose 应用模型。
顶层元素 services 支持 profiles
属性,用于定义命名 Profiles 列表。没有 profiles
属性的服务总是启用的。
当列出的 profiles
没有一个与活动的 Profiles 匹配时,Compose 会忽略该服务,除非该服务被命令明确指定。在这种情况下,其 Profile 将被添加到活动 Profiles 的集合中。
注意
所有其他顶层元素不受
profiles
影响,并且始终处于活动状态。
对其他服务的引用(通过 links
、extends
或共享资源语法 service:xxx
)不会自动启用一个否则会被活动 Profiles 忽略的组件。相反,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 应用模型时未启用任何 Profile,它将只包含
web
服务。 - 如果启用了
test
Profile,模型将包含test_lib
和coverage_lib
服务,以及总是启用的web
服务。 - 如果启用了
debug
Profile,模型将包含web
和debug_lib
服务,但不包含test_lib
和coverage_lib
,因此根据debug_lib
的depends_on
约束,该模型是无效的。 - 如果启用了
debug
和test
Profiles,模型将包含所有服务:web
、test_lib
、coverage_lib
和debug_lib
。 - 如果 Compose 执行时明确指定运行
test_lib
服务,即使test
Profile 未启用,test_lib
和test
Profile 也会处于活动状态。 - 如果 Compose 执行时明确指定运行
coverage_lib
服务,服务coverage_lib
和test
Profile 将处于活动状态,并且test_lib
将通过depends_on
约束被拉入。 - 如果 Compose 执行时明确指定运行
debug_lib
服务,则根据debug_lib
的depends_on
约束,模型仍然无效,因为debug_lib
和test_lib
没有列出共同的profiles
。 - 如果 Compose 执行时明确指定运行
debug_lib
服务且启用了test
Profile,则debug
Profile 会自动启用,并且服务test_lib
将作为依赖项被拉入,从而启动debug_lib
和test_lib
这两个服务。
请参阅如何在 Docker Compose 中使用 profiles
。