Sub topic 11
Previewing a prune and avoiding data loss
This chapter assumes you have used docker ps, docker images, and docker system df, and that you know the earlier prune commands (container prune, image prune, builder prune, network prune). It covers how to see what a prune will delete before you run it, and which commands can destroy data you wanted to keep.
Why prune needs a preview step
The prune commands have no dry-run flag. There is no --dry-run option on docker system prune or on any other prune subcommand. What you get instead is a confirmation prompt, and that prompt is less useful than it looks. It prints a generic warning, such as "This will remove all stopped containers", but it does not list the objects it will remove. Answering y is a commitment you make without seeing the list.
The list only appears after the deletion, as IDs plus a "Total reclaimed space" line. By then the objects are gone.
So the preview is manual. For each kind of object, you run the matching ls command with the same filter the prune uses. That shows you roughly what the prune will target.
Preview each object type
Each prune targets one category, and each category has an ls command that lists the same candidates.
# Containers: prune removes every container that is not running
docker ps -a --filter status=exited --filter status=created --filter status=dead
# Images: plain image prune removes dangling images only
docker image ls --filter dangling=true
# Volumes: "dangling" here means not referenced by any container
docker volume ls --filter dangling=true
# Networks: prune removes user-defined networks with no attached containers
docker network ls
# Build cache and the overall size of each category
docker system df -vRepeating a filter, as in --filter status=exited --filter status=created, matches either value.
Three limits apply to these previews:
- Unused images (
prune -a). There is no single filter for "not used by any container". To preview it, rundocker image ls -aand compare it withdocker ps -a --format '{{.Image}}'. Any image that no container, running or stopped, references is a candidate. - Networks.
docker network lsshows everything, including networks that are in use. The built-inbridge,host, andnonenetworks are never removed. For the rest,docker network inspect NAMEshows attached containers. - Timing. A preview is a snapshot. A container can stop or a build can finish between your
lsand your prune, which changes what is unused. Run the preview and the prune close together.
You can bundle the checks into a script and run it before any manual cleanup:
#!/usr/bin/env bash
set -euo pipefail
echo "== Stopped containers =="
docker ps -a --filter status=exited --filter status=created --filter status=dead
echo "== Dangling images =="
docker image ls --filter dangling=true
echo "== Unreferenced volumes =="
docker volume ls --filter dangling=true
echo "== Networks =="
docker network ls
echo "== Disk usage =="
docker system dfApply the same filter to preview and prune
Prune filters (until, label) restrict what gets deleted. The ls commands accept label filters, so you can preview the same selection:
docker volume ls --filter dangling=true --filter label=env=scratch
docker volume prune --filter label=env=scratchThe prune commands also accept a negated label, label!=, which protects objects:
docker container prune --filter "label!=keep"This removes stopped containers that do not carry the keep label. Labels give you a deliberate safety net: label anything you care about when you create it, and prune with label!=keep. Check that the prune subcommand you use supports the filter. Support varies by command, and an unsupported filter produces an error rather than being ignored.
The until filter works on creation time, not on last use. An image built eight days ago and run this morning still matches until=168h, but prune -a skips it if a container uses it. A volume's until filter also reflects when it was created, so an old volume that holds live data is not "stale".
Which commands can lose data
Most prunes remove things you can rebuild: containers from images, images from Dockerfiles or registries, cache from builds. The exception is volume data. A named volume often holds a database or uploaded files, and nothing regenerates it.
| Command | What it removes | Data-loss risk |
|---|---|---|
| `docker container prune` | Stopped containers | Low. Loses the container's writable layer and its logs. Volumes remain. |
| `docker image prune` | Dangling images | Low. Rebuildable. |
| `docker image prune -a` | All images not used by a container | Medium. Images with no registry copy and no Dockerfile are lost. |
| `docker builder prune` | Unused build cache | Low. The next build is slower. |
| `docker network prune` | Unused networks | Low. Networks can be recreated, but fixed IP assignments are lost. |
| `docker volume prune` | Unreferenced volumes | High. Deletes data. |
| `docker system prune` | Stopped containers, unused networks, dangling images, build cache | Low to medium. Volumes are not touched. |
| `docker system prune --volumes` | All of the above plus unreferenced volumes | High. Deletes data. |
A container's writable layer is part of the container. If an application writes files inside a container without a volume, docker container prune deletes those files with the container. Check with docker diff CONTAINER before pruning a container you are not sure about.
Bind mounts (-v /host/path:/data) are never touched by any prune. They are ordinary host directories, not Docker-managed objects.
The stopped-container trap
A volume counts as in use while any container references it, and stopped containers still reference their volumes. A volume prune on its own therefore skips the volume of a stopped database container.
The risk appears when you chain commands. docker system prune --volumes removes stopped containers first, then evaluates volumes. The database container you stopped last week is deleted, its volume becomes unreferenced, and the same command deletes the volume.
docker run -d --name pg -v pgdata:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=example postgres:16
docker stop pg
# The next command deletes the pg container and then the pgdata volume
docker system prune -f --volumesAnonymous versus named volumes
Docker Engine 23.0 and later changed the volume default: docker volume prune removes only anonymous volumes (the ones with a random 64-character name) unless you add -a (--all), which also removes unused named volumes. The same applies to docker system prune --volumes. Older engines remove both kinds. Check with docker version before relying on either behavior, and when in doubt, treat every unreferenced volume as at risk.
Back up a volume before deleting it
When you are unsure whether a volume matters, copy it to a tar archive using a throwaway container:
docker run --rm \
-v pgdata:/data:ro \
-v "$PWD":/backup \
alpine tar czf /backup/pgdata.tgz -C /data .
tar tzf pgdata.tgz | headThe volume is mounted read-only (:ro), the archive lands in your current directory, and --rm removes the helper container when it exits. To restore, create the volume again and extract into it:
docker volume create pgdata
docker run --rm \
-v pgdata:/data \
-v "$PWD":/backup \
alpine tar xzf /backup/pgdata.tgz -C /dataFor a live database, a file-level copy of a running instance can be inconsistent. Stop the container first or use the database's own dump tool.
Safe habits
- Run
docker system dfbefore and after, so you know what each command actually freed. - Run the matching
lscommand first, and read it. - Prefer targeted prunes (
container prune,image prune,builder prune) oversystem prune. The scope is explicit. - Keep
--volumesandvolume prune -aout of scripts and crontabs. Run them by hand, after a preview. - Use
-fonly where no human is present to answer the prompt, and only with commands that cannot delete volumes. - Label important volumes and containers when you create them, and prune with
label!=. - Never rely on a volume you cannot recreate unless it is backed up elsewhere.
Summary of the preview workflow
- Prune has no dry run, and the confirmation prompt does not list objects.
- Preview with
docker ps -a,docker image ls,docker volume ls, anddocker network ls, using the same filters. - Volumes are the only category where a prune destroys data you cannot rebuild.
- Chained commands change what counts as unused: removing stopped containers releases their volumes.
- Back up any volume you are unsure about before pruning it.
Exercise
A Postgres container named db was stopped last week. It stores data in a named volume dbdata. You run docker volume prune on Docker Engine 24, and the volume survives. A colleague then runs docker system prune -f --volumes on the same host. Does dbdata survive, and why? What would you run first to be safe?
Show answer
Probably not on an engine older than 23.0, and the answer on 23.0 and later depends on the volume type. The first command spares the volume because the stopped db container still references it. The second command removes stopped containers first, which deletes db and leaves dbdata unreferenced. On Docker Engine 23.0 or later, --volumes without -a removes only anonymous volumes, so a named volume like dbdata would survive. On older engines it would be deleted. Either way, you should not depend on this. Before pruning, run docker volume ls --filter dangling=true and docker ps -a to see what is referenced, then back up with docker run --rm -v dbdata:/data:ro -v "$PWD":/backup alpine tar czf /backup/dbdata.tgz -C /data ..
Exercise
Why is the confirmation prompt on docker system prune not a reliable preview, and what do you use instead?
Show answer
The prompt prints only a generic warning about the categories of objects to be removed, not the actual objects. Use the matching list commands with the same filters, for example docker ps -a --filter status=exited, docker image ls --filter dangling=true, and docker volume ls --filter dangling=true, then compare against docker system df -v.