How to clean up docker diskspace

Feb. 4, 2024

Jeff Sheffield Jeff Sheffield

TLDR: `docker system df` followed by `docker builder prune`

Sometimes when using docker you end up with some strange errors when attempting to build a container. This can occur when you use up all of the disk space allotted to building containers. This space is called the build cache.

Here is how to identify when that is happening:

docker system df

followed by a

docker builder prune

Or if you have a docker build version before Docker 23

docker buildx prune
docker system df

TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 45 10 18.18GB 15.99GB (87%)
Containers 10 10 994.5kB 0B (0%)
Local Volumes 25 7 1.343GB 1.152GB (85%)
Build Cache 337 0 2.635GB 2.635GB

Here is how to clean up that `Build Cache`.

docker builder prune

Whats the difference between the `docker builder` and `docker buildx`?

As I was working through this problem I became aware of docker buildx and docker builder.

  • docker buildx was introduced as a plugin that extended Docker's build capabilities with BuildKit — multi-platform builds, better caching, multiple builder instances, etc. It was opt-in and separate from the default docker build.
  • docker builder is the newer, integrated alias. Starting with Docker 23.0, BuildKit became the default build backend, and docker builder was added as a top-level command that exposes the same functionality.

In practice:

  • docker buildx pruneand docker builder prune do the same thing — clean the BuildKit build cache
  • docker buildx build and docker builder build are equivalent
  • docker buildx ls and docker builder ls both list builder instances