使用 GPU 存取權執行 Docker Compose 服務

如果 Docker 主機包含 GPU 裝置且 Docker Daemon 已進行相應設定,則 Compose 服務可以定義 GPU 裝置保留。為此,請確保您已安裝 先決條件(若尚未安裝)。

以下章節中的範例專門介紹如何透過 Docker Compose 為服務容器提供 GPU 裝置存取權。您可以使用 docker-composedocker compose 命令。如需更多資訊,請參閱 遷移至 Compose V2

為服務容器啟用 GPU 存取

compose.yaml 檔案中,服務若需要 GPU,可使用 Compose Deploy 規範中的 device 屬性來參照 GPU。

這可提供對 GPU 保留更細緻的控制,因為可以為以下裝置屬性設定自訂值:

  • capabilities:此值指定為字串列表。例如,capabilities: [gpu]。您必須在 Compose 檔案中設定此欄位,否則在部署服務時會傳回錯誤。
  • count:指定為整數或值 all,代表應保留的 GPU 裝置數量(前提是主機擁有該數量的 GPU)。如果 count 設定為 all 或未指定,則預設會使用主機上所有可用的 GPU。
  • device_ids:此值指定為字串列表,代表來自主機的 GPU 裝置 ID。您可以在主機上執行 nvidia-smi 的輸出中找到裝置 ID。如果未設定 device_ids,則預設會使用主機上所有可用的 GPU。
  • driver:指定為字串,例如 driver: 'nvidia'
  • options:代表驅動程式特定選項的鍵值對。
重要

您必須設定 capabilities 欄位,否則在部署服務時會傳回錯誤。

注意

countdevice_ids 是互斥的。您一次只能定義一個欄位。

如需有關這些屬性的更多資訊,請參閱 Compose Deploy 規範

範例:一個用於執行可存取 1 個 GPU 裝置的服務之 Compose 檔案

services:
  test:
    image: nvidia/cuda:12.9.0-base-ubuntu22.04
    command: nvidia-smi
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

使用 Docker Compose 執行

$ docker compose up
Creating network "gpu_default" with the default driver
Creating gpu_test_1 ... done
Attaching to gpu_test_1    
test_1  | +-----------------------------------------------------------------------------+
test_1  | | NVIDIA-SMI 450.80.02    Driver Version: 450.80.02    CUDA Version: 11.1     |
test_1  | |-------------------------------+----------------------+----------------------+
test_1  | | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
test_1  | | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
test_1  | |                               |                      |               MIG M. |
test_1  | |===============================+======================+======================|
test_1  | |   0  Tesla T4            On   | 00000000:00:1E.0 Off |                    0 |
test_1  | | N/A   23C    P8     9W /  70W |      0MiB / 15109MiB |      0%      Default |
test_1  | |                               |                      |                  N/A |
test_1  | +-------------------------------+----------------------+----------------------+
test_1  |                                                                                
test_1  | +-----------------------------------------------------------------------------+
test_1  | | Processes:                                                                  |
test_1  | |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
test_1  | |        ID   ID                                                   Usage      |
test_1  | |=============================================================================|
test_1  | |  No running processes found                                                 |
test_1  | +-----------------------------------------------------------------------------+
gpu_test_1 exited with code 0

在擁有多個 GPU 的機器上,可以設定 device_ids 欄位以鎖定特定的 GPU 裝置,並使用 count 來限制分配給服務容器的 GPU 裝置數量。

您可以在每個服務定義中使用 countdevice_ids。如果您嘗試兩者並用、指定無效的裝置 ID,或使用的 count 值超過系統中的 GPU 數量,則會傳回錯誤。

$ nvidia-smi   
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.80.02    Driver Version: 450.80.02    CUDA Version: 11.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla T4            On   | 00000000:00:1B.0 Off |                    0 |
| N/A   72C    P8    12W /  70W |      0MiB / 15109MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  Tesla T4            On   | 00000000:00:1C.0 Off |                    0 |
| N/A   67C    P8    11W /  70W |      0MiB / 15109MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   2  Tesla T4            On   | 00000000:00:1D.0 Off |                    0 |
| N/A   74C    P8    12W /  70W |      0MiB / 15109MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   3  Tesla T4            On   | 00000000:00:1E.0 Off |                    0 |
| N/A   62C    P8    11W /  70W |      0MiB / 15109MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

存取特定裝置

若要僅允許存取 GPU-0 和 GPU-3 裝置:

services:
  test:
    image: tensorflow/tensorflow:latest-gpu
    command: python -c "import tensorflow as tf;tf.test.gpu_device_name()"
    deploy:
      resources:
        reservations:
          devices:
          - driver: nvidia
            device_ids: ['0', '3']
            capabilities: [gpu]
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.