Sub topic 11
Previewing a prune and avoiding data loss
This chapter assumes you can run docker ps, docker image ls, and docker volume ls. It also assumes you know that a stopped container still holds data on disk. Here you will learn how to see what a prune will delete before you run it, and which prune commands can destroy data that cannot be recovered.
Prune has no dry-run flag
docker container prune, docker image prune, and docker system prune all ask Are you sure you want to continue? [y/N]. The prompt does not list what will be removed. There is also no --dry-run option. To preview, you run the matching list command with the same filter and read the result.
docker image ls --filter dangling=true
docker ps -a --filter status=exited
docker volume ls --filter dangling=trueEach command lists the objects the matching prune would target:
| Prune command | Preview with |
|---|---|
| `docker image prune` | `docker image ls --filter dangling=true` |
| `docker container prune` | `docker ps -a --filter status=exited` |
| `docker volume prune` | `docker volume ls --filter dangling=true` |
A dangling volume is one that no container references, whether the container is running or stopped.
Know which prunes can lose data
Images, build cache, and networks can be recreated. Pull the image again, rebuild the cache, recreate the network. A volume cannot be recreated this way. Database files, uploads, and similar data live in volumes, and a pruned volume is gone for good.
Stopped containers carry a smaller risk. Their writable layer is deleted with them, so anything written inside the container and not stored in a volume is lost.
Docker protects volumes in two ways:
- A plain
docker system prunenever touches volumes. - Volumes are removed only when you ask for it explicitly.
docker volume prune
docker system prune --volumesBoth commands can delete data that no container currently references. A volume used by a stopped container is still referenced, so it is safe. A volume whose container you already removed is not. Recent Docker Engine versions (23.0 and later) make docker volume prune remove only anonymous volumes. Named volumes are removed only if you add --all. Older versions removed every unreferenced volume. Check with docker volume prune --help before you rely on either behavior.
A safe workflow
- Run
docker system dfto see which object type holds the space. Prune only that type. - Preview with the list command and read every line.
- For volumes, run
docker volume ls --filter dangling=trueand check each name. Anonymous volumes have long hexadecimal names. Named ones are easier to identify. - Back up any volume you might need, then prune.
- Run
docker system dfagain to confirm the space came back.
Two habits reduce the risk. First, prefer the narrow command (docker image prune) over docker system prune when you only need one kind of space. Second, avoid -f in interactive sessions, because it skips the one prompt that gives you a chance to stop.
Exercise
A teammate stopped and removed the containers for a Postgres test database last week, but left its named volume. You run docker system prune --volumes -f to reclaim space. What happens to the data, and what should you have done first?
Show answer
The volume is no longer referenced by any container, so the prune can delete it, and -f skips the confirmation. On older Docker versions this removes the named volume too, and the data is lost. First run docker volume ls --filter dangling=true to see the volume, then back it up or confirm it is no longer needed. Use plain docker system prune without --volumes if you only want containers, networks, and images cleaned up.