Lesson 2 of 9 · 9 min · Containers and images

Images, layers, tags and registries

Covers: Docker Engine 29 on Ubuntu 24.04; alpine 3.20, python 3.12-slim. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • What a layer is and how layers are shared and cached
  • The union filesystem and copy-on-write
  • Image names: registry, repository, tag and digest
  • pull, images, history, inspect, tag, push, save/load
  • Why latest is a trap and how to pin versions

In simple words

An image is built in layers, like a stack of transparent sheets: one adds the operating system files, the next adds Python, the next adds your code. Layers that are the same are stored once and shared, which keeps downloads small. This lesson shows how images are named, stored and fetched.

Words to know

  • Layer: one step of an image: a set of file changes placed on top of the previous step.
  • Tag: a label on an image, usually a version, such as nginx:1.27.
  • Registry: an online store for images. Docker Hub is the best known.
  • Digest: an image's exact fingerprint. Unlike a tag, it can never point to something else.

Images are stacks of layers

An image is not one big file. It is a stack of read-only layers, each recording the filesystem changes made by one build step. Look at the history of a small image:

docker history alpine:3.20 --format '{{.Size}}\t{{.CreatedBy}}'
0B	CMD ["/bin/sh"]
8.47MB	ADD alpine-minirootfs-3.20.10-x86_64.tar.gz …

The Alpine image has two layers: the root filesystem (8.5 MB) and a metadata step (CMD, which adds no files). Our Python application image shows the layers of its Dockerfile:

0B	CMD ["python" "app.py"]
0B	EXPOSE [8000/tcp]
12.3kB	COPY app.py . # buildkit
13.3MB	RUN /bin/sh -c pip install --no-cache-dir -r…
12.3kB	COPY requirements.txt . # buildkit
8.19kB	WORKDIR /app

(listed newest first; below these are the layers of the python:3.12-slim base image). Each RUN, COPY and ADD instruction in a Dockerfile creates a layer; CMD, ENV, EXPOSE and similar only add metadata.

Why layers matter

  1. Sharing. Layers are identified by a hash of their content. If ten images are built on python:3.12-slim, its layers are stored once on disk and downloaded once. Pulling a new version of your app only downloads the layers that changed.
  2. Build caching. Docker reuses a layer if the instruction and everything before it are unchanged (lesson 3).
  3. Layers are immutable and cumulative. Deleting a file in a later layer does not shrink the image: the file still exists in the earlier layer, just hidden. This is why a secret COPYed and then RUN rm'd is still in the image (see the secrets lesson), and why large temporary files should be removed in the same RUN that created them.

The writable layer and copy-on-write

When a container starts, Docker adds a thin writable layer on top of the image's read-only layers, and a union filesystem (overlay2) presents them as a single tree. Reading a file reads from the topmost layer that has it. Writing a file that lives in a lower layer first copies it up into the writable layer (copy-on-write). Containers from the same image share the read-only layers and each get their own writable layer, so starting a hundred containers costs almost no extra disk. When the container is removed, its writable layer is deleted.

Names: registry, repository, tag, digest

A full image name has parts:

registry.example.com / myteam/shop-api : 1.4.2
└────── registry ─────┘└─ repository ─┘ └ tag ┘
  • nginx:1.27-alpine is shorthand for docker.io/library/nginx:1.27-alpine (Docker Hub, the official library namespace).
  • A tag is a movable label for a version (1.27-alpine, latest). Tags can be repointed to different content over time.
  • A digest (sha256:d9e853e8...) identifies content immutably. alpine:3.20@sha256:d9e853e8... always means the exact same bytes.

Show digests: docker images --digests. The Alpine 3.20 digest we pulled began sha256:d9e853e87e55526f6b2917d....

latest and pinning

latest is just a tag name that is the default when you give none. It does not mean "newest" or "stable", and it changes without notice. Using FROM node or image: myapp:latest makes builds and deployments non-reproducible: today's build and next month's differ, and a rollback "to the previous version" has no meaning.

Pin versions:

  • Base images: a specific version tag (python:3.12-slim, better python:3.12.7-slim-bookworm), and for the strictest reproducibility the digest.
  • Your own images: tag with something unique and traceable, such as the Git commit SHA or a semantic version (shop-api:1.4.2, shop-api:3f9c2a1). You may also keep a moving tag like 1.4, but deploy by immutable identifiers.

