Okay, let's build a Docker environment that includes PyTorch with GPU (CUDA) support and uses uv for managing Python packages.
-
Choose a Base Image: The easiest and recommended way is to start with an official PyTorch image that already includes the necessary CUDA and cuDNN libraries. These images are built on top of NVIDIA's CUDA images. You can find available tags on Docker Hub under pytorch/pytorch. Choose a tag that matches your desired PyTorch version and the CUDA version supported by your GPU driver (e.g., pytorch/pytorch:2.3.0-cuda12.1-cudnn8-runtime). The runtime images are generally smaller than devel images and sufficient for running pre-built PyTorch.
-
Create a Dockerfile: Create a file named Dockerfile in your project directory.
-
Write the Dockerfile Content:
# Choose an official PyTorch base image with CUDA support
# Find tags here: https://hub.docker.com/r/pytorch/pytorch/tags
# Example: PyTorch 2.3.0, CUDA 12.1, CuDNN 8, Runtime environment
FROM pytorch/pytorch:2.3.0-cuda12.1-cudnn8-runtime
# Set the working directory inside the container
WORKDIR /app
# (Optional) Install system dependencies if needed
# RUN apt-get update && apt-get install -y --no-install-recommends \
# package1 \
# package2 \
# && rm -rf /var/lib/apt/lists/*
# Install uv using pip (which is available in the base image)
# Using --no-cache-dir can slightly reduce layer size
RUN pip install --no-cache-dir uv
# Copy your Python requirements file (or pyproject.toml)
# Copy requirements first to leverage Docker layer caching
COPY requirements.txt .
# Or if using pyproject.toml:
# COPY pyproject.toml .
# COPY poetry.lock . # If using poetry/pdm managed pyproject.toml
# Install Python dependencies using uv
# This assumes you have a requirements.txt file
# uv is generally much faster than pip
RUN uv pip install --no-cache -r requirements.txt
# If using pyproject.toml (e.g., with PDM/Poetry sections or just dependencies)
# you might use:
# RUN uv pip install --no-cache .
# Copy the rest of your application code into the container
COPY . .
# (Optional) Specify the default command to run when the container starts
# Example: running a Python script
# CMD ["python", "your_script.py"]
# Or keep it open to run commands interactively
CMD ["bash"]
-
Create a requirements.txt file: In the same directory as your Dockerfile, create a requirements.txt file listing your Python dependencies (excluding PyTorch itself, as it's already in the base image, unless you need a very specific version different from the base).
Example requirements.txt:
numpy
pandas
scikit-learn
matplotlib
# Add other dependencies here
- Note: If the PyTorch version in the base image (
2.3.0 in the example) is sufficient, you don't need to list torch, torchvision, or torchaudio here. If you do list them, uv pip install will attempt to install them, potentially overwriting or conflicting with the versions included in the base image. It's usually best to rely on the base image's PyTorch installation.
-
Build the Docker Image: Open your terminal in the directory containing the Dockerfile and requirements.txt, and run:
docker build -t my-pytorch-gpu-uv-app .
Replace my-pytorch-gpu-uv-app with your desired image name.
-
Run the Docker Container: To run the container and give it access to your GPU(s), use the --gpus all flag (requires the NVIDIA Container Toolkit):
docker run --gpus all -it --rm my-pytorch-gpu-uv-app bash
--gpus all: Exposes all available host GPUs to the container.
-it: Runs the container in interactive mode with a pseudo-TTY.
--rm: Automatically removes the container when you exit.
my-pytorch-gpu-uv-app: The name of the image you built.
bash: The command to run inside the container (overrides the CMD in the Dockerfile for this run).
-
Verify Installation inside the Container: Once inside the container's bash prompt:
This setup provides a reproducible environment with PyTorch, GPU acceleration, and the speed of uv for package installation, suitable for development and deployment.