Operations 7 min read

How to Reclaim Disk Space Used by Docker: A Complete Guide

This guide explains why Docker consumes large amounts of disk space, breaks down the different types of storage Docker uses, and provides step‑by‑step commands to clean up containers, images, volumes, and build caches so you can free up space efficiently.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Reclaim Disk Space Used by Docker: A Complete Guide

Docker consumes a lot of disk space; running containers, pulling images, deploying applications, and building your own images can quickly fill up your storage.

You can view Docker’s disk usage with the command: $ docker system df The output categorises usage into four types:

Images : space taken by all images, both pulled and locally built.

Containers : space used by the writable layer of each running container.

Local Volumes : space occupied by data volumes mounted by containers.

Build Cache : cache generated during image builds (available with BuildKit in Docker 18.09+).

The RECLAIMABLE column shows how much space can be freed.

Container Disk Usage

When a container is created, Docker creates directories such as /var/lib/docker/containers/ID (logs) and /var/lib/docker/overlay2 (the writable layer).

Starting from a clean system, run an NGINX container and check disk usage: $ docker system df You will see one image (≈126 MB) and one container.

Create a 100 MB empty file inside the container:

$ docker exec -ti www \
  dd if=/dev/zero of=test.img bs=1024 count=0 seek=$[1024*100]

Running docker system df again shows the container’s space has increased; the file resides in the container’s writable layer.

Stopping the container makes that space reclaimable. You can delete stopped containers with: $ docker container prune Or remove all containers (running or stopped) with:

$ docker rm -f $(docker ps -aq)
$ docker container rm -f $(docker container ls -aq)

Image Disk Usage

Some images are “dangling” (no longer referenced) and can be removed. List dangling images: $ docker image ls -f dangling=true Delete them with:

$ docker image rm $(docker image ls -f dangling=true -q)

Or prune all unused images: $ docker image prune To delete every image (except those in use by containers):

$ docker image rm $(docker image ls -q)

Volume Disk Usage

Data volumes store persistent data outside the container’s filesystem. After testing a MongoDB container, the volume remains even after the container is removed.

Remove unused volumes with: $ docker volume rm $(docker volume ls -q) Or prune them:

$ docker volume prune

Build Cache Disk Usage

Docker 18.09 introduced BuildKit, which improves build performance and storage management. Clear the build cache with:

$ docker builder prune

One‑Click Cleanup

All the above resources share the prune sub‑command. Docker also provides a system‑wide prune to clean up any unused space: $ docker system prune Running this command regularly is a good habit to keep your machine tidy.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsLinuxContainer ManagementDisk Cleanup
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.