How do you containerize a Python training script for GPU cloud VMs?
This tests reproducible GPU containerization. A strong answer uses an NVIDIA CUDA base image, installs Python dependencies at build time, copies the training script, and runs with --gpus.
What's really being asked
This question checks if you understand the difference between a generic Docker container and a GPU-enabled reproducible ML environment. The interviewer wants to see that you know Docker images encapsulate the application, libraries, and environment variables, but that GPU access requires specific host driver compatibility and runtime flags. It also reveals whether you treat containers as immutable artifacts versus mutable servers.
The full answer
A good answer hits four things in order. First, choose an NVIDIA base image such as an NGC PyTorch or TensorFlow container, or an NVIDIA CUDA runtime image, because these already contain the correct CUDA, cuDNN, and NCCL libraries aligned to host driver versions. Second, install Python dependencies inside the Dockerfile using a requirements file and pin versions so the environment is reproducible. Third, copy the training script and any local data or configuration into the image during the build stage rather than mounting them at runtime, which ensures the image is self-contained. Fourth, run the container with the Docker --gpus all flag so the NVIDIA Container Toolkit exposes the host GPU devices into the container without needing the legacy nvidia-docker2 runtime.
The mistakes people make
Red flags include suggesting you SSH into the VM and install CUDA manually inside the container, because containers share the host kernel and should not bundle kernel drivers. Another red flag is using a generic python base image and trying to compile CUDA from source, which creates massive images and version skew. Candidates also err by omitting the --gpus flag and then wondering why torch.cuda.is_available returns False, or by building the image on a laptop with an ARM chip and expecting it to run on a cloud x86 VM without multi-platform builds.
What usually comes next
The interviewer may ask how you handle large datasets that do not fit in the image, and the correct pattern is to mount a volume at runtime for data while keeping the code in the image. They may also ask about multi-stage builds to reduce image size, or how you verify that the host NVIDIA driver is compatible with the container CUDA version. Another common follow-up is how you would orchestrate this on Kubernetes, which requires requesting NVIDIA GPUs via resource limits and ensuring the device plugin is installed.
A concrete example
Suppose you have train.py and requirements.txt. Your Dockerfile starts with FROM nvcr.io/nvidia/pytorch:23.10-py3, then runs pip install --no-cache-dir -r requirements.txt, copies the script with COPY train.py /workspace/train.py, and sets CMD python /workspace/train.py. On the cloud VM, which already has the NVIDIA driver and Docker 19.03 or later, you build with docker build -t trainer . and run with docker run --rm --gpus all trainer. This works because Docker natively supports NVIDIA GPUs as devices since Docker 19.03, so the special nvidia-docker2 runtime is no longer necessary.
Interview question
What is the correct approach to ensure a Docker container can run a Python training script on a GPU cloud VM?
- a.Build the image on a local ARM laptop, push to an x86 cloud VM, and run with the nvidia-docker2 runtime.
- b.Use an NVIDIA CUDA base image, mount the script at runtime, and omit the --gpus flag since the base image includes GPU support.
- c.Use an NVIDIA CUDA base image, install dependencies at build time, copy the script into the image, and run with --gpus all.Correct
- d.Use a generic python image, install CUDA drivers inside the container, and run with --gpus all.
Why? this is the answer
The correct workflow uses an NVIDIA base image with pre-installed CUDA libraries, build-time dependency pinning, a self-contained copied script, and the --gpus flag to expose host GPUs. Option D is wrong because containers share the host kernel and should never bundle NVIDIA drivers, while option B fails because omitting --gpus prevents the NVIDIA Container Toolkit from exposing GPU devices.
Just read this? Test yourself on what you have been reading.
Read the original → docs.nvidia.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.
See open roles