What is the difference between a container's writable layer and a persistent volume?
Learn how a container's ephemeral writable layer differs from a persistent Docker volume, why data survives in volumes, and when to use each for stateful apps.
Expected Interview Answer
A container's writable layer is an ephemeral, copy-on-write layer that exists only for the container's lifetime and is destroyed when the container is removed, whereas a persistent volume is Docker-managed storage that lives independently of any container and survives removal.
Every running container adds a thin writable layer on top of its read-only image layers; all file changes made inside the container are written there. Because that layer is tied to the container ID, deleting the container discards the data, and it is also slower for heavy I/O due to the union filesystem. A volume is stored outside the union filesystem (under /var/lib/docker/volumes by default), can be shared between containers, and is the recommended way to keep databases, uploads, and any state that must outlive the container.
- Data survives container removal and recreation
- Volumes can be shared across multiple containers
- Better I/O performance than the writable layer
- Decouples data lifecycle from container lifecycle
- Easier to back up, migrate, and manage with docker volume commands
AI Mentor Explanation
The writable layer is like chalk marks a batter scuffs onto the crease during one innings — the moment the innings ends and the pitch is rolled, they vanish. A persistent volume is the official scorebook kept off the field: it records runs permanently, is carried to the next match, and is never wiped when a single innings finishes.
Step-by-Step Explanation
Step 1
Understand image layers
An image is a stack of read-only layers; the container cannot modify them directly.
Step 2
The writable layer is added
When a container starts, Docker adds a thin copy-on-write writable layer on top for all runtime changes.
Step 3
See why it is ephemeral
The writable layer is keyed to the container ID, so docker rm deletes it and its data.
Step 4
Create a volume
Use docker volume create or a -v/--mount flag so data is stored outside the union filesystem.
Step 5
Mount and verify persistence
Mount the volume into a path; remove and recreate the container and confirm the data is still there.
What Interviewer Expects
- Knows the writable layer is per-container and ephemeral
- Understands copy-on-write and the union filesystem
- Knows volumes live outside the container lifecycle
- Can name the docker volume commands
- Explains when to choose volumes over the writable layer
Common Mistakes
- Thinking data in the writable layer survives docker rm
- Confusing volumes with bind mounts
- Believing writing to the writable layer is as fast as a volume
- Not knowing volumes can be shared between containers
- Storing database data in the container instead of a volume
Best Answer (HR Friendly)
“A container's writable layer is temporary scratch space that disappears when the container is deleted, while a persistent volume is separate storage that Docker keeps safe even after the container is gone. That is why databases and uploaded files should always be stored in volumes.”
Code Example
# Data written here lives only in the writable layer
docker run --name temp alpine sh -c 'echo hi > /data.txt'
docker rm temp # /data.txt is gone forever
# Create a persistent volume and mount it
docker volume create appdata
docker run --name db -v appdata:/var/lib/postgresql/data postgres:16
# Remove the container; the volume and its data remain
docker rm -f db
docker volume ls # appdata is still listed
docker run --name db2 -v appdata:/var/lib/postgresql/data postgres:16Follow-up Questions
- What is the difference between a volume and a bind mount?
- How does copy-on-write work in Docker's union filesystem?
- Where does Docker store named volumes on the host?
- How would you back up and restore a Docker volume?
- What is a tmpfs mount and when would you use it?
MCQ Practice
1. What happens to data in a container's writable layer when the container is removed?
The writable layer is tied to the container ID, so docker rm deletes it along with any data written there.
2. Which storage option is recommended for a database that must outlive the container?
Named volumes live outside the container lifecycle and are the recommended way to persist stateful data like databases.
3. Where are named volumes stored by default on a Linux host?
Docker manages named volumes under /var/lib/docker/volumes, outside the union filesystem of any container.
Flash Cards
What is the writable layer? — A thin copy-on-write layer added on top of an image's read-only layers, holding all runtime changes and deleted when the container is removed.
What is a persistent volume? — Docker-managed storage outside the union filesystem that survives container removal and can be shared between containers.
Why is a volume faster than the writable layer? — Volumes bypass the union filesystem's copy-on-write overhead, giving better I/O for heavy write workloads.
Which command lists volumes? — docker volume ls