马春杰杰 Exit Reader Mode

非特权容器如何安装nvidia-docker?

由于MIG功能需要设置nvidia.runtimetrue,但是它跟security.privileged是冲突的,所以为了使用MIG,只能关闭security.privileged。下面是步骤:

容器名为t3为例:

# 停止容器 t3,确保可以修改配置
lxc stop t3

# 给容器添加一个 GPU 设备,类型是 MIG
# 指定 MIG 的 UUID 和所在物理 GPU 的 PCI 地址
lxc config device add t3 mig0 gpu \
  gputype=mig \
  mig.uuid=MIG-0a8dad45-3186-5221-ae18-52e54ffee0ac \
  pci=0000:3b:00.0

# 设置容器为非特权模式(MIG + nvidia.runtime 需要非特权容器)
lxc config set t3 security.privileged false

# 打开 NVIDIA runtime,这样 LXD 才会自动挂载 NVIDIA 相关设备和库
lxc config set t3 nvidia.runtime true

# 允许嵌套容器(使容器内能运行 Docker、K8s 等)
lxc config set t3 security.nesting true

# 允许挂载并写 cgroup(对 systemd/docker 必须)
lxc config set t3 raw.lxc "lxc.mount.auto=cgroup:rw"

# 用 heredoc 方式正确写入 raw.lxc 配置(两行)
# 1. 放宽 AppArmor profile(unconfined,解决 overlayfs、mount 限制)
# 2. 自动挂载 cgroup 并允许写
cat <<'EOF' | lxc config set t3 raw.lxc -
lxc.apparmor.profile=unconfined
lxc.mount.auto=cgroup:rw
EOF

# 指定容器内的 GPU 能力范围(只暴露计算和 utility 功能,避免显卡输出相关)
lxc config set t3 nvidia.driver.capabilities compute,utility

# 启动容器 t3,配置生效
lxc start t3