Sub topic 9
Containers and images in brief
This chapter covers the working model of containers and images. It assumes you can use pip and the command line, and that you have trained a model in a framework such as PyTorch or scikit-learn. You do not need prior Docker experience. The next chapter writes a full Dockerfile. This one explains what a container pins, what it leaves out, and the errors that catch most newcomers.
Why containers follow lock files
A lock file pins the Python packages. It cannot pin the Python interpreter, the system libraries those packages load (such as glibc or libgomp), or the CUDA userland libraries. Two machines with the same lock file can still differ in any of those.
A container packages the user-space part of the operating system together with your Python installation and packages. Running the same image on two hosts gives both the same files below the kernel. That removes most operating system differences as a cause of different results.
The working model
Four terms cover most of what you need.
- An image is a read-only, layered snapshot of a filesystem plus metadata. The metadata includes the default command, environment variables and the working directory. An image is built once and does not change.
- A container is a running process started from an image. It gets its own filesystem view, process list and network. It adds a thin writable layer on top of the image.
- A registry is a server that stores images, such as Docker Hub or Amazon ECR. You pull images from a registry and push images to it.
- A Dockerfile is a text recipe that builds an image. Each instruction produces a layer.
An image has a name made of a repository, a tag and optionally a digest, for example python:3.11-slim. A tag is a movable label. A digest is a SHA-256 hash of the image content, written @sha256:<digest>. Digests are covered in a later chapter. For now, remember that a tag does not guarantee the same content tomorrow.
What a container shares with the host
A container is not a virtual machine. It does not run its own kernel. It uses the host's kernel, and Linux features (namespaces and cgroups) isolate its processes, filesystem and resource use.
This has direct consequences for ML work.
| Item | Comes from | Pinned by the image? |
|---|---|---|
| Python interpreter | Image | Yes |
| Python packages | Image (installed from the lock file) | Yes |
| System libraries (glibc, OpenSSL, BLAS) | Image | Yes |
| CUDA runtime and cuDNN libraries | Image, if you install them there | Yes |
| Linux kernel | Host | No |
| NVIDIA driver | Host | No |
| CPU model and instruction sets | Host | No |
The last three rows explain why "same image" does not always mean "same result". A different CPU can select a different vectorized code path in a math library. A different GPU can pick different kernels. The image removes software differences. It does not remove hardware differences.
Layers
Each Dockerfile instruction that changes the filesystem creates a layer. Layers are stacked and identified by content hash. Two images that start with the same instructions share those layers on disk and in the registry.
Layers matter for two reasons:
- Build caching. Docker reuses a cached layer if the instruction and its inputs are unchanged. As soon as one layer changes, every layer after it is rebuilt.
- Size. Deleting a file in a later layer does not shrink the image. The earlier layer still holds the file. Clean up in the same instruction that created the files.
The next chapters build on this. Ordering instructions so that the slow, rarely changing steps come first is the basis of fast rebuilds.
Commands you will use
Check that Docker works, then list local images and containers.
docker --version
docker images
docker ps -aPull an image and run a one-off command in it. --rm deletes the container when it exits.
docker pull python:3.11-slim
docker run --rm python:3.11-slim python --versionTo see how a container isolates its filesystem, run an interactive shell and create a file. The file exists only in that container.
docker run --rm -it python:3.11-slim bash
# inside the container:
echo "scratch" > /tmp/note.txt
exit
docker run --rm python:3.11-slim ls /tmpThe second run lists nothing, because it started from the unchanged image.
Getting data and results in and out
A container's writable layer disappears with the container. Anything you want to keep, such as checkpoints, logs and metrics, must be written outside it. A bind mount maps a host directory into the container.
mkdir -p outputs
docker run --rm -v "$(pwd)/outputs:/app/outputs" ml-train:devHere ml-train:dev is the image built in the next chapter. Files the training script writes to /app/outputs appear in ./outputs on the host.
Do not bake large datasets into the image. The image becomes slow to build and push, and every data change forces a new image. Mount data at run time and version it separately, as a later chapter describes.
Common errors
- Assuming the tag is stable.
python:3.11-slimreceives updates, including security patches and new minor versions of system libraries. A rebuild next month can produce a different image from the same Dockerfile. - Expecting the container to include the GPU driver. The driver lives on the host. The image carries only the CUDA userland libraries. A mismatch between them produces errors when the framework starts, and a later chapter covers it.
- Losing results when the container exits. If outputs go to a path that is not mounted, they vanish with the container.
- Running as root without noticing. Most base images run as root by default. Files created in a bind mount are then owned by root on the host. Pass
--user "$(id -u):$(id -g)"todocker runto avoid this. - Treating a container as a source of truth. Installing packages by hand inside a running container and committing it makes an image no one can rebuild. Build images from a Dockerfile so the recipe is in version control.
- Mixing CPU architectures. An image built for
linux/amd64runs slowly under emulation on an ARM machine such as an Apple Silicon laptop, and some ML wheels are not published for both. Check withdocker image inspect --format '{{.Architecture}}' <image>.
What to carry forward
- A lock file pins Python packages. An image also pins the interpreter, system libraries and CUDA userland.
- A container shares the host kernel and the GPU driver, so those are not part of the image.
- Layers are cached in order, so instruction order affects rebuild time.
- Outputs and data belong outside the container's writable layer.
Exercise
A teammate builds an image from your Dockerfile and gets slightly different training results than you do on the same lock file. Name two causes that remain even though the image is identical, and one cause that exists if the images are not actually identical.
Show answer
With an identical image, the cause could be different hardware (CPU instruction sets or GPU model selecting different kernels) or a different host NVIDIA driver version. The kernel is also host-provided, and nondeterministic operations or unset seeds can differ between runs. If the images are not identical, the likely cause is a moving base image tag (such as python:3.11-slim) that was rebuilt at different times and pulled different system library versions.
Exercise
You run a training container with docker run --rm ml-train:dev. The script saves a checkpoint to /app/outputs/model.pt, but the file is missing on the host after the run. Why, and what change fixes it?
Show answer
The file was written to the container's writable layer, which is deleted when the container exits (and --rm removes the container). Mount a host directory over the output path, for example docker run --rm -v "$(pwd)/outputs:/app/outputs" ml-train:dev, so the file is written to the host.