问题
I have a GPU application that does unit-testing during the image building stage.
With Docker 19.03, one can specify nvidia runtime with docker run --gpus all
but I also need access to the gpus for docker build
because I do unit-testing. How can I achieve this goal?
For older version of docker that use nvidia-docker2 it was not possible to specifiy runtime during build stage, BUT you can set the default runtime to be nvidia, and docker build works fine that way. Can I do that in Docker 19.03 that doesn't need nvidia-docker anymore? If so, how?
回答1:
You need use nvidia-container-runtime as explained in docs: "It is also the only way to have GPU access during docker build".
Steps for Ubuntu:
Install nvidia-container-runtime:
sudo apt-get install nvidia-container-runtime
Edit/create the /etc/docker/daemon.json with content:
{
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
},
"default-runtime": "nvidia"
}
Restart docker daemon:
sudo systemctl restart docker
Build your image (now GPU available during build):
docker build -t my_image_name:latest .
回答2:
A "solution" I found is to first run a base image with the host nvidia drivers mounted on it
docker run -it --rm --gpus ubuntu
And then build my app within the container manually and commit the resulting image. This is not ideal and it would be best to have access to nvidia-smi during the build phase.
回答3:
You have to install the NVIDIA driver and Docker 19.03 for your Linux distribution Note that you do not need to install the CUDA toolkit on the host, but the driver needs to be installed
With the release of Docker 19.03, usage of nvidia-docker2 packages are deprecated since NVIDIA GPUs are now natively supported as devices in the Docker runtime.
If you are using the nvidia-docker2 packages, review the instructions in the “Upgrading with nvidia-docker2”.
For first-time users of Docker 19.03 and GPUs, continue with the instructions for getting started below. Usage
# Add the package repositories
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
for more information please visit : https://github.com/NVIDIA/nvidia-docker
来源:https://stackoverflow.com/questions/59691207/docker-build-with-nvidia-runtime