Docker containers are the standard way to package and deploy AI workloads. They solve the "works on my machine" problem by bundling your code, CUDA libraries, Python dependencies, and model weights into a single reproducible image. This guide covers installing the NVIDIA Container Toolkit, writing GPU-enabled Dockerfiles, running vLLM and PyTorch workloads in containers, and deploying to GPU cloud providers — from a single RTX 4090 to a multi-node H100 cluster.
Prerequisites: NVIDIA Container Toolkit
Docker alone cannot access the GPU — you need the NVIDIA Container Toolkit (formerly nvidia-docker2) to expose GPU devices inside containers. Install it on Ubuntu: `curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list && sudo apt update && sudo apt install -y nvidia-container-toolkit`.
Configure Docker to use the NVIDIA runtime: `sudo nvidia-ctk runtime configure --runtime=docker && sudo systemctl restart docker`. Verify the setup: `docker run --rm --gpus all nvidia/cuda:12.1.0-base-ubuntu22.04 nvidia-smi`. You should see your GPU listed in the output. If you get a "could not select device driver" error, ensure your NVIDIA driver version is 525+ (`nvidia-smi` on the host to check).
Most GPU cloud providers (Lambda Labs, CoreWeave, RunPod) offer base images with CUDA and the Container Toolkit pre-installed. Check live GPU cloud prices for providers that include Docker + CUDA in their base images — it saves 20–30 minutes of setup time.
Writing a GPU-enabled Dockerfile
Always start from an official NVIDIA CUDA base image rather than a generic Ubuntu image. NVIDIA publishes three image variants: `base` (CUDA runtime only), `runtime` (CUDA runtime + cuDNN), and `devel` (full toolkit including compilers). Use `runtime` for inference workloads and `devel` only if you need to compile CUDA extensions.
A minimal vLLM Dockerfile: start with `FROM vllm/vllm-openai:latest` (vLLM's official image already includes CUDA 12.1 and all dependencies). Add `COPY requirements.txt .` and `RUN pip install -r requirements.txt` for any additional Python packages. Set `ENV HUGGING_FACE_HUB_TOKEN=""` as a placeholder — pass the real token at runtime via `--env`. Expose port 8000 and set the entrypoint to the vLLM server command.
For a PyTorch training image: `FROM pytorch/pytorch:2.3.0-cuda12.1-cudnn8-runtime`. Install your dependencies, copy your training script, and set the entrypoint. Keep the image lean — don't install Jupyter, matplotlib, or other dev tools in production images. Use multi-stage builds to separate build dependencies from the runtime image.
Pin your base image to a specific tag (e.g. `cuda:12.1.0-runtime-ubuntu22.04`) rather than `latest`. CUDA version mismatches between the container and host driver are the most common source of GPU container failures. The container's CUDA version must be ≤ the host driver's supported CUDA version (check with `nvidia-smi`).
Running GPU containers
The key flag is `--gpus`. Use `--gpus all` to expose all GPUs, `--gpus 1` for a single GPU, or `--gpus '"device=0,1"'` to expose specific GPU indices. Example: `docker run --gpus all -p 8000:8000 -e HUGGING_FACE_HUB_TOKEN=your_token vllm/vllm-openai:latest --model meta-llama/Meta-Llama-3.1-8B-Instruct`.
For persistent model storage (avoid re-downloading on every container start), mount a volume: `-v /home/ubuntu/.cache/huggingface:/root/.cache/huggingface`. The first run downloads the model; subsequent runs load from the mounted cache. A Llama 3.1 70B model is ~140 GB — downloading it on every container start wastes 20–30 minutes and egress bandwidth.
Resource limits: use `--shm-size=16g` for PyTorch distributed training (shared memory for inter-process communication). Without this, DataLoader workers crash with "bus error" on large batch sizes. For vLLM, `--shm-size=8g` is sufficient. Set `--ulimit memlock=-1` to allow GPU memory locking, which improves inference latency.
Check GPU utilisation inside a running container: `docker exec -it container_name nvidia-smi`. This runs nvidia-smi inside the container and shows real-time GPU memory and compute utilisation — useful for confirming the model is actually using the GPU and not falling back to CPU.
Docker Compose for multi-service AI stacks
A typical AI app stack has multiple services: a web server, an inference backend, a database, and a cache. Docker Compose manages all of them together. A minimal `docker-compose.yml` for a Next.js + vLLM stack: define a `web` service (your Next.js app, no GPU needed) and an `inference` service (vLLM, with GPU access via `deploy.resources.reservations.devices`).
The Compose GPU syntax: under the `inference` service, add `deploy: resources: reservations: devices: - driver: nvidia count: 1 capabilities: [gpu]`. This is the Compose v3 equivalent of `--gpus 1`. For all GPUs, set `count: all`.
Use a shared network so services communicate by service name: `networks: [app-network]`. Your Next.js app calls the inference service at `http://inference:8000/v1` — no need to expose the inference port publicly. Only expose the web service port (3000 → 80/443 via Nginx).
Start the stack: `docker compose up -d`. View logs: `docker compose logs -f inference`. Restart a single service after a code change: `docker compose up -d --no-deps --build web`. The `--no-deps` flag restarts only the web service without touching the inference service (which takes 2–3 minutes to load the model).
Building and pushing images to a registry
For deployment to GPU cloud instances, build your image locally and push to a container registry. Docker Hub is free for public images. For private images, use GitHub Container Registry (free for public repos, $0/GB storage for private with GitHub Pro) or AWS ECR ($0.10/GB/month).
Build and tag: `docker build -t ghcr.io/yourorg/yourapp:latest .`. Push: `docker push ghcr.io/yourorg/yourapp:latest`. On your GPU cloud instance, pull and run: `docker pull ghcr.io/yourorg/yourapp:latest && docker run --gpus all -p 8000:8000 ghcr.io/yourorg/yourapp:latest`.
For CI/CD, add a GitHub Actions workflow that builds and pushes on every push to main. Use `docker/build-push-action` with layer caching (`cache-from: type=gha` and `cache-to: type=gha,mode=max`) to speed up builds. A well-cached build of a vLLM-based image takes 2–3 minutes vs 15–20 minutes without caching.
Image size matters for deployment speed. A naive vLLM image can exceed 20 GB. Use multi-stage builds to separate build and runtime stages. Remove pip cache (`pip install --no-cache-dir`), clear apt lists (`rm -rf /var/lib/apt/lists/*`), and avoid copying unnecessary files. A well-optimised vLLM inference image should be under 8 GB (excluding model weights, which are mounted as a volume).
Deploying to GPU cloud providers
Most GPU cloud providers support Docker natively. Lambda Labs, CoreWeave, and RunPod all allow you to specify a Docker image at instance launch. CoreWeave's Kubernetes-based platform (CKSF) is the most powerful option for multi-node deployments — define a Pod spec with GPU resource requests and your container image, and CoreWeave handles scheduling across their H100 cluster.
RunPod's template system lets you save a Docker image + environment variables as a reusable template. Launch instances from the template with one click or via API. This is the fastest path from a local Docker image to a running GPU instance — typically under 5 minutes including model download from a pre-warmed cache.
For Kubernetes deployments, the NVIDIA GPU Operator automates installation of the NVIDIA driver, Container Toolkit, and device plugin on every node. Once installed, request GPUs in your Pod spec with `resources: limits: nvidia.com/gpu: "1"`. The device plugin handles GPU assignment and isolation automatically.
Monitor container resource usage in production: `docker stats` shows CPU, memory, and network I/O. For GPU metrics, use DCGM (Data Center GPU Manager) — it exports GPU utilisation, memory usage, temperature, and power draw as Prometheus metrics. Pair with Grafana for dashboards. CoreWeave and Lambda Labs both support DCGM on their managed instances.