The Ultimate Docker Cheat Sheet for AI & ML (2026 Edition)

Introduction

Docker has become an indispensable tool in the world of artificial intelligence (AI) and machine learning (ML). Its ability to package applications and dependencies into lightweight, portable containers has revolutionized how AI/ML models are developed, trained, and deployed.

Consider these statistics:

  • 83% of AI/ML developers use Docker in their workflow (source)
  • Docker adoption has grown 50% annually within the AI/ML community (source)
  • Over 10M Docker images related to AI/ML have been pulled from Docker Hub (source)

By enabling reproducible, isolated environments, Docker solves many challenges in AI/ML development, such as conflicting dependencies, platform inconsistencies, and difficulties in collaboration. This cheat sheet will guide you through the essentials of using Docker for AI/ML, from installation to advanced usage.

Installing Docker

Docker provides installers for all major operating systems. For AI/ML workloads, it‘s recommended to use Docker Engine 20.10 or later, which includes the new NVIDIA Container Toolkit for GPU support (source).

Linux

Docker can be installed from official repositories on most Linux distributions. See the Docker documentation for specific instructions.

macOS

Docker Desktop for Mac requires macOS Sierra 10.12 or later. It can be installed via Homebrew or the Docker website.

Windows

Docker Desktop for Windows requires Windows 10 or later and the Hyper-V feature enabled. Download the installer from the Docker website.

To verify your installation, run:

docker version

Docker Fundamentals

Images and Containers

Docker images are the blueprints for containers. They include the application code, runtime, libraries, and environment variables. Containers are running instances of images that are isolated from the host and each other.

Some popular AI/ML Docker images include:

To run a container from an image:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

For example, to start a Jupyter notebook server:

docker run -p 8888:8888 jupyter/datascience-notebook

Managing Containers

List running containers:

docker ps

Stop a running container:

docker stop CONTAINER

Remove a container:

docker rm CONTAINER

Managing Images

List available images:

docker images

Pull an image from a registry:

docker pull IMAGE

Remove an image:

docker rmi IMAGE

Building Images

To build a custom image, create a Dockerfile with the necessary instructions. For example:

FROM tensorflow/tensorflow:latest-gpu
RUN pip install scikit-learn pandas matplotlib
WORKDIR /app
COPY train.py .
ENTRYPOINT ["python", "train.py"]

Build the image with:

docker build -t myimage .

GPU Enablement

Docker supports GPU access within containers, which is crucial for accelerating AI/ML workloads. To use NVIDIA GPUs, you need the NVIDIA Container Toolkit (source).

Verify GPU access with:

docker run --gpus all nvidia/cuda nvidia-smi

To run a container with GPU access:

docker run --gpus all IMAGE

The --gpus flag controls which GPUs are accessible. Use --gpus all to expose all GPUs.

Storage and Networking

Docker provides options for persisting data and enabling communication between containers.

Volumes

Volumes allow data to persist beyond the lifecycle of a container. To mount a volume:

docker run -v /host/path:/container/path IMAGE

Networks

Docker creates a default bridge network for containers. To expose ports:

docker run -p HOST_PORT:CONTAINER_PORT IMAGE

For custom networking, create a user-defined network:

docker network create NETWORK

Connect containers to the network:

docker run --network NETWORK IMAGE

Docker Compose

Docker Compose is a tool for defining and running multi-container applications. It‘s particularly useful for AI/ML workflows involving multiple services, like data preprocessing, model training, and serving.

Compose File

The docker-compose.yml file describes the services, networks, and volumes:

version: ‘3‘
services:
  notebook:
    image: jupyter/datascience-notebook
    volumes:
      - ./data:/home/jovyan/data
    ports:
      - 8888:8888
  training:
    build: ./training
    volumes: 
      - ./data:/data
    command: python train.py
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [gpu]

This example defines a Jupyter notebook service and a training service with GPU access.

Compose Commands

Start the application:

docker-compose up

Stop and remove containers:

docker-compose down

Other useful commands:

  • docker-compose ps – List running containers
  • docker-compose logs – View output from containers
  • docker-compose build – Build or rebuild services
  • docker-compose scale – Scale services to a specified number of replicas

AI/ML Frameworks

Several popular AI/ML frameworks provide official Docker images for easy deployment:

Using these official images ensures compatibility and reduces environment setup time.

Performance Considerations

To optimize Docker performance for AI/ML workloads:

  • Use NVIDIA Docker for GPU acceleration. Significant speedups vs. CPU (50-500x) for deep learning (source).
  • Minimize image size by using a lightweight base image and only installing necessary dependencies. Smaller images deploy faster and use less storage (source).
  • Leverage caching when building images. Stages that haven‘t changed can be reused, speeding up builds (source).
  • Use volumes for data sets and checkpoints to avoid costly copies. Bind mounts perform better than named volumes (source)
  • Enable live-restore to keep containers running during daemon downtime. Prevents interrupting long-running training jobs.

Case Studies

Many companies have adopted Docker for their AI/ML pipelines:

  • Uber – Uses Docker to package ML models for deployment across different regions and cloud providers (source).
  • Airbnb – Runs ML models in Docker containers for real-time prediction services (source).
  • Netflix – Deploys ML models in Docker containers on AWS for scalable serving (source).
  • Spotify – Uses Docker to streamline ML model deployment and experimentation (source).

These case studies demonstrate the scalability, portability, and reproducibility benefits of Docker for AI/ML workflows.

Conclusion

Docker has become a critical tool for AI/ML developers, enabling efficient development, training, and deployment of models. By providing reproducible environments, GPU acceleration, and easy scalability, Docker simplifies the entire AI/ML lifecycle.

As the AI/ML field continues to evolve, Docker adoption is expected to grow even further. Emerging technologies like Kubeflow and MLOps platforms are built on Docker and Kubernetes, cementing Docker‘s position as a foundational technology for AI/ML.

To learn more, check out these resources:

Happy containerizing your AI/ML workloads!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts