
Images vs Containers: Layers, the Writable Layer and Why Your Data Vanished
- كاتب موثّق
محتويات المقال
/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
docker exec are permanent.
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.
What actually happens
An image is a read-only stack of layers
A container adds one thin writable layer
Removing the container removes the layer
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
The fix: put data that matters in a volume
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 directorydocker volume create invoices# invoicesdocker run -d --name app -v invoices:/srv alpine:3.22 sleep 3600# 8d671a15136d29c25384471a3c014ca8712161f2c210faf688558a7f2b922a23docker exec app sh -c 'echo invoice-1042 > /srv/invoice.txt'docker rm -f app# appdocker run -d --name app -v invoices:/srv alpine:3.22 sleep 3600# 4a4d4c313774d830d3841a40f93001f61a7c0334e12fc6e7386c6d98a3565d4bdocker exec app cat /srv/invoice.txt# invoice-1042docker 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.
docker stop, then docker start or docker restart | |
docker rm, or the container exits with --rm | |
How to apply it in your project
Find out what your containers write Run docker diffon a container that has handled real traffic in staging. Uploads, SQLite files, caches and generated reports all show up there.Give lasting data a named volume Mount 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. Move manual fixes into the image Treat every docker execchange 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.Test by recreating, not restarting In 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 execare temporary; put them in the Dockerfile or configuration and rebuild.
التعليقات (0)