Sub topic 1
Where Docker disk space goes
Docker stores several kinds of objects on disk, and each one grows in its own way. This chapter names those objects and explains why they pile up. Later chapters show how to measure and remove them.
Prerequisites
This chapter assumes you have the following:
- Basic Docker CLI use:
run,ps,images, andbuild. - A clear idea of the difference between an image and a container.
- Comfort running commands in a Linux or macOS terminal.
The five kinds of stored objects
Docker keeps its data in one storage area, usually /var/lib/docker on Linux. Inside it, five kinds of objects take up space.
| Object | What it is | Why it grows |
|---|---|---|
| Images | Read-only templates made of layers | Every pull and every build adds layers. New versions of a tag do not delete the old ones. |
| Containers | Running or stopped instances of an image | A stopped container keeps its writable layer and its logs until you remove it. |
| Volumes | Storage that outlives containers | Data stays after the container is gone. Nothing removes it automatically. |
| Networks | Virtual networks that connect containers | They use almost no disk space, but unused ones clutter the host. |
| Build cache | Saved results of earlier build steps | Each changed build step adds new cache entries. |
Images, containers, volumes, and build cache account for nearly all of the space. Networks are listed because the cleanup commands cover them too.
How the space accumulates
Most growth comes from normal work, not from mistakes. Consider a typical week of development:
- You rebuild an image with the same tag many times. Each build moves the tag to the new image. The previous image loses its tag but stays on disk.
- You run test containers without
--rm. Every one stays in theExitedstate with its writable layer. - A database container writes to a volume. You delete the container, but the volume stays.
- Each build adds cache entries so the next build runs faster. Old entries are not removed when your code changes.
Container logs
A running container also writes logs. With the default json-file log driver, each container's output is stored in a file on the host and has no size limit. A chatty service can fill a disk without creating any new images or volumes. Log rotation is covered in a later chapter.
How the objects relate
The objects depend on each other. This order decides which cleanup works first. An image cannot be removed while a container still uses it, even a stopped one. Removing stopped containers therefore often frees images for removal too.
Looking at the objects yourself
You can list each kind of object with its own command. The commands below show what exists on your machine right now.
docker image ls
docker ps -a
docker volume ls
docker network lsThe next chapter shows how to see how much space each kind takes.
Exercises
Exercise
You rebuild an image named app:latest ten times. You then delete nothing. Which object types now use more disk space, and why?
Show answer
Images use more space because each rebuild leaves the previous image on disk without a tag. Build cache also grows because each changed step adds cache entries. Docker does not remove either automatically.
Exercise
You run docker rm on a database container. Is its data gone? Why or why not?
Show answer
Not necessarily. If the data was stored in a volume, the volume stays on disk after the container is removed. Only removing the volume deletes the data.