您的位置 首页 大模型

在 Ubuntu 24.04 上配置 Docker GPU 支持

在 Ubuntu 24.04 上配置 Docker GPU 支持

前提条件
首先需要确认你的服务器是否有 NVIDIA GPU:

# 检查是否有 NVIDIA GPU
lspci | grep -i nvidia

# 或者
nvidia-smi

 

步骤一:安装 NVIDIA 驱动

# 1. 更新系统
sudo apt update
sudo apt upgrade -y

# 2. 检测推荐的 NVIDIA 驱动
ubuntu-drivers devices

# 3. 自动安装推荐驱动
sudo ubuntu-drivers autoinstall

# 或者手动安装特定版本(例如 nvidia-driver-550)
sudo apt install -y nvidia-driver-550

# 4. 重启系统
sudo reboot

# 5. 重启后验证驱动安装
nvidia-smi

 

步骤二:安装 NVIDIA Container Toolkit

这是让 Docker 能够使用 GPU 的关键组件:

# 1. 配置仓库
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
  sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
  sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

# 2. 更新包索引
sudo apt update

# 3. 安装 NVIDIA Container Toolkit
sudo apt install -y nvidia-container-toolkit

# 4. 配置 Docker 运行时
sudo nvidia-ctk runtime configure --runtime=docker

# 5. 重启 Docker 服务
sudo systemctl restart docker

# 6. 验证 GPU 在 Docker 中可用
docker run --rm --gpus all nvidia/cuda:12.2.0-base-ubuntu22.04 nvidia-smi

步骤三:修改 docker-compose-xinfence.yaml 以使用 GPU

 

version: '3.5'

services:

##################### 使用xinference部署大模型 #####################

# docker 文档
# https://inference.readthedocs.io/zh-cn/latest/getting_started/using_docker_image.html#docker-image
# 使用qwen2大模型
# https://inference.readthedocs.io/zh-cn/latest/models/builtin/llm/qwen2-instruct.html
# 启动 7b模型
# xinference launch --model-engine vllm --model-name qwen2-instruct --size-in-billions 7 --model-format awq --quantization Int4

  xinf:
    restart: always
    container_name: xinf
    #image: crpi-nz64v48xk0nsuqkh.cn-beijing.personal.cr.aliyuncs.com/zzkattgatt/xinference:latest-cpu
    image: registry.cn-hangzhou.aliyuncs.com/baimeidashu/bmds:xprobe-xinference-latest-20260120
    ports:
      - 9997:9997
    environment:
      - XINFERENCE_MODEL_SRC=modelscope
    volumes:
      - ./xinf-cache/:/root/.cache
    # 命令启动 xinference
    entrypoint: xinference-local -H 0.0.0.0 --log-level debug
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]

networks:
  default:
    name: xinf-network

 

主要修改:
添加了 deploy.resources.reservations.devices 配置段
count: all 表示使用所有可用的 GPU
如果只想使用特定数量的 GPU,可以改为 count: 1 或 count: 2

 

步骤四:指定特定 GPU(可选)
如果你有多个 GPU,可以指定使用特定的 GPU:

    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              device_ids: ['0', '1']  # 使用 GPU 0 和 GPU 1
              capabilities: [gpu]

 

实际的docker-compose.yaml:


services:
  xinference:
    container_name: xinference
    # 使用稳定版本而非 latest,避免兼容性问题
    image: xprobe/xinference:v1.17.1
    restart: always
    ports:
      - "9997:9997"
    environment:
      - XINFERENCE_HOME=/data
      - XINFERENCE_MODEL_SRC=modelscope
      - HF_ENDPOINT=https://hf-mirror.com
      # vLLM 相关环境变量优化
      - VLLM_WORKER_MULTIPROC_METHOD=spawn
      - NCCL_DEBUG=WARN
      # 限制 vLLM 使用的 GPU 内存比例,避免OOM
      - VLLM_GPU_MEMORY_UTILIZATION=0.85
      # 禁用某些可能引起问题的功能
      - VLLM_USE_V1=0
      # 设置 CUDA 可见设备(确保使用双卡)
      - CUDA_VISIBLE_DEVICES=0,1
      # Ray 相关配置 - 确保Ray能正确识别GPU
      - RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
      # 添加 PyTorch 内存管理优化
      - PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
     # Ray不使用0.0.0.0,让Ray自动检测
      - RAY_SCHEDULER_EVENTS=0
      # 禁用Ray的metrics导出,避免连接错误
      - RAY_DISABLE_METRICS=1
      # --- 核心修复:强制 Ray 识别 GPU ---
      # 1. 明确告诉 Ray 有 2 张卡,ID 是 0 和 1
      - RAY_visible_GPUs=0,1
      # 2. 防止 Ray 自动探测失败(可选,视版本而定)
      - RAY_EXPERIMENTAL_NOSET_CUDA_VISIBLE_DEVICES=1

    volumes:
      - ./xinference-data:/data
      # 挂载共享内存,解决 vLLM 多进程通信问题
      - /dev/shm:/dev/shm
      # 挂载  自定义启动脚本
      - ./start-xinference.sh:/usr/local/bin/start-xinference.sh
    # GPU 配置 - 使用双 RTX 4090
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 2
              capabilities: [gpu]
    runtime: nvidia
    # 添加 IPC 锁定权限和共享内存大小
    ipc: host
    shm_size: '32gb'
 #   command: /bin/bash /usr/local/bin/start-xinference.sh
    command: >
      /bin/bash -c "
      ray start --head --num-gpus=2 --block &
      sleep 10 &&
      /bin/bash /usr/local/bin/start-xinference.sh
      "
    #command: xinference-local -H 0.0.0.0 --log-level debug
    networks:
      - xinference-network
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "5"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9997/v1/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 120s
    # 添加必要的系统能力
    cap_add:
      - SYS_PTRACE
    ulimits:
      memlock: -1
      stack: 67108864
      nofile:
        soft: 65536
        hard: 65536
    # 增加启动等待时间
    stop_grace_period: 30s

networks:
  xinference-network:
    driver: bridge
    name: xinference-network

start-xinference.sh

#!/bin/bash
# 启动 Xinference 并设置默认的 max_model_len

export VLLM_MAX_MODEL_LEN=32000

# Ray 配置 - 解决GPU识别问题
export RAY_NUM_GPUS=2
export RAY_AVAILABLE_RESOURCES='{"GPU": 2}'

# 启动 xinference-local
exec xinference-local -H 0.0.0.0 --log-level debug

 

 

 

步骤五:启动服务并验证

# 1. 启动服务
docker compose -f docker-compose-xinfence.yaml up -d

# 2. 查看容器状态
docker compose -f docker-compose-xinfence.yaml ps

# 3. 查看日志
docker compose -f docker-compose-xinfence.yaml logs -f xinf

# 4. 进入容器验证 GPU 是否可用
docker exec -it xinf nvidia-smi

# 5. 在容器内启动模型时指定使用 GPU
docker exec -it xinf xinference launch --model-engine vllm --model-name qwen2-instruct --size-in-billions 7 --model-format awq --quantization Int4

 

 

欢迎来撩 : 汇总all

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章