好吧,我终于设法在不使用 --privileged 模式的情况下做到了。
我在 ubuntu 服务器 14.04 上运行,我使用的是最新的 cuda(6.0.37 用于 linux 13.04 64 位)。
准备
在您的主机上安装 nvidia 驱动程序和 cuda。 (这可能有点棘手,所以我建议您遵循本指南https://askubuntu.com/questions/451672/installing-and-testing-cuda-in-ubuntu-14-04)
注意:保留用于主机 cuda 安装的文件非常重要
使用 lxc 让 Docker 守护进程运行
我们需要使用 lxc 驱动程序运行 docker daemon 才能修改配置并让容器访问设备。
一次性使用:
sudo service docker stop
sudo docker -d -e lxc
永久配置
修改位于 /etc/default/docker 中的 docker 配置文件
通过添加 '-e lxc' 更改 DOCKER_OPTS 行
这是我修改后的线路
DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 -e lxc"
然后使用重新启动守护进程
sudo service docker restart
如何检查守护进程是否有效使用lxc驱动?
docker info
执行驱动程序行应如下所示:
Execution Driver: lxc-1.0.5
使用 NVIDIA 和 CUDA 驱动程序构建您的映像。
这是一个基本的 Dockerfile 来构建一个 CUDA 兼容的镜像。
FROM ubuntu:14.04
MAINTAINER Regan <http://stackoverflow.com/questions/25185405/using-gpu-from-a-docker-container>
RUN apt-get update && apt-get install -y build-essential
RUN apt-get --purge remove -y nvidia*
ADD ./Downloads/nvidia_installers /tmp/nvidia > Get the install files you used to install CUDA and the NVIDIA drivers on your host
RUN /tmp/nvidia/NVIDIA-Linux-x86_64-331.62.run -s -N --no-kernel-module > Install the driver.
RUN rm -rf /tmp/selfgz7 > For some reason the driver installer left temp files when used during a docker build (i don't have any explanation why) and the CUDA installer will fail if there still there so we delete them.
RUN /tmp/nvidia/cuda-linux64-rel-6.0.37-18176142.run -noprompt > CUDA driver installer.
RUN /tmp/nvidia/cuda-samples-linux-6.0.37-18176142.run -noprompt -cudaprefix=/usr/local/cuda-6.0 > CUDA samples comment if you don't want them.
RUN export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64 > Add CUDA library into your PATH
RUN touch /etc/ld.so.conf.d/cuda.conf > Update the ld.so.conf.d directory
RUN rm -rf /temp/* > Delete installer files.
运行您的图像。
首先,您需要确定与您的设备关联的主要号码。
最简单的方法是执行以下命令:
ls -la /dev | grep nvidia
如果结果为空白,则使用在主机上启动其中一个示例应该可以解决问题。
结果应该是这样的
如您所见,组和日期之间有一组 2 个数字。
这两个数字被称为主要和次要数字(按此顺序编写)并设计一个设备。
为方便起见,我们将仅使用主要数字。
为什么要激活 lxc 驱动程序?
使用允许我们的容器访问这些设备的 lxc conf 选项。
选项是:(我建议使用 * 作为次要编号,因为它会减少运行命令的长度)
--lxc-conf='lxc.cgroup.devices.allow = c [major number]:[minor number or *] rwm'
所以如果我想启动一个容器(假设你的图像名称是 cuda)。
docker run -ti --lxc-conf='lxc.cgroup.devices.allow = c 195:* rwm' --lxc-conf='lxc.cgroup.devices.allow = c 243:* rwm' cuda