You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中PyTorch的torch.cuda.is_available()返回False求助

Docker容器内PyTorch CUDA不可用问题

Docker容器内PyTorch的torch.cuda.is_available()返回False,但容器外同版本PyTorch运行正常。

环境信息

  • 宿主机:Debian 12
  • GPU:NVIDIA GeForce RTX 3060 Ti(驱动版本:550.54.14)
  • CUDA版本:12.1(宿主机和容器通过nvcc --version均显示该版本)
  • Docker版本:28.0.4,build b8034c0
  • Docker Compose版本:v2.34.0

Docker配置

docker-compose.yml

services:
  bert:
    build:
      context: ./bert
    environment:
      - BERT_MODEL_DIR=/app/model
      - BERT_MAX_SEQ_LEN=256
    volumes:
      - ./bert:/app
      - model:/app/model
    ports:
      - "8000:8000"
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    runtime: nvidia

Dockerfile

FROM nvidia/cuda:12.1.0-devel-ubuntu22.04

# Verify installation

RUN nvcc --version

ENV PATH=/usr/local/cuda/bin:$PATH
ENV LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

# Update and install system dependencies

RUN apt-get update && apt-get install -y --no-install-recommends \
python3 \
python3-pip \
python3-dev \
python3-venv \
wget \
curl \
libc-dev \
git \
build-essential \
ldconfig \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Ensure `python3` points to `python`

RUN ln -s /usr/bin/python3 /usr/bin/python \
&& python --version \
&& pip --version

# Set the working directory

WORKDIR /app

COPY ./requirements.txt /app/requirements.txt

# Install Python libraries

RUN python3 -m venv /opt/venv \
&& . /opt/venv/bin/activate \
&& pip install --no-cache-dir -r /app/requirements.txt

# Activate virtual environment by default

ENV PATH="/opt/venv/bin:$PATH"

COPY ./ /app

# Expose port for the API

EXPOSE 8000

# Run FastAPI app

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

requirements.txt

--extra-index-url https://download.pytorch.org/whl/cu121

# List of dependencies
fastapi>=0.103.1
loguru>=0.7.2
numpy<2
pydantic>=2.4.0
scikit-learn
sentencepiece>=0.1.99
torch==2.2.2+cu121
transformers>=4.35.0
uvicorn>=0.23.2

已尝试的解决方法

  • 确认宿主机和容器通过nvidia-smi均显示相同GPU
  • 确认容器外相同版本的PyTorch运行torch.cuda.is_available()返回True
  • 为Docker添加NVIDIA容器工具包
  • 在docker-compose.yml中使用runtime: nvidia
  • 设置正确的CUDA环境变量

上述方法均未解决问题,求可行建议。

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 06:51:16