How do I specify nvidia runtime from docker-compose.yml?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Running a container with GPU access is a Docker host configuration problem first and a Compose syntax problem second. If the NVIDIA container toolkit is installed correctly, Docker Compose can pass GPU devices into the service; if the host is not ready, no Compose file will fix it.
Verify the Host Before Touching Compose
Before editing docker-compose.yml, confirm that plain Docker can see the GPU. This isolates driver and toolkit problems from Compose problems.
If that command fails, fix the host first. Common prerequisites are:
- a working NVIDIA driver on the machine
- the NVIDIA Container Toolkit installed
- a recent Docker Engine that supports
--gpus
Only move to Compose after nvidia-smi works in a normal docker run.
Modern Compose Configuration
On current Docker setups, the preferred approach is to request GPU devices explicitly. A Compose service can declare a device reservation so the container gets access to the NVIDIA driver stack.
Then start the service:
If your environment supports GPU reservations in Compose, the container should print the GPU details and exit successfully.
When you need every available GPU instead of a single device, replace count: 1 with count: all.
Legacy runtime: nvidia
Older Compose examples often show a service-level runtime setting:
This format existed before Docker standardized the --gpus workflow. It can still appear in older projects, but it depends heavily on the Docker and Compose versions installed on the host. For new setups, treat it as a compatibility path rather than the default recommendation.
That distinction matters because many failures come from copying an old blog post into a newer Docker environment and assuming the syntax is universal.
Check the Container After Startup
Once the service is up, verify that the container can really use the GPU rather than assuming the Compose file worked.
For machine learning workloads, you can also check from inside the application runtime. For example, with Python and PyTorch:
If torch.cuda.is_available() returns False, the issue is still in the container runtime path, not in your training code.
Keep the Compose File Focused
It is tempting to change several things at once: base image, runtime syntax, environment variables, and application command. Resist that. Start with a minimal service that only runs nvidia-smi. Once that works, add your actual image and command. This staged approach gives you a clean checkpoint and prevents application bugs from being confused with GPU runtime bugs.
For example, this is a good progression:
- Run
docker run --gpus all ... nvidia-smi - Run minimal Compose service with
nvidia-smi - Add your real image
- Add your application command
Each step narrows the problem space.
Common Pitfalls
- Editing Compose before verifying
docker run --gpus allworks wastes time. Fix host drivers and toolkit issues first. - Using
runtime: nvidiaon a newer setup can fail because many current environments expect device requests instead of legacy runtime configuration. - Assuming the container has GPU access because it started successfully is misleading. Always run
nvidia-smior an application-level CUDA check. - Mixing application debugging with runtime debugging makes failures harder to isolate. Start with a minimal CUDA image and a trivial command.
- Forgetting that Compose behavior depends on Docker and toolkit versions leads to copy-paste errors. Match the syntax to the environment you actually have.
Summary
- Confirm the host GPU setup with
docker run --gpus all ... nvidia-smifirst. - Prefer explicit GPU device reservations in Compose for modern Docker environments.
- Treat
runtime: nvidiaas a legacy compatibility option, not the default approach. - Validate access inside the running container with
nvidia-smior a CUDA-aware library. - Debug in stages so you can separate host, runtime, and application issues.
Related reading
- How do I use Batch Normalization during test time in Keras?
- How do I use distributed DNN training in TensorFlow?
- How do I use TensorFlow GPU?
- How do tf.gradients work?
- How do I specify the model_config_file variable to tensorflow-serving in docker-compose?
- How do I start tensorflow docker jupyter notebook
- How do the loss weights work in Tensorflow?
- How do you add new categories and training to a pretrained Inception v3 model in TensorFlow?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.