Sub topic 9
Containers and images in brief
Assume you know basic command-line use, Git, and Python environments. This chapter introduces the container concepts needed before building an ML image. It does not yet cover a complete Dockerfile or GPU configuration.
Why containers help reproducibility
A container packages an application with its user-space environment. That environment can include the Python interpreter, installed packages, system libraries, and configuration files. Running the same image gives different machines the same starting filesystem.
A container is a running process created from an image. An image is an immutable package of filesystem layers and metadata. The image is a template. The container is the temporary or persistent runtime created from that template.
Containers do not provide a complete second kernel in the way a virtual machine does. They normally share the host kernel while isolating processes, filesystems, networking, and users through operating-system features. This makes containers start quickly and use fewer resources than full virtual machines, but host compatibility still matters.
Images are built from layers
An image usually consists of ordered, read-only layers. A command such as installing packages adds a layer. Later layers can reuse earlier layers that have not changed. A container adds a writable layer above the image while it runs.
This layering affects both reproducibility and build speed:
- An image records the result of its build, but a floating input can still change that result.
- A base-image tag such as
python:3.11-slimidentifies a moving name, not one permanent filesystem. - A registry stores and distributes images. A digest identifies one exact image manifest.
- Data and model outputs should usually be mounted or stored separately rather than baked into every image.
| Item | Meaning | Reproducibility concern |
|---|---|---|
| Image tag | Human-readable name such as `ml-train:1.0` | The name can be reused |
| Image digest | Content-derived identifier | Identifies one exact image |
| Container | Running instance of an image | Runtime settings can differ |
| Volume | Storage mounted into a container | Its contents need separate versioning |
Inspecting the boundary
A simple container can print its Python version and then exit:
docker run --rm python:3.11-slim python --versiondocker run creates and starts a container. The --rm option removes that container after the command finishes. The image remains available locally. In a training workflow, you might mount a dataset directory and an output directory, while keeping the software environment inside the image.
A container does not automatically make an experiment reproducible. The host can provide different GPUs, drivers, environment variables, mounted files, or network responses. The image handles one important part of the problem: the user-space software environment. Lock files can make the Python packages inside that image more exact.
Exercise
What is the difference between an image and a container, and why does a tag alone not fully identify an image?
Show answer
An image is the packaged, read-only template; a container is a running instance of it. A tag is a reusable name, while a digest identifies the exact image content.