Lesson 2 of 24Containers
Images and Containers
Understand image layers, tags, digests, and registries, and drive a container through its whole lifecycle.
Every Docker command you will ever type acts on one of two things: an image or a container. Once you can picture an image as a stack of read-only layers and a container as a thin writable sheet on top, caching, disk usage, and "where did my file go?" stop being mysteries.
Images are stacks of read-only layers
An image is not one big file. It is a stack of layers, and each layer is a set of filesystem changes: files added, changed, or deleted. A Python API image might look like this, with the base at the bottom.
The base image is itself a few layers: a minimal Debian filesystem and a Python install. You did not build it; you pulled it.
A union filesystem (Docker uses overlay2) merges the layers into one view. When the container reads a path, Docker looks from the top layer down and returns the first match. When the container changes a file from a lower layer, Docker first copies it up into the writable layer, then changes the copy. This is called copy-on-write, and it is why the image underneath never changes.
Run this model of a union filesystem. Deleting a lower-layer file does not remove it; it writes a "whiteout" marker that hides it.
Press Run (⌘ Enter) to see what happens.What does this print?
layers = [{"/a": "base"}, {"/a": "app"}] # bottom to top
container = {}
def read(path):
for layer in [container] + layers[::-1]:
if path in layer:
return layer[path]
print(read("/a"))
container["/a"] = "edited"
print(read("/a"))
print(layers[1]["/a"])Names, tags, and digests
A full image reference has up to four parts: registry/namespace/repository:tag.
python:3.13-slim # short for docker.io/library/python:3.13-slimghcr.io/acme/api:1.4.0 # GitHub Container Registry123456789012.dkr.ecr.us-east-1.amazonaws.com/api:7f3c2a1 # AWS ECRpython@sha256:4c2e5a9f... # pinned by digest- A tag (
3.13-slim,1.4.0) is a human-friendly label. Tags are mutable: the owner can pointpython:3.13-slimat a new build tomorrow with security patches. latestis just the tag Docker uses when you do not write one. It does not mean "newest"; it means "whatever was last pushed with the taglatest".- A digest (
sha256:...) is a hash of the image content. It is immutable: the same digest always means byte-for-byte the same image.
docker pull myapi:latest a week apart. Are they guaranteed to run the same code?Registries: how images travel
A registry is a server that stores images and hands them out. Docker Hub is the default public one. Teams also use GitHub Container Registry (ghcr.io), AWS ECR, Google Artifact Registry, and Azure Container Registry. Pushing uploads only the layers the registry does not already have, and pulling downloads only the layers you are missing.
An image is built from a Dockerfile, on a laptop or (better) in CI.
Driving a container
These commands cover most daily work. Try reading each comment before the command.
docker pull python:3.13-slim # download an imagedocker images # list local imagesdocker run -d --name api -p 8000:8000 myapi:1.0 # create + start in the backgrounddocker ps # running containersdocker ps -a # include stopped onesdocker logs api # what the process printeddocker exec -it api sh # open a shell inside the running containerdocker stop api # send SIGTERM, then SIGKILL after 10 sdocker start api # start the same container againdocker rm api # delete the container and its writable layerMatch the goal to the command.
The container lifecycle
A container moves through a few states. docker run is really two commands in one: docker create plus docker start.
docker create sets up the writable layer, network, and config. Nothing is running yet.
Put a container's typical life in order.
- Tap the steps below in the right order.
Containers are disposable
The writable layer lives and dies with the container. That is a feature: you can throw a container away and start a fresh one from the image in a second, which is how deploys, rollbacks, and scaling work. It also means you must never keep important data only in a container.
FROM python:3.13-slim. Roughly how many copies of the Python base layers are stored on disk?- layer
- A read-only set of filesystem changes. Images are stacks of layers, shared on disk by content hash.
- copy-on-write
- A container changing a file from the image first copies it into its own writable layer, so the image never changes.
- tag
- A movable, human-readable label for an image, like
3.13-slimorlatest. - digest
- The immutable
sha256:content hash of an image. Same digest, same bytes. - registry
- A server that stores and serves images: Docker Hub, GHCR, ECR, Artifact Registry.
- writable layer
- The thin per-container layer where all writes land. Deleted with
docker rm.