الانتقال إلى المحتوى
DevOps والـ CloudDocker· 6 دقائق للقراءة

Images vs Containers: Layers, the Writable Layer and Why Your Data Vanished

What a container writes lives in its own thin layer: it survives restarts, never reaches the image and is deleted on every redeploy.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A small internal tool stores uploaded invoices under /srv inside its container. It runs for months without trouble. Then a routine deploy pulls the new image, removes the old container and starts a fresh one, and every uploaded invoice is gone. So is the timeout someone once fixed by running docker exec and editing a config file. Nothing crashed and no command failed: the deploy did exactly what deploys do.

The common misconception

The belief: a container works like a virtual machine's disk. Whatever you change inside it is saved, into the container and from there into its image, so files written at runtime and fixes made with docker exec are permanent.

It's easy to believe because it's half true. Stop a container and start it again, and your files are still there: in our test, a file written inside a container was still readable after docker restart. docker exec feels like SSH-ing into a server, and on a server, changes stay. Nothing in the day-to-day workflow warns you that the container you're editing is disposable.

The half that's missing: those changes belong to that one container. They never reach the image, and they disappear the moment the container is removed.

What actually happens

An image is a read-only stack of layers

Docker's docs state two principles. "Images are immutable. Once an image is created, it can't be modified. You can only make a new image or add changes on top of it." And: "Each layer represents a set of file system changes that add, remove, or modify files." A is those layers stacked into one view of a filesystem. Running a container doesn't add to that stack.

A container adds one thin writable layer

When you create a container, Docker's storage docs say, "you add a new writable layer on top of the underlying layers." This is where "all changes made to the running container, such as writing new files, modifying existing files, and deleting files" go. Image files are shared through copy-on-write: a container reads a file straight from the image layer, and the first time it modifies one, the file is copied into its writable layer and changed there. That's how many containers can share one image while each keeps its own changes.

Removing the container removes the layer

The same docs are blunt: "When the container is deleted, the writable layer is also deleted. The underlying image remains unchanged." Containers get deleted all the time: by docker rm, by --rm when they exit, and by every deploy that replaces an old container with a new one. In our test, a second container started from the same image found /srv empty while the first one still held the file. Stop and restart keep the layer; remove and recreate don't.

Volumes live outside the container

A is the place for data that must last. The docs describe volumes as "persistent data stores for containers, created and managed by Docker", and say that "a volume's contents exist outside the lifecycle of a given container." Mount one at the path where your app writes, and removing the container leaves the data alone.

The fix: put data that matters in a volume

Here's the invoice tool's problem, reduced to Alpine and one file, on Docker 29.4:

Bash terminal
docker run -d --name app alpine:3.22 sleep 3600# bee75fd49ff55c594c6ac0e89c19f3f4725072fee5310dec0c332fef1cfedceedocker exec app sh -c 'echo invoice-1042 > /srv/invoice.txt'docker diff app    # what the writable layer holds# C /srv# A /srv/invoice.txtdocker rm -f app   # what a redeploy does to the old container# appdocker run -d --name app alpine:3.22 sleep 3600# 015ecf32f0e810a62b1bd0e13b6d9b0078c473f23c46ab2354656622f34e43d7docker exec app cat /srv/invoice.txt# cat: can't open '/srv/invoice.txt': No such file or directory

In the first tab, docker diff shows the whole story: /srv changed and invoice.txt was added, both in the writable layer. docker ps -s put that layer at 12.3kB (virtual 9MB); the virtual size adds the image underneath. When the container went, the layer went with it. In the second tab, docker diff printed nothing at all, because the write went to the volume instead of the container, and the new container found the file where the old one left it.

What you doFiles the container wrote
docker stop, then docker start or docker restartKept: same container, same writable layer
docker rm, or the container exits with --rmDeleted along with the writable layer
Deploy a new image versionThe new container starts with an empty writable layer
Start a second container from the same imageIt gets its own layer and can't see the first one's files
Write to a mounted named volumeKept until you delete the volume

How to apply it in your project

  1. Find out what your containers writeRun docker diff on a container that has handled real traffic in staging. Uploads, SQLite files, caches and generated reports all show up there.
  2. Give lasting data a named volumeMount a named volume at each path that must survive a redeploy, or move the data out of the container entirely, to a database or object storage.
  3. Move manual fixes into the imageTreat every docker exec change as an experiment. When it works, write it into the Dockerfile or the app's configuration and rebuild, so the next container starts with it.
  4. Test by recreating, not restartingIn staging, remove the container and start a new one, then check that the data is still there. A restart can't catch this bug, because it keeps the writable layer.

Key takeaways

  • An image is a read-only stack of layers; running a container never changes it.
  • Each container adds one thin writable layer, and every file it writes lands there.
  • That layer survives stop and restart, but it's deleted with the container, on every redeploy.
  • Data that must outlive a container goes in a named volume, outside the container's lifecycle.
  • Fixes made with docker exec are temporary; put them in the Dockerfile or configuration and rebuild.

📚 المصادر

  1. What is an image? — Docker Docs
  2. Understanding the image layers — Docker Docs
  3. Storage drivers — Docker Docs
  4. Volumes — Docker Docs

التعليقات (0)