Profiles

目录

通过 Profiles,您可以定义一组活动 Profiles,以便根据各种用途和环境调整您的 Compose 应用模型。

顶层元素 services 支持 profiles 属性,用于定义命名 Profiles 列表。没有 profiles 属性的服务总是启用的。

当列出的 profiles 没有一个与活动的 Profiles 匹配时,Compose 会忽略该服务,除非该服务被命令明确指定。在这种情况下,其 Profile 将被添加到活动 Profiles 的集合中。

注意

所有其他顶层元素不受 profiles 影响,并且始终处于活动状态。

对其他服务的引用(通过 linksextends 或共享资源语法 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_libcoverage_lib 服务,以及总是启用的 web 服务。
  • 如果启用了 debug Profile,模型将包含 webdebug_lib 服务,但不包含 test_libcoverage_lib,因此根据 debug_libdepends_on 约束,该模型是无效的。
  • 如果启用了 debugtest Profiles,模型将包含所有服务:webtest_libcoverage_libdebug_lib
  • 如果 Compose 执行时明确指定运行 test_lib 服务,即使 test Profile 未启用,test_libtest Profile 也会处于活动状态。
  • 如果 Compose 执行时明确指定运行 coverage_lib 服务,服务 coverage_libtest Profile 将处于活动状态,并且 test_lib 将通过 depends_on 约束被拉入。
  • 如果 Compose 执行时明确指定运行 debug_lib 服务,则根据 debug_libdepends_on 约束,模型仍然无效,因为 debug_libtest_lib 没有列出共同的 profiles
  • 如果 Compose 执行时明确指定运行 debug_lib 服务且启用了 test Profile,则 debug Profile 会自动启用,并且服务 test_lib 将作为依赖项被拉入,从而启动 debug_libtest_lib 这两个服务。

请参阅如何在 Docker Compose 中使用 profiles

页面选项