Build Docker images with NVIDIA runtime

I need to run unit-tests on my GPU application during the image building stage. Is there a way to do this with Docker 19.03?

Solution:

I need to run unit-tests on my GPU application during the image building stage using Docker 19.03. Is this possible?

docker run --gpus all

allows specifying nvidia runtime, but this doesn’t work for docker build. With nvidia-docker2, it was not possible to specify a runtime during the build stage, but is there a way to set the default runtime to be nvidia with Docker 19.03, so that docker build works?

Yes, it is possible to set the default runtime to be NVIDIA with Docker 19.03 so that docker build works. You can set the default runtime by creating a file /etc/docker/daemon.json with the following contents:

{
    "default-runtime": "nvidia"
}

Then restart the Docker daemon using the command:

sudo systemctl restart docker

After this, docker build will use the NVIDIA runtime by default, and you can run unit-tests on your GPU application during the image building stage.