ENع
←Images
Lesson 08 · Containers and Kubernetes

Images and registries

12 min11 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 8 · Interactive
01 / 11

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)
Loading figure…
or ← →
Read the lesson step by step
  1. 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)
  2. 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__)
  3. 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 in
  4. Each 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 settings
  5. Code 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.py
  6. Name 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      → latest
  7. Push: 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, anywhere
  8. Rebuild: 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 first
  9. Push 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:...
  10. 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)
  11. 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

Quiz Unlocks after the last step.