liveBook · Manning

Copy-on-write

As explained in Learn Docker in a Month of Lunches, Second Edition

Copy-on-write is a storage technique that copies shared data into a writable location only when a modification is made.

Copy-on-write lets a container edit a file from a read-only image layer without changing the image. Docker copies the file into the container’s writable layer, then applies edits there. section 6.1

The storage behavior follows a simple sequence:

  • The container reads the original file from an image layer. section 6.1
  • An edit triggers a copy into the container’s writable layer. section 6.1
  • The changed file affects that container only; the image and other containers remain unchanged. section 6.1

Change a file inside a container

Run the file-display container, copy a replacement file to /input.txt, and start it with attachment. The same container then displays the new contents, while a new container uses the image’s original file.

docker container run --name f1 diamol/ch06-file-display:2e
echo "https:/./blog.sixeyed.com" > url.txt
docker container cp url.txt f1:/input.txt
docker container start --attach f1

Use the runnable command sequence to observe copy-on-write through a file edit and the container’s subsequent output. section 6.1

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Works through the writable-layer mechanism and a file-edit exercise, but stops short of storage-driver internals.
Examples
Uses Docker commands to replace /input.txt, rerun a file-display container, and compare original and changed contents.
What it assumes
Assumes basic container and image familiarity while teaching image layers, writable layers, and container data lifetime.
What you can do after
Apply it via Docker commands to edit container files and distinguish writable-layer changes from image contents.
How it connects
Connects image layers, writable layers, container filesystems, Docker volumes, and transient container data.

Also explained in

Compare how 5 books explain copy-on-write →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.