Sub topic 10
What removal deletes and what it keeps
This chapter assumes you can start, stop, list, and remove containers with docker run, docker stop, docker ps, and docker rm. It also assumes you know an image is a read-only template and a container is a running or stopped instance of one. Here you will learn exactly what docker rm destroys, what it leaves behind, and how to avoid losing data you needed.
The three places a container keeps data
A container's data can live in three places. Removal treats each one differently.
- The image layers. These are read-only and shared by every container created from the same image. Removing a container never touches them.
- The writable layer. Docker adds a thin read-write layer on top of the image layers for each container. Every file the container creates or modifies lands here. This layer belongs to that one container.
- Mounts. These are volumes or host directories attached to a path inside the container. Their data is stored outside the writable layer.
What `docker rm` deletes
Removing a container deletes:
- The container's writable layer, including every file created or changed since it started.
- The container's metadata: its name, ID, configuration, and entry in
docker ps -a. - The container's logs, when the default
json-filelogging driver is used. The log files are stored with the container. - The container's network endpoints. The container no longer appears on any network.
- Anonymous volumes attached to it, but only if you pass
-v(or--volumes).
The name is also released. After removal you can reuse the name for a new container. While a stopped container still exists, docker run --name web ... fails with a name conflict.
What `docker rm` keeps
Removing a container does not delete:
- The image. Run
docker imagesbefore and after. The image is still there. To delete it, usedocker rmi. - Named volumes. A volume you created with
docker volume createor named in-v mydata:/var/lib/datasurvives removal, even with-v. - Bind mounts. A bind mount maps a host directory into the container. The files live on the host. Docker never deletes them when it removes the container.
- Anonymous volumes, unless you ask. Without
-v, they stay on disk with no container referring to them.
The -v flag on docker rm is narrower than it looks. It removes only anonymous volumes. It does not remove named volumes or bind mounts.
Seeing the writable layer disappear
Run this to watch the writable layer being created and destroyed.
docker run -d --name scratch alpine sh -c "echo hello > /note.txt && sleep 3600"
docker exec scratch cat /note.txt
docker diff scratchdocker diff lists files added (A), changed (C), or deleted (D) compared to the image. You should see A /note.txt.
Now stop and restart the container.
docker stop scratch
docker start scratch
docker exec scratch cat /note.txtThe file is still there. A stopped container keeps its writable layer. Stopping is not the same as removing.
Remove it and create a new container with the same name.
docker rm -f scratch
docker run -d --name scratch alpine sleep 3600
docker exec scratch cat /note.txtThe last command fails with No such file or directory. The new container starts from a fresh writable layer on the same image. The old file is gone.
docker rm -f scratchAnonymous volumes in detail
Many images declare a VOLUME instruction in their Dockerfile. When you run a container from such an image without naming a volume, Docker creates an anonymous volume. It gets a random 64-character hexadecimal name.
The official postgres and mysql images do this for their data directories. This is the classic way data gets lost or leaks disk space, depending on how you remove the container.
You can also create an anonymous volume yourself by giving only a container path.
docker run -d --name anon -v /data alpine sleep 3600
docker volume ls
docker inspect anon --format '{{ json .Mounts }}'docker volume ls shows a volume with a long random name. The inspect output shows the same name, a Type of volume, and Destination of /data.
Now compare two removals.
docker rm -f anon
docker volume lsThe volume is still listed. It is orphaned, meaning no container uses it. Docker keeps it because you might want the data.
Repeat with -v:
docker run -d --name anon -v /data alpine sleep 3600
docker rm -f -v anon
docker volume lsThis time the anonymous volume is gone.
Named volumes survive
Named volumes are the safe way to keep data past a container's life.
docker volume create appdata
docker run -d --name app1 -v appdata:/data alpine sh -c "echo saved > /data/state.txt && sleep 3600"
docker rm -f -v app1
docker run --rm -v appdata:/data alpine cat /data/state.txtThe final command prints saved. Even docker rm -v left appdata alone, because it is named. A new container mounted the same volume and read the file.
To delete a named volume you must do it explicitly.
docker volume rm appdataDocker refuses to remove a volume that a container, even a stopped one, still references. Remove the container first.
Bind mounts keep host files
A bind mount connects a host path to a container path.
mkdir -p ./site
echo "<h1>Hi</h1>" > ./site/index.html
docker run -d --name web -v "$(pwd)/site:/usr/share/nginx/html:ro" nginx:alpine
docker rm -f -v web
cat ./site/index.htmlThe file on the host is unchanged. Removing the container only removes the mapping.
The reverse risk is worth knowing. If a container has a bind mount without :ro, it can modify or delete host files while it runs. Those changes persist after the container is removed. Removal does not undo them.
Automatic removal and volumes
docker run --rm removes the container when it exits. It also removes the container's anonymous volumes, as if you had run docker rm -v. Named volumes and bind mounts are kept.
docker run --rm -v /scratch alpine sh -c "echo temp > /scratch/x"
docker volume lsNo orphaned volume appears. This makes --rm a good default for throwaway containers. It is a poor choice for a container whose logs you may need after it exits, because the logs go with it.
Finding and cleaning orphaned volumes
Orphaned anonymous volumes build up when containers are removed without -v. List the unused ones with a filter.
docker volume ls -f dangling=trueA dangling volume is one that no container references. Review the list. Then remove them with:
docker volume pruneDocker asks for confirmation. The command deletes all unused local volumes, and named ones too in recent Docker versions unless you add a filter or use --all semantics differently. Behavior has changed between releases, so read the confirmation prompt before you answer y.
Checking what a removal will affect
Before removing a container, inspect it.
docker inspect web --format '{{ range .Mounts }}{{ .Type }} {{ .Name }} {{ .Source }} -> {{ .Destination }}{{ println }}{{ end }}'Each line shows the mount type, the volume name if any, the host source, and the container path. Use it to decide:
| Mount type | Name | What `docker rm` does | What `docker rm -v` does |
|---|---|---|---|
| volume | long random hex | Orphans the volume | Deletes the volume |
| volume | human-chosen | Keeps the volume | Keeps the volume |
| bind | (empty) | Keeps host files | Keeps host files |
| tmpfs | (empty) | Memory contents discarded | Memory contents discarded |
A tmpfs mount lives in memory and never survives a stop, so it is lost regardless.
Other things that outlive removal
- Images. Removed containers leave their image in place. Over time unused images use disk space. Clean those separately with
docker image prune. - Networks. User-defined networks remain after their containers are removed. Use
docker network prunefor unused ones. - Data already copied out. If you used
docker cpto copy files to the host before removal, that copy is yours to keep.
Saving data before you remove a container
If you realize too late that something in the writable layer matters, you have options only while the container still exists, even when stopped.
docker cp web:/var/log/nginx ./nginx-logs
docker logs web > web.log 2>&1
docker commit web web-snapshot:debugdocker cpcopies files out of a stopped or running container.docker logsprints captured output. Redirect it to a file.docker commitsaves the container's current filesystem as a new image. Use it for debugging, not as a build process, because the result is not reproducible.
Once docker rm has finished, none of these will work.
Common mistakes
- Assuming
docker rm -vis "remove everything". It affects only anonymous volumes. - Leaving data in the writable layer. Application data written to a path with no mount disappears with the container. Mount a named volume at the data path.
- Forgetting
-vand leaking disk. Repeated runs of database images without-vor--rmleave a long random-named volume each time. - Pruning volumes without looking. Always list first.
- Expecting a stopped container to be clean. It still holds its writable layer and name. Only removal frees them.
Exercise
You run docker run -d --name db -v /var/lib/data alpine sleep 3600, then docker rm -f db. A day later you notice disk use has grown. What remains on disk, why, and which single command variant would have prevented it?
Show answer
The container's writable layer and metadata are gone, but the anonymous volume created for /var/lib/data remains as an orphaned volume (visible in docker volume ls), because docker rm without -v keeps anonymous volumes. Running docker rm -f -v db would have deleted it. Starting the container with --rm would also have removed it when the container exited.
Exercise
A container named site has a bind mount of ./html on the host and a named volume uploads. You run docker rm -f -v site. Which of these survive: the image, the writable layer, the files in ./html, the uploads volume?
Show answer
The image, the files in ./html, and the uploads volume survive. The writable layer is deleted. The -v flag affects only anonymous volumes, so it does not touch the named volume or the bind-mounted host directory.
Exercise
You wrote a configuration file at /etc/app/config.ini inside a stopped container with no mounts. Name two commands that can save it, and say when they must be run.
Show answer
docker cp <container>:/etc/app/config.ini . copies the file to the host, and docker commit <container> <image-name> saves the whole filesystem as a new image. Both must be run before docker rm, because removal deletes the writable layer that holds the file.