Sub topic 1
Where Docker disk space goes
Docker rarely fills a disk in one step. Each build, run, and pull leaves something behind, and most of it stays until you remove it. Before you delete anything, you need to know what Docker stores and where each kind of object lives. This chapter gives you that map. The later chapters use it to measure usage and clean up each kind of object.
Prerequisites
This chapter assumes you have:
- Basic Docker CLI use:
docker run,docker ps,docker images, anddocker build. - A clear idea of the difference between an image (a read-only template) and a container (a running or stopped instance of one).
- Comfort in a Linux or macOS terminal, including running commands with
sudoon Linux.
Nothing in this chapter deletes data. Every command shown only reads.
The five kinds of objects
Docker stores five kinds of objects, and each grows for a different reason. A cleanup plan that treats them as one pile will either miss most of the space or delete something you needed.
| Object | What it is | Why it grows | Typical size |
|---|---|---|---|
| Images | Read-only stacks of layers, plus tags that point to them | Every `docker pull` and `docker build` adds layers. Old versions stay after you pull new ones | Tens of MB to several GB each |
| Containers | A writable layer on top of an image, plus config and logs | Stopped containers keep their writable layer and log until removed | Usually small, but can be large |
| Volumes | Storage managed by Docker that outlives containers | Databases and uploads write into them. Removing a container does not remove its volume | Often the largest, and the most valuable |
| Build cache | Intermediate results from `docker build` | Every build step that runs is cached for reuse | Can reach tens of GB on busy build machines |
| Networks | Virtual networks and their config | Compose projects and `docker network create` add them | Negligible disk, but they clutter |
Networks matter for tidiness, not for space. Treat the other four as real disk consumers. Container logs are a sixth source of growth that is easy to overlook. They are technically part of a container, and the Container logs chapter covers them in full.
Images and layers
An image is built from layers. Each instruction in a Dockerfile that changes the filesystem (RUN, COPY, ADD) produces one layer. Layers are stored once and shared. If ten images start from the same python:3.12-slim base, that base is on disk once.
This sharing has two consequences:
- The size column in
docker imagesis the virtual size of the whole image. Adding up that column over-counts, because shared layers are counted once per image. - Deleting one image frees only the layers that no other image uses. You may remove a 1 GB image and gain 50 MB.
A tag is a name that points to an image. When you rebuild myapp:latest, the tag moves to the new image. The old image loses its name and shows up as <none>:<none>. That is a dangling image, and it keeps all its layers on disk. Dangling images are one of the most common sources of waste on development machines. A later chapter covers the difference between dangling and unused images.
Containers and their writable layer
When a container starts, Docker adds a thin writable layer on top of the image layers. Everything the container writes to its own filesystem goes there. When the container stops, that layer stays. It is deleted only when the container is removed.
This means a stopped container still uses disk. Common cases:
- Containers started without
--rmand exited long ago. - Containers from CI jobs or one-off commands (
docker run ubuntu ls). - Crashed containers kept for debugging.
A container that writes heavily to its own filesystem, such as temp files or caches, can hold several GB in its writable layer. Data you want to keep belongs in a volume instead.
Volumes
A volume is storage that Docker creates and manages, separate from any one container. Removing a container does not remove its volumes. This is deliberate, because it protects database files. It also means volumes accumulate.
Two kinds are worth telling apart:
- Named volumes, for example
pgdata, created by you or by a Compose file. - Anonymous volumes, which get a long random name. They are created when an image declares a
VOLUMEinstruction, as many database images do, and you run it without naming one.
Anonymous volumes are the main source of orphaned data. Each run of such an image can leave a new volume behind, and nothing in the name tells you what it held.
Bind mounts, such as -v ./data:/data, are different. Docker does not manage them. They are ordinary directories on your host, and Docker commands neither count nor delete them.
Build cache
When you run docker build, Docker saves the result of each step. If a step and everything before it is unchanged, the next build reuses the saved result. This speeds up builds, but the cache is never trimmed on its own schedule by default. Each changed COPY or RUN step adds a new entry, and old entries stay.
Build cache can look like it belongs to images, but it is tracked separately. Deleting images does not clear it. It often grows large on machines that build often, such as CI runners and developer laptops.
Where it lives on disk
On Linux, Docker keeps almost everything under one directory, the Docker root. You can print its location and the storage driver in use:
docker info --format 'root={{.DockerRootDir}} driver={{.Driver}}'On a default Linux install, the output looks like this:
root=/var/lib/docker driver=overlay2The subdirectories map onto the objects above:
sudo du -sh /var/lib/docker/* 2>/dev/null | sort -hThe names and contents depend on your setup, but on an overlay2 install you will typically see:
| Path under the Docker root | Holds |
|---|---|
| `overlay2/` | Image layers and container writable layers |
| `containers/` | Per-container config and log files |
| `volumes/` | Named and anonymous volume data, in `<name>/_data` |
| `buildkit/` | BuildKit cache metadata (the cached layers themselves are in the storage driver's area) |
| `image/` | Metadata about images and layers |
| `network/` | Network state, which is tiny |
Two caveats about this layout:
- Newer Docker releases can use the containerd image store instead of the classic overlay2 layout. Image and layer data then sits in a
containerddirectory under the Docker root. The object types and the cleanup commands stay the same, but the directory names differ. - Running
duonoverlay2over-reports. Mounted containers appear as merged views, so the same bytes can be counted more than once. Usedufor a rough guide only. The next chapter shows the more reliabledocker system df.
Docker Desktop
On macOS and Windows, Docker Desktop runs the engine inside a Linux virtual machine. All of the directories above are inside that VM, and du on your host cannot see them. The VM stores its data in one large disk image file on your host.
That file grows as Docker uses space. It does not always shrink when you delete objects, because the VM may keep the space reserved. If you prune and your host disk usage stays the same, this is the usual reason. Recent Docker Desktop versions reclaim space more readily than older ones, but check your version's behavior before assuming the host will show the gain right away. Pruning still frees space inside the VM, so Docker has room to reuse.
Used, unused, and reclaimable
Docker's cleanup commands work by reference. An object is in use if something still points to it, and unused if nothing does:
- An image is in use if any container, running or stopped, was created from it. Its layers are also in use if another image is built on them.
- A volume is in use if any container, running or stopped, mounts it.
- A network is in use if any container is connected to it.
- A container is unused if it is not running.
This explains behavior that surprises people. Deleting a stopped container can make its image unused. Deleting that image can make its parent layers unused. Order matters: clear containers first, then images. The Image cleanup chapter builds on that point.
Reclaimable space is what Docker could free by removing unused objects. It is an estimate, and it does not tell you whether the data is worth keeping. A volume with a production database that is not currently attached to a container counts as unused and reclaimable.
Quick check of your own machine
Before moving on, take a first look at what you have. These commands only read:
docker image ls
docker ps -a
docker volume ls
docker network ls
docker builder duCompare the counts. A typical neglected machine shows many images with <none> names, many exited containers, and a long list of volumes with random 64-character names. The next chapter replaces this manual survey with one summary command.
Exercise
You delete a 2 GB image that shows in docker images, but your disk gains only 300 MB. Give two reasons this can happen.
Show answer
First, image layers are shared. Only the layers that no other image uses are freed, so most of the 2 GB may belong to layers still referenced by other images. Second, the listed size is the virtual size of the whole image, so it over-counts shared layers. Related causes are that the image's containers or build cache may still hold data, and on Docker Desktop the VM disk file may not shrink on the host.
Exercise
A container named db ran a database image and was removed with docker rm db. Does its data still use disk space? What decides the answer?
Show answer
It depends on where the data was written. If it was in a named or anonymous volume, the volume survives docker rm and still uses space until you remove the volume. If it was only in the container's writable layer, removing the container freed it. If it was in a bind mount, the data is a plain host directory that Docker never deletes.