Review: Docker Storage Architecture

You've mastered the persistence layer. You now understand that containers are ephemeral, but your data doesn't have to be. From Bind Mounts for development to Volumes for production, and Overlay2 for the image layers themselves.

Key Takeaways

  1. Ephemeral by Default: The container’s Read-Write layer dies with the container. Never trust it with state.
  2. Bind Mounts (-v /host:/container): Best for Development. Connects host source code to the container for live reloading. Performance can vary (Mac/Windows).
  3. Volumes (-v vol:/container): Best for Production. Managed by Docker, high performance, safe from permissions issues.
  4. Tmpfs: Best for Secrets/Cache. In-memory only. Zero disk I/O.
  5. Overlay2: The storage driver that merges read-only image layers with the writable container layer using Copy-on-Write (CoW).

Module Review: Docker Storage

[!NOTE] This module explores the core principles of Module Review: Docker Storage, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.

1. Decision Matrix

Requirement Solution Why?
I need to persist DB data Volume High perf, managed, easy backup.
I need to edit code live Bind Mount Host changes reflect instantly.
I need to inject a config file Bind Mount Simple file mapping.
I need to store API keys Tmpfs Never touches disk (Security).
I need to massive scratch space Volume Disk is cheaper than RAM.

2. Interactive Flashcards

Test your knowledge. Click to flip.

What is Copy-on-Write (CoW)?
A strategy where Docker only copies a file from the Read-Only image layer to the Read-Write container layer when it is modified.
Why are Bind Mounts slow on Mac/Windows?
Because files must cross the boundary between the Host OS and the Linux VM, adding overhead. (Mitigated by VirtioFS).
What happens to a Volume when you delete a container?
Nothing. Volumes are independent lifecycle objects. You must explicitly prune them.
Where does Overlay2 store the container's changes?
In the `UpperDir`.
Does `tmpfs` persist data after reboot?
No. RAM is volatile. Data is lost when the container stops or the host reboots.

3. Cheat Sheet

CLI Commands

Command Description
docker volume create <name> Create a named volume
docker volume ls List all volumes
docker volume inspect <name> See where data lives on host
docker volume prune Delete all unused volumes
docker run -v vol:/path Mount a volume
docker run -v $(pwd):/path Bind mount current dir
docker run --tmpfs /path Mount a tmpfs

Dockerfile

Instruction Effect
VOLUME ["/data"] Declares a mount point. If user doesn’t provide a volume, Docker creates an Anonymous Volume.