使用 Docker Compose 的多容器支持#

默认的 NVIDIA AI Workbench 容器 经过优化,可以支持您的开发工作流程,但在某些情况下,您可能需要为您的项目提供额外的隔离环境。当典型的 AI 应用程序的不同部分(例如推理服务器或嵌入模型)具有不同的依赖项时,可能会发生这种情况。AI Workbench 通过使用 Docker Compose 来支持多容器。

警告

仅当您使用 Docker 而不是 Podman 作为您的 容器运行时 时,才支持 Docker Compose。

使用此文档了解以下内容

AI Workbench 中 Docker Compose 的概述#

要使用 Docker Compose,您需要将 compose 文件添加到您的项目,然后在工作时启动和停止 compose 文件环境。compose 文件必须位于项目的根目录或名为 deploy 的文件夹中。有关更多信息,请参阅Docker Compose 文件示例Compose 文件参考

提示

您可以将您的 compose 文件命名为以下名称之一,AI Workbench 将按此顺序使用找到的第一个文件:compose.yamlcompose.ymldocker-compose.ymldocker-compose.yaml/deploy/compose.yaml/deploy/compose.yml/deploy/docker-compose.yml/deploy/docker-compose.yaml

在您的 compose 文件中,您可以定义代表不同场景的多个配置文件。有关更多信息,请参阅 将配置文件与 Compose 结合使用

AI Workbench 不管理为 compose 容器设置绑定挂载值。AI Workbench 创建一个共享卷,所有容器(包括项目容器)都可以使用该卷。挂载在 /nvwb-shared-volume 上可用。

提示

如果您以不同的用户身份运行不同的容器,您可能需要修改项目创建的文件的权限,以便所有容器都可以根据需要读取和写入。

Docker Compose 的桌面应用程序支持#

要在 AI Workbench 桌面应用程序中创建 compose 文件并管理您的 compose 环境,请使用以下步骤。

  1. 在 AI Workbench 桌面应用程序中打开一个项目。

  2. 单击环境以打开环境页面。

  3. 单击 Compose 或滚动到 compose 部分。

  4. 单击创建 compose 文件。将出现创建 compose 文件窗口。

  5. 创建 compose 文件窗口中,编辑您的 compose 文件。完成后,单击保存。有关更多信息,请参阅Docker Compose 文件示例Compose 文件参考

  6. (可选)对于配置文件,选择您要在启动 compose 环境时使用的一个或多个配置文件。

  7. 单击启动以启动项目的 compose 环境。

  8. 单击停止以停止项目的 compose 环境。

Docker Compose 的 CLI 支持#

AI Workbench CLI 通过以下命令支持 Docker Compose

Docker Compose 文件示例#

以下示例演示了 AI Workbench 中的 Docker Compose 文件支持

简单 Compose 文件示例#

以下是一个示例 Docker compose 文件,其中包含一个 Web 应用程序服务。此服务上没有配置文件,因此它始终运行。

 1services:
 2
 3   web1:
 4      # Using build: builds the image from a local dockerfile in the project
 5      image: hashicorp/http-echo
 6      environment:
 7         # Setting the NVWB_TRIM_PREFIX env var causes this service to be routed through the proxy.
 8         # NVWB_TRIM_PREFIX=true trims the proxy prefix.
 9         # The env var PROXY_PREFIX is injected into the service if you need it.
10         - NVWB_TRIM_PREFIX=true
11      ports:
12         - '5678:5678'
13      command: ["-text=hello from service 1"]

需要 GPU 的 Web 应用程序示例#

以下是一个示例 Docker compose 文件,其中包含两个 Web 应用程序服务。服务 1 始终运行。服务 2 需要 GPU,并且仅当您选择 gpu-service 配置文件时才运行。

 1services:
 2
 3   web1:
 4      # Using build: builds the image from a local dockerfile in the project
 5      image: hashicorp/http-echo
 6      environment:
 7         # Setting the NVWB_TRIM_PREFIX env var causes this service to be routed through the proxy.
 8         # NVWB_TRIM_PREFIX=true trims the proxy prefix.
 9         # The env var PROXY_PREFIX is injected into the service if you need it.
