Topic 03: OverlayFS & Content-Addressable Layer Caching

Target Duration: 2–4 minutes (~300–450 spoken words)
Focus: Pointwise verbal delivery covering image build mechanics, SHA-256 Merkle layer caching, ephemeral build sandboxes, and OverlayFS CoW / whiteouts.


🎙️ Pointwise Spoken Speech (Word-for-Word Delivery)


📋 Step-by-Step Summary (What, How & Why)

Step What Was Done How It Works Why This Mechanism / Order
1. Base Layer Bootstrapped base rootfs Downloaded minimal distro once and marked as immutable base. Provides shared foundational OS files across all images without duplication.
2. Layer Hashing Computed chained SHA-256 IDs hash = SHA256(type + parent_hash + content_hash). Merkle chaining guarantees automatic transitive cache invalidation.
3. Ephemeral Sandbox Ran cache-miss steps in overlay Mounted parent layers as lower, ran command in sandbox, captured upper diff. Captures strictly the incremental filesystem delta created by that command.
4. Runtime CoW Applied Copy-on-Write at runtime Copies lower files to upperdir on edit; uses 0/0 whiteout devices for deletes. Allows multiple containers to share the same image layers safely in parallel.

❓ Anticipated Interview Questions & Crisp Answers

Q1: Why is workdir required in OverlayFS? Why can't OverlayFS write directly to upperdir?

Answer: POSIX file operations like file creation, renaming, and truncating must appear atomic. If a crash or power cut occurs mid-operation, writing directly to upperdir could leave corrupted files in the container root. OverlayFS prepares the metadata and files in workdir first, then issues an atomic kernel rename into upperdir.

Q2: How does OverlayFS handle directory deletions vs. regular file deletions?

Answer: For regular files, OverlayFS places a 0/0 character device (whiteout) in upperdir. For directories, OverlayFS creates an opaque directory by setting the extended attribute trusted.overlay.opaque=y on the directory in upperdir, instructing the kernel to stop searching lower layers.

Q3: Why hash the file contents rather than timestamps during COPY?

Answer: File modification timestamps (mtime) change during git checkouts, cloning, or file transfers without any actual code change. Hashing the concatenated SHA-256 digests of all source files ensures cache hits are purely content-deterministic across different machines and builds.

Q4: What is a Merkle DAG, and how is it used in container layer caching?

Answer:
- What it is: A Merkle DAG (Directed Acyclic Graph) is a graph data structure where every node is uniquely identified by a cryptographic hash of its own contents plus the cryptographic hashes of its parent nodes. It is acyclic (no circular references) and directed (child nodes explicitly point back to their parents).
- How it is used in container layer caching:
1. Content-Addressability: Each container layer's identity is derived as SHA256(instruction_type + parent_layer_hash + content_or_command_hash).
2. Transitive Invalidation: Because child node IDs embed their parent's hash, changing an intermediate layer $K$ instantly alters its hash. Consequently, all descendant layers $K+1 \dots N$ have different parent inputs, causing their hashes to change and automatically missing the cache without requiring a separate dependency tracking database.
3. Deduplication: Multiple different images that share the same base commands (e.g. FROM debian followed by RUN apt update) compute identical hashes for those shared prefixes and reuse the exact same layer directories on disk.