Managing images and disk space

Docker · Essentials · 7 min

Images and build cache quietly eat tens of gigabytes; knowing how to clean up keeps machines healthy.

Every image you pull or build stays on disk until you remove it, and so do stopped containers, unused volumes and the build cache.

Inspecting images:

  • docker image ls (or docker images): repository, tag, ID, size.
  • docker image ls --filter dangling=true: dangling images, untagged leftovers (<none>:<none>) from rebuilding a tag.
  • docker image history nginx:1.27: the layers and the instructions that created them.
  • docker tag nginx:1.27 myreg.example.com/nginx:prod: add another name. A tag is just a label; no data is copied.

Removing:

  • docker rmi nginx:1.27 (docker image rm) removes a tag, and the image data once no tags or containers reference it. An image used by a container, even a stopped one, can't be removed without -f.
  • docker image prune removes dangling images; -a removes every image not used by a container.

The big picture: docker system df shows space used by images, containers, local volumes and build cache, and how much is reclaimable. docker system prune removes stopped containers, unused networks, dangling images and build cache. Add -a for all unused images, and --volumes to include unused volumes. Be careful with that last one: volumes hold data such as your local database.

docker builder prune clears just the build cache, often the largest item on a developer machine.

Example

docker image ls
docker image history --format '{{.Size}}\t{{.CreatedBy}}' python:3.13-slim | head -5
docker tag myapp:dev myapp:1.0.0       # second name, same image ID

docker system df
# TYPE           TOTAL  ACTIVE  SIZE     RECLAIMABLE
# Images         41     6       18.2GB   15.9GB (87%)
# Build Cache    312    0       9.4GB    9.4GB

docker image prune                     # dangling images only
docker builder prune --filter until=168h   # build cache older than a week
docker system prune -a                 # everything unused (not volumes)

Remember

  • Tags are labels; one image can have many
  • Dangling images are untagged leftovers from rebuilds
  • docker system df shows where space goes; prune reclaims it
  • Only prune volumes deliberately: they hold data

Watch out for

  • Running docker system prune -a --volumes on a dev box and wiping your local databases.

Check yourself

  1. You rebuilt myapp:dev several times and now see several <none> images. What are they?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.docker.com/reference/cli/docker/system/prune/