You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

使用Docker Compose调用GPU时遇到的异常问题

Why nvidia-smi works with docker run but not Docker Compose for your Python slim image?

Let's break down the root cause first:

The python:3.8.8-slim-buster image doesn't come with NVIDIA tools like nvidia-smi pre-installed at all. When you use docker run --gpus all, the NVIDIA Docker Runtime automatically mounts your host machine's NVIDIA driver files (including the nvidia-smi binary and its required libraries) into the container—this is why the command works. But when you switch to using Docker Compose's deploy.resources.reservations.devices config, this automatic mounting doesn't trigger properly, leaving the container without access to nvidia-smi.

Fixes you can try:

1. Explicitly specify the NVIDIA Runtime in Docker Compose (Recommended)

This makes your Docker Compose setup behave exactly like the docker run --gpus all command by leveraging the same NVIDIA Runtime mechanism. Update your docker-compose.yml:

services:
  test:
    image: python:3.8.8-slim-buster
    command: sleep infinity
    runtime: nvidia  # This is the key line to add
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [gpu]

Make sure you have Docker Compose 1.28+ (since that's when runtime support was added for GPU setups) and that the NVIDIA Container Toolkit is properly installed on your host.

2. Pre-install NVIDIA tools in your Docker image (Not Recommended)

If you really want the image to include nvidia-smi by default, you can build a custom image based on python:3.8.8-slim-buster and add the NVIDIA tooling. Note that this requires the driver versions in the image to match your host's, which can lead to compatibility issues if your host driver updates. Here's how you'd modify your Dockerfile:

FROM python:3.8.8-slim-buster
# Add NVIDIA repo and install nvidia-smi dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    gnupg2 curl ca-certificates && \
    curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | apt-key add - && \
    curl -s -L https://nvidia.github.io/libnvidia-container/debian10/libnvidia-container.list | \
    tee /etc/apt/sources.list.d/nvidia-container-toolkit.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends nvidia-container-toolkit && \
    # Clean up to keep the image slim
    rm -rf /var/lib/apt/lists/*
CMD ["sleep", "infinity"]

Verification

After applying either fix, start your container with docker-compose up -d, then exec into it with docker-compose exec test nvidia-smi—you should see the same output as when using docker run.

内容的提问来源于stack exchange,提问作者drorpa

火山引擎 最新活动