docker run
docker-compose.yml
As we discussed, containerization offers a powerful way to package and isolate Machine Learning applications, solving common problems like dependency conflicts and ensuring consistency across different environments. The core component that makes this possible is the Docker image. Think of a Docker image as the blueprint or template for creating these standardized environments.
A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software: the code, a runtime (like Python), system tools, system libraries, and settings. It's a read-only template. When you want to run the software defined by the image, you create a container, which is essentially a running instance of that image.
If you're familiar with object-oriented programming, the image is like a class, and the container is like an object (an instance of the class). Or, drawing a parallel to virtualization, an image is somewhat like a virtual machine template or snapshot, but significantly more lightweight because it shares the host machine's OS kernel rather than bundling a full OS.
For ML projects, an image typically bundles:
tensorflow
, pytorch
, scikit-learn
, pandas
, numpy
) pinned to specific versions.Docker images are not monolithic blobs. They are constructed from a series of read-only layers, stacked on top of each other. Each layer represents an instruction in the image's Dockerfile, which is the script used to build the image. For example, installing a system package, copying source code, or setting an environment variable typically creates a new layer.
A simplified view of image layers. Each instruction in a Dockerfile adds a new layer. When a container is run, a thin writable layer is added on top.
This layered architecture, often implemented using technologies like OverlayFS, has several advantages, particularly relevant for ML:
Every Docker image starts from a base image. This is specified by the FROM
instruction in the Dockerfile. The base image provides the foundational layer(s) for your environment. Common choices for ML projects include:
python:3.10-slim-bullseye
provides a minimal Debian-based environment with a specific Python version pre-installed. Good for general Python applications.ubuntu:22.04
or debian:bullseye
provide a more complete Linux environment if you need specific system tools or libraries not included in the slim Python images.nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
) which include the necessary CUDA drivers and libraries. Framework-specific images (tensorflow/tensorflow:latest-gpu
or pytorch/pytorch:latest
) build upon these, adding the ML framework itself.Choosing the right base image involves balancing size, included components, and compatibility with your needs. We'll explore this selection process in more detail in the next chapter.
It's important to distinguish clearly between an image and a container:
docker run <image_name>
, Docker uses the image to create a container.The key difference lies in the addition of a thin writable layer on top of the read-only image layers when a container is created. Any changes made while the container is running, such as writing log files, creating temporary data, or modifying configuration within the container's filesystem, happen in this writable layer. If the container is deleted, this writable layer and any changes within it are lost, unless you explicitly use mechanisms like Docker volumes or bind mounts to persist data outside the container lifecycle, which we will discuss in Chapter 3.
Docker images are immutable. Once an image is built, it cannot be altered. If you need to update the code, change a dependency, or modify the configuration, you must:
This immutability is a cornerstone of reproducibility in ML. An image captures the exact state of the environment, dependencies, and code at the time it was built. If you share this image (e.g., via a registry like Docker Hub), anyone else can pull it and run a container, guaranteeing they have the identical environment you used. This eliminates the "it works on my machine" problem often encountered when collaborating or deploying ML models.
Understanding these fundamentals of Docker images, layers, base images, and the image-container relationship provides the foundation for effectively building, managing, and sharing the consistent environments needed for robust Machine Learning workflows.
© 2025 ApX Machine Learning