執行支援 GPU 訪問的 Docker Compose 服務
目錄
如果 Docker 主機包含 GPU 裝置且 Docker Daemon 已相應設定,Compose 服務可以定義 GPU 裝置預留。為此,請確保您已安裝先決條件。
以下各節中的示例重點介紹如何透過 Docker Compose 為服務容器提供 GPU 裝置訪問許可權。您可以使用 `docker-compose` 或 `docker compose` 命令。有關更多資訊,請參閱遷移到 Compose V2。
為服務容器啟用 GPU 訪問
在 `compose.yaml` 檔案中,GPU 透過 Compose 部署規範中的裝置屬性進行引用,在需要它們的您的服務中。
這提供了對 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` 欄位。否則,在服務部署時會返回錯誤。
注意`count` 和 `device_ids` 互斥。您一次只能定義一個欄位。
有關這些屬性的更多資訊,請參閱Compose 部署規範。
用於執行具有 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 裝置數量。
您可以在每個服務定義中使用 `count` 或 `device_ids`。如果您嘗試同時使用兩者、指定無效的裝置 ID 或使用超出系統中 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]