Skip to content

Lesson 2 of 9 · 26m

Image Layers, OverlayFS, and Why Builds Explode

Read Dockerfiles as layer-producing instructions, understand copy-on-write behavior, and control layer ordering for cache and size.

Images look simple until you inherit one with a 2.8 GB deploy artifact and nobody knows which line added the compiler toolchain. An image is an ordered stack of filesystem layers plus configuration metadata. A running container adds a thin writable layer on top. OverlayFS combines them into one apparent filesystem. Once you internalize that, most image-size and cache mysteries become mechanical.

Every instruction has a filesystem consequence

Consider this common Dockerfile:

FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*
COPY app /app
CMD ["/app/server"]

The rm does not erase the package indexes from history. It creates whiteouts in a later layer while the bytes remain in the earlier layer. Inspect the result:

docker build -t layer-demo .
docker history layer-demo --no-trunc
dive layer-demo

docker history shows per-instruction size; dive shows which layer actually introduced files. Both are indispensable when a base image becomes a junk drawer.

Overlay is copy-on-write

When a container writes to a file supplied by the image, OverlayFS copies the file into the container's writable upper layer before modifying it. Deleting an image file creates a whiteout rather than removing lower-layer data. The practical consequences are:

large mutable files        expensive copy-up on first write
rm in later layers         hides but does not reclaim image bytes
cache invalidation         every instruction after the changed line reruns
shared base layers         disk reuse across images with identical digests

Order Dockerfile lines by volatility

Put stable, expensive operations first and volatile source code last:

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY src/ ./src/
CMD ["python", "-m", "src.server"]

If COPY . . comes before dependency installation, every source edit invalidates the dependency cache. That is how laptops melt and CI bills grow.

Use .dockerignore as a build boundary

The build context is tarred and sent to the daemon before the first instruction matters. Exclude everything that is not input to the build:

.git
node_modules
dist
coverage
.env
.env.*
*.pem
/var/lib/docker

A secret copied into a layer and then deleted remains recoverable from image history. Do not treat deletion as redaction.

Field notes

  • docker image prune removes dangling layers, not images still referenced by tags or containers.
  • Layer sharing depends on identical content and lineage, not merely identical filenames.
  • Prefer explicit package versions in production Dockerfiles; floating dependencies create cache hits with different software.
  • COPY --link can reduce cache coupling in modern BuildKit builds, but it does not fix a sloppy context.
  • Check both compressed registry size and uncompressed runtime size; they answer different operational questions.

Was this lesson helpful?

Sign in to rate