Sub topic 11
Working with many containers and pruning
This chapter assumes you can list, stop, start, and remove a single container, and that you know the difference between an image and a container. It covers how to apply those commands to many containers at once, and how to clean up stopped containers with docker container prune.
Why bulk operations matter
A development machine collects containers quickly. Every docker run without --rm leaves a stopped container behind. Running docker stop or docker rm on each one by hand does not scale, so you combine a listing command with an action command.
The quiet flag: IDs only
The -q (quiet) flag makes docker ps print only container IDs, one per line. Shell command substitution, written $(...), inserts that output as arguments to another command.
docker stop $(docker ps -q)This stops every running container. Without -a, docker ps shows only running containers, so the list contains exactly the containers that can be stopped.
To remove every stopped container the same way, combine -a, -q, and a filter:
docker rm $(docker ps -aq --filter "status=exited")The filter matters. Without it, docker ps -aq also lists running containers, and docker rm refuses to remove those unless you add -f.
Using filters to choose targets
The filters from the earlier chapter work with -q, so you can select a precise group before acting on it. Always run the listing on its own first and read the result.
docker ps -a --filter "status=exited" --format "table {{.Names}}\t{{.Status}}\t{{.Image}}"If the table shows only containers you want to delete, rerun the same filter with -q inside the docker rm command.
Other useful filters include name= (matches part of a name), ancestor= (containers created from a given image), and label=.
docker stop $(docker ps -q --filter "ancestor=nginx:alpine")Pruning stopped containers
docker container prune removes all stopped containers in one step.
docker container pruneDocker asks for confirmation before it deletes anything:
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N]You can narrow the scope with --filter. For example, until removes only containers created before a given time:
docker container prune --filter "until=24h"The -f flag skips the prompt. Use it in scripts, not when typing commands by hand.
The sequence below shows the usual cleanup order.
Comparing the approaches
| Approach | Removes running containers? | Asks for confirmation? | Best for |
|---|---|---|---|
| `docker rm $(docker ps -aq --filter "status=exited")` | No | No | Removing a specific filtered group |
| `docker container prune` | No | Yes | Clearing all stopped containers |
| `docker rm -f $(docker ps -aq)` | Yes | No | Wiping everything, including running containers |
Common errors
- Empty command substitution. If no containers match,
$(docker ps -q)expands to nothing. Thendocker stopprints a usage error such as"docker stop" requires at least 1 argument. This is harmless. Check withdocker psfirst if it surprises you. - Removing too much.
docker rm -f $(docker ps -aq)kills running containers without a grace period and deletes them. Anonymous volumes stay behind unless you add-v. - Assuming prune is selective. Prune removes every stopped container, including ones you meant to start again later. A stopped container that holds data in its writable layer loses that data.
Exercise
You want to stop only the running containers created from the image nginx:alpine, then delete all containers that exited more than a day ago. Which commands do you use?
Show answer
Stop them with docker stop $(docker ps -q --filter "ancestor=nginx:alpine"). Then run docker container prune --filter "until=24h" and confirm with y. Prune only affects stopped containers, so the running ones are safe, and the until filter limits removal to older containers.
Exercise
docker stop $(docker ps -q) prints "docker stop" requires at least 1 argument. What happened, and is anything wrong?
Show answer
No containers were running, so docker ps -q printed nothing and the command received no arguments. Nothing is wrong. Run docker ps to confirm.