Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 31 of 8021. Container FundamentalsFree lesson

Understanding Immutable Image Layers

Explain how Docker images are constructed as a series of read-only layers that represent filesystem changes during the build process.

In this lesson, you will learn how Docker images are built as a stack of "read-only" layers that represent every change made to the filesystem.

The Stackable Concept

Think of a Docker image like a transparent stack of plastic sheets. Each sheet contains only the files you added or changed during a specific step of the build process. Once a sheet is placed in the stack, it is "locked" and cannot be changed. If you need to update a file, you don't edit the existing sheet; instead, you add a brand new sheet on top that records the new version of that file.

This structure is what we call immutability. Because previous layers are read-only, they are inherently stable and consistent across every environment where your container runs.

Visualizing Layers in a Dockerfile

Every time you include an instruction like RUN, COPY, or ADD in your Dockerfile, Docker creates a new layer:

FROM ubuntu:22.04         # Layer 1: Base image
COPY app.py /app/         # Layer 2: Adds the file app.py
RUN apt-get update        # Layer 3: Creates a layer with updated package index

When you look at this, imagine these as three distinct physical blocks stacked on top of one another. The final "image" is simply the view looking down through all three layers combined.

Why Does This Matter?

This layering system is incredibly efficient for two reasons:

  1. Sharing: If you have ten different applications based on the same ubuntu:22.04 image, your host computer only stores that base layer once. All ten containers point to those same read-only files.
  2. Caching: During the build process, if you change app.py but not the apt-get update command, Docker knows it doesn't need to re-run the update. It simply re-uses the cached layer from your previous build and only creates a new layer for the change.

By building images this way, Docker ensures that once an image is created, it remains identical whether it is on your laptop, a testing server, or a production cluster.

Key Takeaway: Docker images are composed of immutable, read-only layers that stack together to form a complete filesystem, enabling efficient storage and reliable, reproducible deployments.