The idea
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.
Build: a Dockerfile, layer by layer
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.
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.
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.
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.
Share: tags, registries, the cache
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.
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.
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.
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.
Run: pull and start
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.
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.
What happens
;; step 1/11 · image 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 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. 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.