Course contentsShow
← Cloud and Infrastructure
Lesson 979 of 2,98919. Container FundamentalsFree lesson

Copy-on-Write Strategy

How containers create writable layers on top of read-only image layers for modifications.

Copy-on-Write Strategy

What you'll learn: How containers use copy-on-write to make image layers efficient and allow modifications without changing the original image.

The Core Problem

Remember that container images are built from read-only layers stacked on top of each other. But containers need to run applications, which means writing logs, creating temporary files, and modifying data. How can a container modify files when all the image layers are read-only?

How Copy-on-Write Works

When you start a container from an image, the container runtime adds a thin writable layer on top of all the read-only image layers. This is called the "container layer."

Here's the clever part: when your container needs to modify a file that exists in a lower read-only layer, the system doesn't change the original. Instead, it:

  1. Copies the entire file from the read-only layer up to the writable layer
  2. Writes the changes to this new copy in the writable layer
  3. The original file in the image layer remains untouched

An Everyday Analogy

Think of a textbook in a library (read-only image layers). When you need to highlight or make notes, you don't mark up the library's copy. Instead, you photocopy the page (copy), then write on your personal copy (write). The library's original stays pristine for everyone else to use.

Why This Matters

  • Image integrity: Original images never change, so multiple containers can safely share them
  • Storage efficiency: Only modifications consume extra space
  • Fast container creation: No need to copy entire images when starting containers
  • Isolation: Each container's changes are independent

Key Takeaway: Copy-on-write lets containers modify files by copying them from read-only image layers to a writable container layer, keeping images unchanged and shareable while allowing each container to work independently.