Skip to content
BIASED

Container images

What a container image is: content-addressed layers and a config under an OCI manifest, union file systems, Dockerfile instructions and the build-cache order rule, multi-stage builds, small bases, non-root users, and pinning by digest.

What an image is

A built program still needs an interpreter or libc, locked packages, OpenSSL, CA certificates and a timezone database. Copying only the binary to a server recreates "works on my laptop" failures. A container image ships the program with the user-space file tree it expects. The OS course covered how a container runs (namespaces, cgroups, OverlayFS). This topic is the image — the artifact CI builds and CD promotes.

By the OCI image specification an image is a small graph of blobs named by SHA-256 digests:

  • Layers — compressed tar archives of files one build step added, changed or deleted. A typical node:20-slim base may be ~200 MB compressed; your npm ci layer another 150 MB; the application copy 5 MB.
  • Config — JSON: architecture, Cmd/Entrypoint, env, working directory, user, rootfs.diff_ids.
  • Manifest — JSON naming config and layers by digest; the manifest digest is the image identity.
  • Index — for multi-arch, a list of manifests (a tag like alpine:3.22 often points here so linux/amd64 and linux/arm64 resolve correctly).

Nothing here requires Docker: you can write an OCI layout by hand or build with BuildKit, Podman or Kaniko. Changing one byte in a file changes that layer's digest and therefore the manifest — a Merkle tree, like Git. Two tags can point at the same digest; one tag can move to a new digest tomorrow. Deploy digests, not floating tags.

Dockerfile and cache order

A Dockerfile is a recipe of instructions; each typically produces a layer. The cache order rule: instructions form a prefix cache — the first changed instruction invalidates every layer after it. Copy the lockfile and install dependencies before copying frequently edited application sources so the dependency layer stays warm across commits that only touch src/.

Worked timing. Cold npm ci on CI: 90 s. Application COPY + build: 20 s. If every commit invalidates npm ci because COPY . . came first, 100 commits/day waste ~2.5 CPU-hours on dependency install alone. Putting COPY package.json package-lock.json + RUN npm ci first drops that to roughly one cold install per lockfile change.

Multi-stage builds use a fat builder image (compiler, headers, SDK) and COPY --from=… only the artifact into a thin runtime image, so the toolchain never ships to production. A Go service that builds in golang:1.22 (~800 MB) and copies a 20 MB static binary into gcr.io/distroless/static keeps the deployed attack surface tiny and pulls fast on every node.

.dockerignore matters as much as layer order: copying a 2 GB .git or local node_modules into the build context slows uploads and can poison the cache key. Measure context size; keep it under a few tens of megabytes when you can.

Small and safe images

Smaller bases mean fewer packages and fewer vulnerabilities — pick distroless, Alpine, Chainguard or a slim distro deliberately, knowing the debugging trade-off (no shell in distroless means you debug with ephemeral debug containers or ephemeral kubectl debug). Run as non-root (USER with a non-zero uid such as 65532). Pin by digest (image@sha256:…) for bases and for what you deploy; tags move when maintainers republish. Scan images in CI (Trivy, Grype, registry scanners); a clean scan is not a substitute for patching the base on a cadence — yesterday's zero-CVE image grows CVEs as disclosures land.

Layer size is an ops number you can budget. Prefer one RUN apt-get update && apt-get install … && rm -rf /var/lib/apt/lists/* over three separate RUNs that each leave package lists in intermediate layers. Deleting a file in a later layer does not shrink the image: the bytes remain in the lower blob (see whiteouts below). Rebuild from a cleaner base instead of rm-ing your way to small.

Layers and the union FS

At runtime OverlayFS stacks layers into one tree: upper wins. Deleting a lower file is a whiteout in an upper layer — lower blobs stay immutable and content-addressed. Shared digests are stored once in the registry and once on disk, so two services on the same base share the download.

Worked storage. Two images share three identical lower layers and each has one unique upper layer → the registry stores 5 unique layer blobs (3 shared + 2 unique), not 8. On a node pulling both, the three shared layers download once. At 80 MB per shared layer that is 240 MB saved per node — material when hundreds of nodes pull on a rolling restart.

Writable container layers (the upperdir for a running container) are for temp files, not for state you care about: they vanish when the container is removed. Persist with volumes; keep images immutable.

What changed

Copy program to server VM image Container image
What ships binary only full disk + kernel user-space layers + config
Identity filename hope large file hash manifest digest
Start time depends minutes seconds
Isolation weak strong (hypervisor) namespaces/cgroups
Typical size MBs GBs tens–hundreds of MB

The next topic, Continuous integration, runs the build and image steps on every change on clean machines — so the digest you just learned to pin is produced the same way for every commit.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Image layers and cache hashoptional

    For a 5-step Dockerfile mark which layer a COPY vs RUN apt edit invalidates.

    ~20 min · easier on a computer · The matching course topic has been read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.