Skip to content

Lesson 2 of 24Containers

Images and Containers

Understand image layers, tags, digests, and registries, and drive a container through its whole lifecycle.

14 min read0/6 questions

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.

Layers of myapi:1.0
  1. Container layer (writable)added by docker run, one per container
  2. COPY . .your source code, about 40 KB
  3. RUN pip install -r requirements.txtFastAPI, Pydantic, about 30 MB
  4. COPY requirements.txt .1 KB
  5. python:3.13-slimDebian slim + Python, about 120 MB

The base image is itself a few layers: a minimal Debian filesystem and a Python install. You did not build it; you pulled it.

1/4

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.

Pythonunion_fs.py
WHITEOUT = object()layers = [ # bottom to top, all read-only {"/etc/os-release": "Debian 12", "/usr/local/bin/python3": "3.13"}, {"/app/requirements.txt": "fastapi"}, {"/app/main.py": "v1"},]container = {} # the writable layer def read(path): for layer in [container] + layers[::-1]: if path in layer: return "No such file" if layer[path] is WHITEOUT else layer[path] return "No such file" container["/app/main.py"] = "v2 (edited inside the container)"container["/etc/os-release"] = WHITEOUT # rm /etc/os-release print(read("/app/main.py"))print(read("/etc/os-release"))print("image layer still has:", layers[2]["/app/main.py"])
Output
Press Run (⌘ Enter) to see what happens.
Predict the outputPython

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"])
Need a nudge? Hints walk you through it.

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 point python:3.13-slim at a new build tomorrow with security patches.
  • latest is just the tag Docker uses when you do not write one. It does not mean "newest"; it means "whatever was last pushed with the tag latest".
  • A digest (sha256:...) is a hash of the image content. It is immutable: the same digest always means byte-for-byte the same image.
Quick check
Two servers ran docker pull myapi:latest a week apart. Are they guaranteed to run the same code?
Need a nudge? A hint rules out a wrong answer.

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.

Build once, run anywhere
Your laptop: docker buildYour laptopdocker buildCI runner: docker pushCI runnerdocker pushRegistry: ghcr.ioRegistryghcr.ioProd server: docker pullProd serverdocker pullTeammate: docker pullTeammatedocker pullContainer: docker runContainerdocker runpushpushpullpullrun

An image is built from a Dockerfile, on a laptop or (better) in CI.

1/4

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 layer
Match the pairs0/5 matched

Match the goal to the command.

Choose an item, then its match on the other side.

The container lifecycle

A container moves through a few states. docker run is really two commands in one: docker create plus docker start.

Container states
Image: read-onlyImageread-onlyCreated: docker createCreateddocker createRunning: process aliveRunningprocess alivePaused: frozenPausedfrozenExited: stoppedExitedstoppedRemoved: docker rmRemoveddocker rmcreatestartpausestop / startrm

docker create sets up the writable layer, network, and config. Nothing is running yet.

1/5
Put it in order4 steps

Put a container's typical life in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

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.

Quick check
Your Postgres container stores data in its writable layer. Which action loses the data?
Need a nudge? A hint rules out a wrong answer.
Quick check
You have 5 images on your laptop, all built FROM python:3.13-slim. Roughly how many copies of the Python base layers are stored on disk?
Need a nudge? A hint rules out a wrong answer.
Key terms
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-slim or latest.
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.