Filtering Images

Difficulty: Intermediate

Question

How do you list only dangling (untagged) images, and how are they different from unused images?

Answer

Run docker images -f dangling=true (the same as docker image ls --filter dangling=true). A dangling image is one with no tag and no repository, shown as <none>:<none>, that no tagged image depends on. They pile up when you rebuild an image with the same tag: the new build takes the tag and the old one is left nameless.

How dangling images appear

Say you build myapp:dev twenty times while developing. Each build produces a new image ID. The tag myapp:dev moves to the newest ID, and the previous one keeps its layers but loses its name. It is no longer reachable by any tag, so it is dangling. Pulling a newer version of a tag (docker pull postgres:16) does the same to the old copy.

Dangling versus unused

TermMeaningRemoved by
DanglingUntagged and not a parent of a tagged imagedocker image prune
UnusedNot used by any container, tagged or notdocker image prune -a
Intermediate (docker images -a)Untagged parents from the legacy builder's layer cacheRemoved with their child image

The distinction matters: docker image prune -a also deletes tagged images like node:20-alpine that no container is using right now, so the next build pulls them again. On a CI runner that is a cache wipe; on a laptop with a slow connection, it is an annoying re-download. Plain docker image prune only targets dangling images and is almost always safe.

With BuildKit (the default builder since Docker Engine 23.0), build steps go to a separate build cache rather than intermediate images, which is cleaned with docker builder prune.

Other useful image filters

  • --filter reference='myorg/api:*': images matching a name or tag pattern.
  • --filter before=myapp:1.0 / --filter since=myapp:1.0: images created before or after another image.
  • --filter label=team=payments: images with a label, useful when many teams share a build host.
  • -q: print only IDs, ready for piping into docker rmi.
  • --format "{{.Repository}}:{{.Tag}} {{.Size}}": custom columns.

Measuring before cleaning

docker system df shows how much space images, containers, volumes and build cache use, and how much is reclaimable. Run it first so you know whether dangling images or the build cache is the real problem. On build servers, a scheduled docker image prune -f --filter "until=72h" keeps disk usage stable without throwing away images that the current day's jobs need.

How an image becomes dangling. Rebuilding the same tag moves the tag and leaves the old image nameless.
  1. Build myapp:dev (image A): Tag myapp:dev points to image A.
  2. Change code, rebuild myapp:dev: A new image B is created.
  3. Tag moves from A to B: myapp:dev now points to B.
  4. Image A shows as <none>:<none>: It is dangling and still uses disk.
  5. docker image prune removes A: Reclaims the space safely.

Code examples

List, count and remove dangling images

# List dangling images
docker images -f dangling=true

# Just the IDs (handy for counting or piping)
docker images -f dangling=true -q | wc -l

# See how much space is reclaimable overall
docker system df

# Remove dangling images (safe)
docker image prune -f

# Only those older than 3 days, for a CI cron job
docker image prune -f --filter "until=72h"

Both rows are older builds of an image whose tag has since moved to a newer build. docker image prune removes them without touching tagged images.

Key points

  • Dangling images are untagged (<none>:<none>) and not parents of tagged images
  • They usually come from rebuilding or re-pulling the same tag
  • docker image prune removes dangling images; -a also removes unused tagged images
  • BuildKit stores build steps in a build cache, cleaned with docker builder prune
  • Use docker system df to see what is actually using disk before pruning

Common follow-up questions

  • What is the difference between docker image prune and docker image prune -a?
  • Why might a CI runner's disk fill up even after pruning images?
  • How does Docker share layers between images?

Interview tips

  • Explain how dangling images are created, not just how to list them
  • Separate dangling from unused clearly, and warn about prune -a deleting base images that act as a cache

Concepts covered

Dangling images, docker images --filter, docker image prune, Image tags, docker system df

More Docker & Containers questions