The container filesystem is ephemeral
Files a container writes live in its writable layer: kept across restarts, deleted with docker rm and lost on every image upgrade. Where data goes instead.
docker run and the container lifecycleLayers, overlayfs and copy-on-write
The container filesystem is ephemeral
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Every container gets its own writable layer on top of the read-only image. Everything it writes outside a mount goes there.
- The writable layer survives docker stop, start and restart, and is deleted with the container by docker rm, docker run --rm or a prune.
- Upgrading means replacing the container with one from a new image, so any data left in the writable layer is lost on every upgrade.
- Docker offers three places outside the writable layer: volumes for data Docker manages, bind mounts for host paths, and tmpfs for data that should never touch disk.
- An image's VOLUME instruction makes Docker create an anonymous volume at that path, so some images protect their data directory and others do not.
- docker diff shows what a container changed in its writable layer, and --read-only makes accidental writes fail loudly.
Where did the rows go?
You run a database in a container, load some data, and everything works. A week later you pull a newer image and recreate the container with the same command. The database starts empty. Nothing crashed and no error appeared. The data was in the old container, and you removed the old container.
This is the most common way people lose data with Docker, and it follows directly from how containers store files.
The writable layer
An image is read-only layers. When you start a container, Docker adds one thin writable layer on top that belongs to that container alone. The storage docs put it simply: by default all files created inside a container are stored on this writable layer, and the data “doesn’t persist when the container is destroyed”. The mechanics, copy-on-write over the image, are in the overlayfs lesson.
The lifetime of that layer is the lifetime of the container, not of the process:
| Action | Writable layer |
|---|---|
docker stop, docker start, docker restart |
Kept |
| The process crashes and a restart policy restarts it | Kept |
docker rm, docker run --rm on exit, docker container prune |
Deleted |
| Upgrading to a new image | Deleted, because upgrading means a new container |
The last row is the trap. A container’s image cannot be swapped in place. Every upgrade, every config change that needs a recreate and every docker compose up that detects a change creates a new container with a new, empty writable layer.
Think of the container as a hotel room. You can leave and come back, and your things are still there. Check out, and housekeeping clears the room. Anything you want to keep goes in a locker that is not part of the room.
Seeing what a container wrote
docker diff lists what a container has changed relative to its image, marking each path A for added, C for changed or D for deleted:
docker diff pg
On a database container you would see its data files there if no volume was mounted. It is a quick audit: anything important in that list is one docker rm from gone.
docker commit can turn a container’s writable layer into a new image. It is tempting as a backup and a bad habit. The result has no Dockerfile, mixes data with software, and includes every log and temp file. Software changes belong in the Dockerfile, data belongs in a volume.
Images that protect their own data
The VOLUME instruction in a Dockerfile tells Docker to put an anonymous volume at that path for every new container, so writes there skip the writable layer. Whether an image does this is up to its authors. I read the configs of some official images from Docker Hub’s registry API:
| Image | VOLUME in the image config |
|---|---|
postgres:18 |
/var/lib/postgresql |
mysql:9, mariadb:11 |
/var/lib/mysql |
mongo:8 |
/data/db, /data/configdb |
redis:7 |
/data |
redis:8 |
none, only WORKDIR /data |
nginx |
none |
So redis:8 with no -v writes its dump straight into the writable layer, where redis:7 used an anonymous volume. An anonymous volume is not much safer anyway. It gets a random name, a container recreated with plain docker run gets a fresh one (Compose carries the old one over), and docker run --rm deletes it. Do not rely on VOLUME. Mount a named volume on purpose, as the next lesson shows.
Three places to put data instead
The storage overview lists the mount types that bypass the writable layer:
- Volumes. Storage managed by Docker, which outlives any container. The default answer for application data.
- Bind mounts. A host directory mapped into the container, for source code and config files you edit on the host.
- tmpfs mounts. Memory only, gone when the container stops, for scratch data and anything that must never touch disk.
The bind mounts and tmpfs lesson covers the last two. The docs add a performance reason too: writing to the writable layer goes through the storage driver’s union filesystem, which is slower than a volume writing to the host filesystem directly.
Making ephemerality explicit
A good container treats its own filesystem as disposable. Logs go to stdout for the logging driver. State goes to a volume or a database. Scratch space goes to a tmpfs. You can enforce it:
docker run --read-only --tmpfs /tmp -v notes-data:/var/lib/notes notes:1.4.2
With --read-only, any write outside the mounts fails immediately with “Read-only file system”, so a forgotten data path shows up on the first run rather than after the first upgrade. Read-only containers also have security benefits.
About the video
The embed is LearnCantrill’s “Free Docker Fundamentals Course - Docker Storage - Writable Layer, Bind Mounts & Volumes”, January 2023, 5 minutes, from Adrian Cantrill’s free course. Its chapters cover the writable layer, tmpfs, bind mounts and volumes, with diagrams. It is a short overview, so the lesson adds the lifecycle table, docker diff and the VOLUME behaviour of real images. English was judged from the channel and the English caption track. Pål-Kristian Hamre’s video in deeper opens with a demo of the data disappearing.
What was run: the VOLUME and WORKDIR entries were read from the image configs of postgres:18, mysql:9, mariadb:11, mongo:8, redis:7, redis:8 and nginx:latest, fetched from Docker Hub’s registry API with curl. No container was run. The lifecycle rules and mount types are from the storage overview and volumes pages, checked 2026-11-28.
Where this fits
The writable layer is for nothing you want to keep. Next, the place you keep it: named volumes.
Check yourself
You run Postgres with no -v flag, insert data, then docker stop and docker start the container. Is the data still there?
Yes. Stopping and starting keeps the same container and its writable layer. The data is lost when the container is removed, for example to upgrade the image.
You upgrade from redis:7 to redis:8 by recreating the container with the same docker run command and no -v. What changes about where data lives?
redis:7 declares VOLUME /data, so its data went into an anonymous volume. redis:8's config declares no volume, so with no -v its data now lives in the writable layer and is deleted with the container.
Why is docker commit a poor way to save a container's state?
It freezes everything in the writable layer, including logs and temporary files, into an image nobody can rebuild or review. Data belongs in a volume and changes to the software belong in the Dockerfile.
How can you find out what a running container has written to its own filesystem?
docker diff <container> lists the paths it added, changed or deleted relative to the image.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.