Sub topic 9
Filtering Prunes by Age and Label
This chapter assumes you can run basic Docker commands and know the difference between an image and a container. You should also know the prune commands from the earlier chapters: docker container prune, docker image prune, docker builder prune, and docker network prune. Here you will limit what those commands remove.
Why filter a prune
A plain prune removes every object of its kind that is unused. That includes the image you built ten minutes ago and the container that stopped a minute ago. A filter narrows the target set, so you can remove only old objects or only objects that carry a particular label.
Prune commands accept the --filter option. The two filters that matter most are until and label.
Filtering by age with `until`
The until filter keeps anything created more recently than the given time. It accepts a duration such as 24h or 168h, or a timestamp.
docker image prune -a --filter "until=168h"This removes images not used by any container, but only if they were created more than 168 hours (7 days) ago. The duration 168h is used because Docker durations are written in hours, minutes, and seconds. There is no d unit for days.
The same filter works on the other prune commands:
docker container prune --filter "until=24h"
docker builder prune --filter "until=72h"
docker network prune --filter "until=48h"Each command asks for confirmation before it deletes anything.
Filtering by label
A label is a key and value pair attached to an object. You can add labels when you create it, for example with docker run --label or the LABEL instruction in a Dockerfile.
The label filter has these forms:
| Filter | Matches objects that |
|---|---|
| `label=key` | have the label `key`, with any value |
| `label=key=value` | have the label `key` with exactly that value |
| `label!=key` | do not have the label `key` |
| `label!=key=value` | do not have the label `key` with that value |
Labels let you protect objects or mark them as disposable. For example, label throwaway containers when you start them:
docker run -d --label purpose=scratch nginx
docker container prune --filter "label=purpose=scratch"You can also invert the logic. If you label long-lived objects with keep, this prune skips them:
docker image prune -a --filter "label!=keep"Combining filters
You can pass --filter more than once. Docker removes an object only if it matches all of the filters.
docker image prune -a --filter "until=168h" --filter "label!=keep"This removes unused images older than seven days that are not labeled keep.
Filters and `docker system prune`
docker system prune also accepts --filter. The filter is applied to every object type the command cleans.
docker system prune -f --filter "until=168h"This removes stopped containers, unused networks, dangling images, and unused build cache older than seven days. The -f flag skips the confirmation prompt.
Common errors
- Writing
until=7d. Docker rejects this or fails to parse it. Use168h. - Expecting
untilto protect a volume. Volume pruning does not supportuntil, so uselabelto control volumes. - Forgetting
-aondocker image prune. Without it, only dangling images are considered, so the filter has less to act on. - Trusting a filter without checking. Prune has no dry-run flag, so list the objects first with
docker image ls,docker ps -a, ordocker volume ls, and use the same label filter there.
Exercise
You want to remove unused images older than 14 days, except any image labeled keep. Which command do you run, and why is the number 336 used?
Show answer
docker image prune -a --filter "until=336h" --filter "label!=keep". Docker durations use hours, not days, and 14 days × 24 hours is 336 hours. The two filters are combined, so only images matching both are removed.
Exercise
Why can a freshly pulled image be removed by docker image prune -a --filter "until=24h", assuming no container uses it?
Show answer
The until filter compares against the image's creation time, which is when it was built, not when it was pulled. An image built weeks ago still matches even if you pulled it a minute ago.