Sub topic 10
What Removal Deletes and What It Keeps
This chapter assumes you can list, stop, start, and remove containers with docker ps, docker stop, docker start, 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 it.
What a container is made of
A container has three parts that matter for removal:
- The image layers. These are read-only and shared by every container created from the same image.
- The writable layer. Docker adds a thin layer on top of the image for each container. Files the container creates or changes at runtime go here.
- Mounted storage. Volumes and bind mounts attach storage that lives outside the writable layer.
docker rm deletes the container's writable layer and its metadata, such as its name, logs, and configuration. It does not delete the image. It does not delete named volumes or bind-mounted host directories.
Seeing the writable layer disappear
Run this sequence to confirm that data written inside a container is lost on removal.
docker run -d --name notes alpine sleep 3600
docker exec notes sh -c 'echo "draft" > /tmp/note.txt'
docker exec notes cat /tmp/note.txt
docker rm -f notes
docker run -d --name notes alpine sleep 3600
docker exec notes cat /tmp/note.txt
docker rm -f notesThe first cat prints draft. The second fails with "No such file or directory". The new container started from the same clean image, with a fresh writable layer.
Stopping alone does not cause this loss. A stopped container keeps its writable layer, so docker start brings the file back. Only removal discards it.
Volumes: named, anonymous, and bind mounts
The three storage types behave differently on removal.
| Storage type | Example | After `docker rm` | After `docker rm -v` |
|---|---|---|---|
| Named volume | `-v appdata:/data` | Kept | Kept |
| Anonymous volume | `-v /data` or a `VOLUME` line in the image | Kept, but orphaned (see below) | Deleted |
| Bind mount | `-v /home/me/site:/usr/share/nginx/html` | Kept | Kept |
An anonymous volume has no name you chose. Docker gives it a long random ID. When you remove the container without -v, the volume stays on disk, and nothing in docker ps tells you it is there. These leftovers are called dangling volumes.
Many images declare a VOLUME in their Dockerfile, so you can create anonymous volumes without asking for them. The mysql and postgres images do this for their data directories.
Removing a container with `-v`
Try both behaviors side by side.
docker run -d --name keep -v /data alpine sleep 3600
docker run -d --name drop -v /data alpine sleep 3600
docker rm -f keep
docker rm -f -v drop
docker volume ls -f dangling=trueThe listing shows one volume, the one that belonged to keep. The volume from drop was deleted along with its container.
Remove the leftover with docker volume rm <volume-id>, using the ID from the listing. To remove all dangling volumes at once, use docker volume prune. Check the list first, because pruning deletes data permanently.
Checking before you remove
To see what storage a container uses, inspect its mounts.
docker inspect --format '{{json .Mounts}}' drop-or-keep-nameEach entry has a Type of volume or bind. For volumes, a Name that is a 64-character hex string means the volume is anonymous. A readable name such as appdata means it is a named volume.
The `--rm` flag and volumes
A container started with docker run --rm is removed automatically when it exits. Docker also removes its anonymous volumes in this case, the same as docker rm -v. Named volumes and bind mounts survive.
Exercise
You run docker run -d --name db -v pgdata:/var/lib/postgresql/data -v /var/lib/postgresql postgres, then docker rm -f -v db. Which of the two volumes survives, and why?
Show answer
The named volume pgdata survives. docker rm -v deletes only anonymous volumes, and /var/lib/postgresql has no name, so it is removed with the container. The named volume stays until you run docker volume rm pgdata.
Exercise
A container wrote a file to /tmp and was then stopped with docker stop. Is the file still there after docker start? What if you had run docker rm instead of docker stop?
Show answer
After docker stop and docker start, the file is still there, because the stopped container keeps its writable layer. After docker rm, the writable layer is deleted, and a new container from the same image will not have the file.