Images and registries
About this lesson
A container image is a read-only package: your app plus every file it needs, built from a Dockerfile as a stack of layers. Build one for a small Python app, line by line. See why a rebuild after a code change takes seconds (the build cache), how a registry stores layers under their sha256 digests so a push or pull only moves what is new, and how a Kubernetes node pulls the image and runs it with one thin writable layer on top.
An image is a sealed package
A container image is a read-only package: your app plus every file it needs, from Linux system files to Python and its libraries. A running copy of an image is a container. Images are built as a stack of layers.
image = app + everything it needs, read-only container = an image, running our image, top to bottom: app.py (our code) pip install (libraries) requirements.txt (a list of libraries) python:3.12-slim (Linux + Python)
Read the lesson step by step
An image is a sealed package
A container image is a read-only package: your app plus every file it needs, from Linux system files to Python and its libraries. A running copy of an image is a container. Images are built as a stack of layers.
image = app + everything it needs, read-only container = an image, running our image, top to bottom: app.py (our code) pip install (libraries) requirements.txt (a list of libraries) python:3.12-slim (Linux + Python)The recipe: a Dockerfile
An image is built from a Dockerfile: a text file of instructions that runs from top to bottom. This one packages a tiny Python shop app. The command docker build reads it and makes the image, layer by layer.
$ cat Dockerfile FROM python:3.12-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY app.py . CMD ["python", "app.py"] $ cat requirements.txt requests==2.32.3 $ cat app.py import requests print("shop 1.0 uses requests", requests.__version__)FROM: start on a base image
FROM python:3.12-slim starts from an image someone else already built: a small Debian Linux with Python 3.12 installed. Docker downloads it once. Its layers become the bottom of our stack.
line 1 FROM python:3.12-slim a base image: an official image on Docker Hub Debian Linux files + Python 3.12 its layers become our bottom layers line 2 WORKDIR /app sets the folder the next lines work inEach change is a layer
COPY puts requirements.txt in the image. RUN pip install then downloads the library it lists. Each instruction that changes files adds a layer: a record of only the files it added or changed, stacked on the ones below.
line 3 COPY requirements.txt . → layer line 4 RUN pip install -r ... → layer a layer holds only what that line changed RUN, COPY and ADD add layers; WORKDIR and CMD mostly record settingsCode on top, then the command
COPY app.py adds our code as the top layer. CMD adds no files: it records the command a container will run, python app.py. The image is finished, and its layers can never be edited, only replaced.
line 5 COPY app.py . → layer line 6 CMD ["python", "app.py"] → setting only finished image: read-only layers + a config: start with python app.pyName it with a tag
docker build -t ghcr.io/acme/shop:1.0 . names the image. The name says where it will live: registry ghcr.io, repository acme/shop, and the tag 1.0, a version label. With no tag at all, Docker uses latest.
$ docker build -t ghcr.io/acme/shop:1.0 . ghcr.io the registry (a server that stores images) acme/shop the repository (one app's images) 1.0 the tag (a version label) no registry → docker.io (Docker Hub) no tag → latestPush: upload to a registry
A registry is a server that stores images, like Docker Hub or GitHub's ghcr.io. docker push uploads each layer. The registry files every layer under its digest: a sha256 fingerprint computed from the layer's bytes.
$ docker push ghcr.io/acme/shop:1.0 4 layers uploaded each layer is stored under its digest: sha256:<64 hex digits> computed from the bytes, so same bytes = same digest, anywhereRebuild: the cache saves time
We fix app.py and build 1.1. Docker reuses every layer before the change from its build cache, marked CACHED, and rebuilds from COPY app.py on. That is why the slow pip install line comes before it.
$ docker build -t ghcr.io/acme/shop:1.1 . CACHED WORKDIR /app CACHED COPY requirements.txt . CACHED RUN pip install -r requirements.txt build COPY app.py . build CMD ["python", "app.py"] a changed layer is rebuilt, with every layer after it so put what changes least firstPush again: only what is new
Pushing 1.1 uploads one layer. For the other three the registry answers "Layer already exists", because it already holds those exact digests. Both versions now share the same base and library layers.
$ docker push ghcr.io/acme/shop:1.1 base layers Layer already exists requirements.txt Layer already exists pip install Layer already exists app.py 1.1 pushed tag = a movable label; digest = fixed pin exact bytes: acme/shop@sha256:...Pull onto a Kubernetes node
A Kubernetes Pod names its image: image: ghcr.io/acme/shop:1.1. The kubelet, the agent on each node, asks the container runtime to pull it. Only layers the node lacks come down; it already had the base.
pod.yaml containers: - name: shop image: ghcr.io/acme/shop:1.1 kubelet → container runtime → pull have: base fetch: 3 layers imagePullPolicy default here: IfNotPresent (tag :latest or none → Always)Run: a thin writable layer
The runtime starts a container: the image's read-only layers plus one thin writable layer on top, where the running app can write. Many containers can share one image. Delete a container and only its writable layer goes.
container = image layers (read-only) + 1 writable layer $ python app.py (the CMD) shop 1.1 uses requests 2.32.3 delete the container → writable layer gone the image is untouched
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Which Dockerfile lines add a new layer of files to the image?
Lines that change files, RUN, COPY and ADD, add layers. CMD only records the start command in the image's settings.
02You edit app.py and rebuild. With the Dockerfile in this lesson, which steps does Docker reuse from its cache?
A changed layer is rebuilt along with every layer after it. Everything before COPY app.py is unchanged, so it comes from the cache, marked CACHED.
03In ghcr.io/acme/shop:1.1, what is 1.1?
ghcr.io is the registry, acme/shop the repository and 1.1 the tag. A digest looks like sha256: followed by 64 hex digits.
04You push version 1.1 after pushing 1.0. Why does the registry say "Layer already exists" for three layers?
A registry files every layer under its digest, a fingerprint of its bytes. Same bytes mean the same digest, so there is nothing new to send.
05A Pod uses image ghcr.io/acme/shop:1.1 and sets no imagePullPolicy. What does Kubernetes use?
The default is Always when the tag is latest or missing, and IfNotPresent otherwise. Either way the node only downloads layers it lacks.
Glossary
- Container image
- A read-only package with an app and every file it needs to run, made of stacked layers plus a small config (such as the start command).
- Layer
- One set of file changes in an image, made by a Dockerfile line such as RUN or COPY. Layers are read-only and can be shared between images.
- Dockerfile
- A text file of instructions (FROM, COPY, RUN, CMD...) that docker build runs from top to bottom to make an image.
- Build cache
- Layers kept from earlier builds. If a line and everything before it are unchanged, Docker reuses its layer instead of running it again.
- Registry
- A server that stores and serves images, such as Docker Hub (docker.io) or GitHub's ghcr.io. You push images to it and pull them from it.
- Tag
- A human-friendly version label on an image name, like 1.1 or latest. Tags can be moved to point at a different image.
- Digest
- A fingerprint of content, written sha256: plus 64 hex digits. Change one byte and the digest changes, so a digest always names exactly the same image.
- Kubelet
- The agent on every Kubernetes node. It asks the container runtime to pull a Pod's images and start its containers.
Notes and sources
- The base image is itself several layers; the figure draws them as one slab. Lines that change files (RUN, COPY, ADD) add layers; CMD and WORKDIR are stored as settings in the image config, though WORKDIR may also add a small layer when it has to create the folder.
- A digest is written algorithm:hex, for example sha256: followed by 64 hex digits. An image as a whole also has a digest (of its manifest, the list of its layers), and that is the one you pin with name@sha256:....
- Tags are mutable: pushing a new image with the same tag moves the tag. The Kubernetes docs put it plainly: digests are immutable, tags can be moved. Give the digest when a Pod must always run exactly the same image.
- imagePullPolicy defaults to Always when the tag is latest or missing, and to IfNotPresent otherwise; with Always the kubelet still only downloads layers the node does not already have.
- ghcr.io/acme/shop is an example name. The Dockerfile, requirements.txt and app.py are real files; the lesson tests compile app.py with python3.
- Docker Docs: What is an image?
- Docker Docs: Understanding the image layers
- Docker Docs: Dockerfile reference
- OCI Image Format Specification: Image configuration (Cmd, WorkingDir)
- Docker Docs: Storage drivers (the container's writable layer)
- Docker Docs: Docker build cache
- Docker Docs: docker image tag
- Docker Docs: What is a registry?
- Docker Docs: docker image pull (pull by digest)
- OCI Image Format Specification: Descriptors and digests
- OCI Distribution Specification
- GitHub Docs: Working with the Container registry
- Kubernetes Docs: Images
- Kubernetes Docs: kubelet
Quiz Unlocks after the last step.