10         - NVWB_TRIM_PREFIX=true
11      ports:
12         - '5678:5678'
13      command: ["-text=hello from service 1"]
14
15   web2:
16      image: hashicorp/http-echo
17      profiles: [gpu-service]
18      environment:
19         - NVWB_TRIM_PREFIX=true
20      ports:
21         - '5679:5679'
22      # Specify GPU requests in this format.
23      # AI Workbench manages reservations and explicitly passes GPUs into each container,
24      # so you don't have to worry about collisions
25      deploy:
26         resources:
27            reservations:
28               devices:
29                  - driver: nvidia
30                  count: 1
31                  capabilities: [gpu]
32      command: ["-text=hello from service 2", "-listen=:5679"]

包含环境变量和密钥的 Compose 示例#

以下是一个示例 Docker compose 文件,其中包含一个 Web 应用程序服务。此 compose 文件包含一个环境变量和一个密钥。在使用此示例之前,在您的 AI Workbench 项目中创建变量 TEST_VAR 和密钥 TEST_SECRET。有关更多信息,请参阅环境变量密钥(敏感环境变量)

此服务上没有配置文件,因此它始终运行。

 1services:
 2
 3   web3:
 4      # Using build: builds the image from a local dockerfile in the project
 5      image: hashicorp/http-echo
 6      environment:
 7         # Setting the NVWB_TRIM_PREFIX env var causes this service to be routed through the proxy.
 8         # NVWB_TRIM_PREFIX=true trims the proxy prefix.
 9         # The env var PROXY_PREFIX is injected into the service if you need it.
10         - NVWB_TRIM_PREFIX=true
11         # Environment variables set in the project in AI Workbench are available by interpolation like this
12         - TEST_ENV_VAR=${TEST_VAR}
13         # Secrets are also available by interpolation if you prefer that over the file
14         - TEST_SECRET_FROM_ENV_VAR=${TEST_SECRET}
15      ports:
16         - '5678:5678'
17      command: ["-text=${TEST_VAR}"]

Compose 密钥示例#

以下是一个示例 Docker compose 文件,其中包含两个 Web 应用程序服务。服务 1 始终运行。服务 4 使用 compose 密钥,并且仅当您选择 compose-secret 配置文件时才运行。您需要在您的 AI Workbench 项目中设置密钥 TEST_SECRET,此服务才能运行。

 1services:
 2
 3   web1:
 4      # Using build: builds the image from a local dockerfile in the project
 5      image: hashicorp/http-echo
 6      environment:
 7         # Setting the NVWB_TRIM_PREFIX env var causes this service to be routed through the proxy.
 8         # NVWB_TRIM_PREFIX=true trims the proxy prefix.
 9         # The env var PROXY_PREFIX is injected into the service if you need it.
10         - NVWB_TRIM_PREFIX=true
11      ports:
12         - '5678:5678'
13      command: ["-text=hello from service 1"]
14
15   web4:
16      image: hashicorp/http-echo
17      profiles: [compose-secret]
18      environment:
19         - NVWB_TRIM_PREFIX=true
20         # This is an example of how you can use the secret as a file.
21         # Compose mounts the secret there for you
22         - TEST_SECRET_FILE=/run/secrets/TEST_SECRET
23      ports:
24         - '5680:5680'
25      # To use a compose secret, you must set the secret if you want it active on the service.
26      # It should match the secret name in the AI Workbench project
27      secrets:
28         - TEST_SECRET
29      command: ["-text=hello from service 4", "-listen=:5680"]
30
31
32# If you want to use compose secrets, set this global value so compose file validation works,
33# but AI Workbench automatically replaces the value.
34# The name should match the name in AI Workbench (e.g. TEST_SECRET).
35secrets:
36TEST_SECRET:
37   environment: "HOME"