Sub topic 13
Running GPU workloads in containers
This chapter assumes you can build and run a Docker image from the earlier chapters. You should also know that a lock file pins Python packages but not the operating system layer. It covers how a container reaches the host GPU, which parts of the CUDA stack live in the image and which live on the host, and which errors you are most likely to see.
The working model: two halves of the CUDA stack
A GPU program needs two pieces of software that come from different places.
- The driver is the NVIDIA kernel module plus the user-space library
libcuda.so. It is installed on the host. A container shares the host kernel, so it cannot bring its own driver. - The CUDA userland is the CUDA runtime, cuBLAS, cuDNN and similar libraries. These belong in the image, where your lock file or base image pins them.
The NVIDIA Container Toolkit connects the two. When you start a container with GPU access, the toolkit mounts the device files and the host's driver libraries into the container. The image never contains the driver, and it should not.
This split explains the main compatibility rule. The driver must be at least as new as the CUDA version your libraries need. A newer driver runs older CUDA userland, so you can upgrade the host without rebuilding images. The reverse fails.
Where the CUDA libraries come from
You have two common ways to put CUDA into an image.
| Approach | CUDA libraries come from | Trade-off |
|---|---|---|
| Python wheels (such as the PyTorch wheels) | Packages in the lock file, which carry hashes | One pinning mechanism, and a small base image works |
| `nvidia/cuda` base image | The base image, pinned by digest | You need it for compiling custom CUDA code with `nvcc`, but the version is tracked in two places |
The wheel approach fits the workflow in this tutorial. The CUDA libraries are resolved, hashed and locked like any other package, so the Dockerfile from the previous chapter works unchanged on a GPU host. The install may need an extra package index for a specific CUDA build of PyTorch. Record that index in the file you give the lock tool, so the lock file always resolves to the same build.
If you need a compiler, use a -devel tagged nvidia/cuda image for the build stage and pin it by digest like any other base image.
Preparing the host
The host needs two things, and neither is part of your project.
- A working NVIDIA driver. Check it with
nvidia-smion the host. - The NVIDIA Container Toolkit, registered with the container runtime.
For Docker, registration looks like this:
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart dockerThen check the whole chain with a container that runs only nvidia-smi. The toolkit injects that binary, so the image does not need it.
docker run --rm --gpus all ml-train:dev nvidia-smiIf this prints the GPU table, the driver, toolkit and runtime are connected. Do this check before debugging any application code.
Building and running
The build is the same as for a CPU image. The GPU is requested only at run time.
docker build -t ml-train:dev .
docker run --rm --gpus all ml-train:dev--gpus all exposes every host GPU. To expose specific devices, name them. The nested quotes are required by Docker's parsing.
docker run --rm --gpus '"device=0,1"' ml-train:devRestricting devices matters on shared machines, because two jobs that both see GPU 0 will compete for its memory.
A check script that reports the stack
Add a small script to the repository and run it first in every new environment. It reports what the process sees, not what you hoped to install.
import torch
def main() -> None:
print("torch version:", torch.__version__)
print("CUDA build of torch:", torch.version.cuda)
print("cuDNN version:", torch.backends.cudnn.version())
available = torch.cuda.is_available()
print("CUDA available:", available)
if available:
for index in range(torch.cuda.device_count()):
print(f"device {index}:", torch.cuda.get_device_name(index))
else:
raise SystemExit("No usable GPU visible to this process")
if __name__ == "__main__":
main()Run it with docker run --rm --gpus all ml-train:dev python -m src.check_gpu after saving it as src/check_gpu.py. Because the Dockerfile copies src/, the script ships in the image. Keep its output in your CI logs or run records, so you can compare the CUDA build and cuDNN version across runs.
Common errors and what they mean
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `could not select device driver "" with capabilities: [[gpu]]` | The toolkit is not installed or not registered with Docker | Install the toolkit, run `nvidia-ctk runtime configure`, restart Docker |
| `nvidia-smi` works but `torch.cuda.is_available()` is `False` | A CPU-only build of PyTorch was locked | Check `torch.version.cuda`. If it is `None`, re-lock with the CUDA package index |
| `CUDA driver version is insufficient for CUDA runtime version` | The host driver is older than the image's CUDA userland | Update the host driver, or lock a package set built for an older CUDA |
| `Found no NVIDIA driver on your system` | The container started without `--gpus` | Add `--gpus all` to `docker run` |
| Works on one machine, runs slower on another | Different GPU model or different driver, so different kernels are chosen | Record `nvidia-smi` output with each run |
What this does and does not make reproducible
The container makes the CUDA userland identical on every machine. It does not make the driver identical, and it does not make the hardware identical. Two hosts with different driver versions or GPU architectures can run the same image and still choose different algorithms for some operations. The cause is the host, not the image.
This is why the next chapters pair the container with seeds and deterministic-mode settings, and with CI checks that rebuild the image from the lock file.
Checklist
- Lock a CUDA build of your framework, with hashes, and confirm
torch.version.cudais notNone. - Keep the driver out of the image.
- Verify the host with
nvidia-smiin a container before running your own code. - Select devices explicitly on shared hosts.
- Log the driver version and GPU model with each run.
Exercise
A container starts with --gpus all and nvidia-smi shows the GPU, but torch.cuda.is_available() returns False. The script prints torch.version.cuda as None. What is wrong, and where do you fix it?
Show answer
The locked PyTorch is a CPU-only build, so the driver and toolkit are working fine. Fix it where the dependencies are resolved. Point the lock tool at the package index that serves the CUDA build of PyTorch, regenerate the hash-pinned lock file, and rebuild the image. Editing the Dockerfile or the host would not help.
Exercise
Your image was built with CUDA 12 libraries from the lock file. A colleague runs it on a host whose driver supports only CUDA 11. What happens, and what are your two options?
Show answer
The program fails with a message such as "CUDA driver version is insufficient for CUDA runtime version", because the host driver is older than the CUDA userland in the image. Either update the driver on the host, or re-lock the project with a package set built for CUDA 11 and rebuild the image. You cannot fix it by installing a driver inside the image.