在 Compose 中使用 Profiles

Profile 通过选择性地激活服务,帮助您调整 Compose 应用程序以适应不同的环境或用例。服务可以分配给一个或多个 Profile;未分配 Profile 的服务默认启动/停止,而分配 Profile 的服务仅在其 Profile 处于活动状态时启动/停止。这种设置意味着特定服务(例如用于调试或开发的服务)可以包含在单个 compose.yml 文件中,并且仅在需要时激活。

为服务分配 Profile

服务通过 profiles 属性与 Profile 关联,该属性接受 Profile 名称数组。

services:
  frontend:
    image: frontend
    profiles: [frontend]

  phpmyadmin:
    image: phpmyadmin
    depends_on: [db]
    profiles: [debug]

  backend:
    image: backend

  db:
    image: mysql

在这里,服务 frontendphpmyadmin 分别被分配到 Profile frontenddebug,因此只有在其各自的 Profile 启用时才会启动。

没有 profiles 属性的服务始终启用。在这种情况下,运行 docker compose up 只会启动 backenddb

有效的 Profile 名称遵循正则表达式格式 [a-zA-Z0-9][a-zA-Z0-9_.-]+

提示

您的应用程序的核心服务不应被分配 profiles,这样它们将始终启用并自动启动。

启动特定 Profile

要启动特定 Profile,请提供 --profile 命令行选项或使用 COMPOSE_PROFILES 环境变量

$ docker compose --profile debug up
$ COMPOSE_PROFILES=debug docker compose up

这两个命令都会启动启用 debug Profile 的服务。在之前的 compose.yaml 文件中,这会启动服务 dbbackendphpmyadmin

启动多个 Profile

您也可以启用多个 Profile,例如使用 docker compose --profile frontend --profile debug up 命令会启用 frontenddebug Profile。

可以通过传递多个 --profile 标志或为 COMPOSE_PROFILES 环境变量提供逗号分隔的列表来指定多个 Profile。

$ docker compose --profile frontend --profile debug up
$ COMPOSE_PROFILES=frontend,debug docker compose up

如果您想同时启用所有 Profile,可以运行 docker compose --profile "*"

自动启动 Profile 和依赖解析

当通过命令行显式指定带有已分配 profiles 的服务时,其 Profile 会自动启动,因此您无需手动启动它们。这可用于一次性服务和调试工具。例如,考虑以下配置:

services:
  backend:
    image: backend

  db:
    image: mysql

  db-migrations:
    image: backend
    command: myapp migrate
    depends_on:
      - db
    profiles:
      - tools
# Only start backend and db
$ docker compose up -d

# This runs db-migrations (and, if necessary, start db)
# by implicitly enabling the profiles "tools"
$ docker compose run db-migrations

但请记住,docker compose 只会自动启动命令行上指定服务的 Profile,而不会自动启动其任何依赖项的 Profile。

这意味着目标服务 depends_on 的任何其他服务应该要么:

  • 共享一个共同的 Profile
  • 始终启动,通过省略 profiles 或显式启动匹配的 Profile
services:
  web:
    image: web

  mock-backend:
    image: backend
    profiles: ["dev"]
    depends_on:
      - db

  db:
    image: mysql
    profiles: ["dev"]

  phpmyadmin:
    image: phpmyadmin
    profiles: ["debug"]
    depends_on:
      - db
# Only start "web"
$ docker compose up -d

# Start mock-backend (and, if necessary, db)
# by implicitly enabling profiles "dev"
$ docker compose up -d mock-backend

# This fails because profiles "dev" is not enabled
$ docker compose up phpmyadmin

尽管指定 phpmyadmin 会自动启动 debug Profile,但它不会自动启动 db 所需的 Profile,即 dev

要解决此问题,您需要将 debug Profile 添加到 db 服务中:

db:
  image: mysql
  profiles: ["debug", "dev"]

或显式启动 dev Profile。

# Profiles "debug" is started automatically by targeting phpmyadmin
$ docker compose --profile dev up phpmyadmin
$ COMPOSE_PROFILES=dev docker compose up phpmyadmin

停止应用程序和带有特定 Profile 的服务

与启动特定 Profile 一样,您可以使用 --profile 命令行选项或使用 COMPOSE_PROFILES 环境变量

$ docker compose --profile debug down
$ COMPOSE_PROFILES=debug docker compose down

这两个命令都会停止并移除带有 debug Profile 和没有 Profile 的服务。在以下 compose.yaml 文件中,这会停止服务 dbbackendphpmyadmin

services:
  frontend:
    image: frontend
    profiles: [frontend]

  phpmyadmin:
    image: phpmyadmin
    depends_on: [db]
    profiles: [debug]

  backend:
    image: backend

  db:
    image: mysql

如果您只想停止 phpmyadmin 服务,可以运行:

$ docker compose down phpmyadmin

$ docker compose stop phpmyadmin

注意

运行 docker compose down 只会停止 backenddb

参考信息

profiles

页面选项