Everyday commands

docker pull nginx:1.27-alpine                  # download an image
docker images                                  # list local images (docker image ls)
docker image inspect nginx:1.27-alpine         # JSON: layers, env, cmd, exposed ports
docker tag shop-api:dev registry.example.com/team/shop-api:1.4.2   # give it another name
docker login registry.example.com
docker push registry.example.com/team/shop-api:1.4.2
docker rmi nginx:1.27-alpine                   # remove a local image
docker image prune                             # remove dangling (untagged) images
docker save -o shop-api.tar shop-api:1.4.2     # export to a file (offline transfer)
docker load -i shop-api.tar

Sizes from our machine: alpine:3.20 12.2 MB, busybox:1.36 6.8 MB, python:3.12-slim 180 MB, python:3.12-alpine 74.8 MB, debian:12 185 MB. Base image choice sets a floor under every image you build (see the security lessons).

Registries

A registry stores and serves images. Docker Hub is the public default (watch its anonymous pull rate limits in CI). Cloud registries: Amazon ECR, Google Artifact Registry, Azure Container Registry, GitHub Container Registry (ghcr.io). Self-hosted: Harbor, the open-source registry image. In a company you push images to a private registry from CI, and servers or Kubernetes pull from it using credentials. Registries also support scanning, signing and retention policies (delete untagged images older than N days to control cost).

Multi-architecture images

The same tag can point to a manifest list with variants for several CPU architectures (linux/amd64, linux/arm64). Docker pulls the one matching your machine. This matters now that many laptops (Apple Silicon) and cloud instances (AWS Graviton) use ARM. A mismatch shows as exec format error. Build for both with docker buildx build --platform linux/amd64,linux/arm64.

Common mistakes

  • Using latest (or no tag) in Dockerfiles, Compose files and Kubernetes manifests.
  • Thinking rm in a later layer shrinks the image.
  • Baking secrets or credentials into layers.
  • Never cleaning up, until the disk is full.
  • Pushing images without a unique tag, which makes it impossible to know what runs in production.
  • Forgetting the architecture when building on an ARM laptop for x86 servers.

Try it yourself

  1. Pull python:3.12-slim and python:3.12-alpine; compare docker images sizes and docker history.
  2. Pull an image twice and note that the second pull downloads nothing. Pull a second image sharing a base and watch "Already exists" lines.
  3. Tag an image with two names and show with docker images that they have the same image id.
  4. Use docker image inspect --format '{{json .RootFS.Layers}}' IMAGE to list the layer hashes of an image.

Check yourself

What happens to the files you delete in a later Dockerfile step?

They are hidden in the final filesystem but remain in the earlier layer, so the image does not get smaller and the data can be recovered from the layer.

Why are images efficient to store and pull?

Layers are content-addressed and shared. Images with the same base share its layers, and a pull downloads only the layers you do not already have.

What is the difference between a tag and a digest?

A tag is a movable, human-friendly label that can be repointed. A digest is the hash of the exact content and is immutable.

Why is FROM python:latest a bad idea?

latest changes over time, so builds are not reproducible and an upgrade can silently break the application. Pin an exact version.

Interview questions

How does copy-on-write work for containers?

Containers share the image's read-only layers and have one thin writable layer. When a container modifies a file that exists in a lower layer, the file is first copied into the writable layer and modified there, leaving the image untouched.

How would you identify which exact image is running in production?

Deploy by an immutable identifier: a unique tag such as the Git SHA or, better, the digest, and record it in the deployment manifest, so you can map any running container back to a source commit and a build.

Why do you get exec format error?

The image's CPU architecture does not match the host (for example an arm64 image on an amd64 server). Build or pull the right platform, or publish a multi-architecture image.

Summary

  • Images are stacks of immutable, shared, content-addressed layers; containers add a writable layer with copy-on-write.
  • Names are registry/repository:tag; digests identify content exactly; latest is not "newest".
  • Pin base versions and tag your images uniquely (Git SHA or version).
  • Keep images small and clean: remove temporary files in the same layer and never put secrets in a layer.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Docker and containers for DevOps Engineer and scores every answer, with what to fix.