Sub topic 1
Why container data disappears
Containers are disposable by design. You can stop, remove, and recreate them freely, but anything written inside one is tied to that container's life. This chapter explains where that data lives and why it is lost, which is the reason Docker has volumes.
Prerequisites
This chapter assumes you can:
- Run basic Docker commands:
docker run,docker ps, anddocker rm. - Tell an image from a container.
- Use a Linux shell.
Where a container writes data
An image is a stack of read-only layers. When Docker starts a container, it adds one thin writable layer on top of that stack. This is the container's writable layer. Every file the container creates, changes, or deletes goes into it.
The image itself never changes. Two containers started from the same image each get their own writable layer, so they do not see each other's files.
Seeing the data disappear
The following commands write a file in a container, remove the container, and then start a new one from the same image.
docker run --name scratch alpine sh -c 'echo hello > /data.txt'
docker rm scratch
# The file was in the container's writable layer and is gone with the container.
docker run --rm alpine cat /data.txt
# cat: can't open '/data.txt': No such file or directoryThe first container wrote /data.txt into its writable layer. docker rm deleted that layer along with the container. The second container started from the unchanged image, so the file did not exist.
What survives and what does not
| Action | Writable layer data |
|---|---|
| `docker stop` then `docker start` | Kept |
| Container process crashes and restarts | Kept |
| `docker rm` | Deleted |
| New container from the same image | Starts empty |
| Recreating a container after pulling a new image | Deleted with the old container |
The last row catches many people. Updating a database image means replacing the container. If the data lived in the writable layer, the update erases it.
Why the writable layer is a poor place for important data
- It does not outlive the container. Removal, recreation, and image upgrades all delete it.
- It is hard to reach. The data sits in Docker's internal storage area on the host, managed by a storage driver. You are not meant to read or edit it directly.
- It is slower for heavy writes. Writes go through the storage driver's copy-on-write layer, which adds overhead compared with a native filesystem. Databases are the usual victim.
- It is not shareable. Another container cannot use it.
The fix: store data outside the container
Anything worth keeping must live outside the writable layer. Docker offers three ways to do that:
- Named volumes, which Docker creates and manages for you.
- Bind mounts, which expose a host directory inside the container.
- tmpfs mounts, which keep data in memory only and are meant for temporary data.
The next chapter compares them. In each case, the container sees a normal directory at the mount path, but the data is stored somewhere that is not part of the container's writable layer.
Exercise
You run a Postgres container without any volume, add data, and then run docker rm -f on it and start a new container from the same image. Is your data still there, and why?
Show answer
No. The data was written to the first container's writable layer, which Docker deletes when the container is removed. The new container gets a fresh writable layer on top of the unchanged image. To keep the data, mount a volume at the Postgres data directory.
Exercise
A container is stopped with docker stop, not removed. You then run docker start on it. Does a file it wrote earlier still exist?
Show answer
Yes. Stopping does not delete the writable layer. Only removing the container does.