Sub topic 1
Where Docker Disk Space Goes
Prerequisites
You should know basic Docker commands such as docker run, docker ps, docker images, and docker build. You should also understand the difference between an image and a container.
Docker stores more than running containers. Disk usage commonly comes from:
- Images: Read-only filesystems made from reusable layers.
- Containers: A writable layer is added above an image. Stopped containers still use space.
- Volumes: Persistent data managed outside a container’s writable layer.
- Build cache: Intermediate layers saved to speed up later builds.
- Networks: Usually small, but unused networks accumulate.
- Container logs: The default JSON log file can grow indefinitely.
Measuring Usage with `docker system df`
Start with an inventory instead of deleting objects immediately:
docker system df
docker system df -vThe first command summarizes space used and reclaimable by images, containers, local volumes, and build cache. The -v option lists individual objects, which helps you connect a large total to a specific image, container, or volume.
“Reclaimable” means Docker considers the space unused by the objects currently in use. It does not mean the data is unimportant. A volume may be unused by Docker but still contain a database backup that you need.
Layers, Tags, and Dangling Images
An image consists of layers. Several tagged images can share the same layer, so deleting one tag may not free much space.
A dangling image is an untagged image layer, often left behind after rebuilding an image. An unused image is any image not used by a container. An unused image can still have a tag and may be useful for rollback.
The default image prune removes dangling images only:
docker image pruneTo inspect likely targets first:
docker image ls --filter dangling=trueUse docker image prune -a only when you intend to remove every image that no container uses.
Removing Stopped Containers
Stopped containers retain their writable layers and metadata. List them before removal:
docker ps -a --filter status=exited
docker container pruneThe prune command asks for confirmation and removes all stopped containers. Remove a specific container when you want tighter control:
docker rm CONTAINER_IDExercise
Which command shows reclaimable Docker space, and which prune command removes stopped containers?
Show answer
Use docker system df to inspect reclaimable space and docker container prune to remove stopped containers.