Docker makes application development and deployment easier, but managing disk space can become confusing over time. You may delete old Docker images, remove files from containers, and clean up unused resources, yet your system continues to report high disk usage.
The reason is that Docker stores data in multiple places, and deleting one resource does not automatically remove every piece of data associated with it. Image layers, stopped containers, volumes, build cache, and logs all have different storage lifecycles.
Understanding these differences helps you reclaim disk space without accidentally deleting important application data.
1. Docker Images Use Shared Layers
A Docker image is not a single file. It consists of multiple read-only layers created as an image is built.
For example, an image might contain a base operating system, application dependencies, and application files. Docker can share common layers between images instead of storing duplicate copies.
This means deleting one image does not necessarily remove all its underlying data. If other images or containers still depend on certain layers, Docker retains those layers.
Another important detail is that deleting a file in a later image layer does not necessarily remove the original file data from an earlier layer. The file may disappear from the final filesystem view while its contents remain in the underlying image.
The key point: Image deletion and physical disk-space reclamation are not always the same thing.
2. Deleting Files Inside a Container May Not Free All the Space
Containers use a writable layer on top of their image's read-only layers. Changes made while a container runs, such as creating files or modifying application data, are generally stored in this writable layer.
When a file originally exists in a read-only image layer, deleting it from the container does not modify that original layer. A layered filesystem such as OverlayFS can hide the file using a deletion marker, while the original data remains in the underlying layer.
However, files created only in the writable layer can generally release their storage when deleted, assuming no other references prevent reclamation.
This distinction explains why removing a file from a running container does not always reduce total Docker storage by the file's original size.
If unwanted files are included in your image, update the Dockerfile to avoid adding them and rebuild the image. Docker can then discard obsolete layers when they are no longer needed.
3. Stopped Containers Still Consume Disk Space
Stopping a container does not remove it. Docker retains its writable layer, which may contain application changes, generated files, temporary data, and other artifacts.
List all containers, including stopped ones:
docker ps -a
Remove a specific container you no longer need:
docker rm my-container
To remove all stopped containers, use:
docker container prune
Removing a container also removes its writable layer. Make sure you have saved anything important before deleting it. Data stored in separate volumes has a different lifecycle and may remain after the container is removed.
4. Docker Volumes Keep Data Independently
Docker volumes store data outside a container's writable layer. They are commonly used for databases, uploaded files, and application state that must survive container replacement.
Consequently, deleting a container does not automatically delete its associated named volumes. Database files, for example, can remain on disk even when the database container no longer exists.
Inspect your volumes:
docker volume ls
To remove unused anonymous volumes:
docker volume prune
Docker also supports docker volume prune -a to remove eligible unused named volumes as well as anonymous volumes.
Be careful: An unused volume may still contain valuable database or application data. Check its contents and backup requirements before removing it.
5. Docker Build Cache Can Consume Significant Space
Docker keeps build cache to make repeated image builds faster. Cached intermediate layers can be reused when the relevant build instructions and inputs remain unchanged.
Over time, builds from different projects or code changes can leave behind substantial amounts of unused cache. Deleting the final image does not guarantee that all build cache will disappear.
Inspect the cache usage:
docker builder du
Remove unused build cache:
docker builder prune
For a broader cleanup of eligible build cache, use docker builder prune -a.
Cleaning the cache can free space, although subsequent builds may take longer because Docker needs to rebuild cached layers.
6. Container Logs Can Fill Up the Disk
Applications frequently write logs through standard output and standard error. These logs may include errors, debugging information, and request details.
If an application generates large amounts of output and log rotation is not configured, log files can grow significantly. Removing an image or deleting application files does not necessarily clean up logs associated with existing containers.
The solution is to inspect the container's logging configuration and configure an appropriate logging driver and log rotation policy. For Docker's json-file logging driver, rotation limits how much log data is retained.
Configuration changes may require recreating existing containers to take effect. Avoid manually deleting Docker-managed log files while the daemon is running.
7. Why docker system prune May Not Be Enough
Docker provides a general cleanup command:
docker system prune
It removes eligible unused containers, networks, dangling images, and build cache. However, it does not remove every unused image or volume by default.
For a broader cleanup of unused images and other eligible resources:
docker system prune -a
To include eligible unused anonymous volumes:
docker system prune -a --volumes
Read the confirmation prompt carefully. These commands can remove resources you intended to keep, and volume cleanup carries additional data-loss risk.
Even after pruning, active containers, retained volumes, logs, shared layers, or storage maintained by the underlying operating system can continue consuming space.
8. How to Find What Is Actually Using Disk Space
Instead of repeatedly deleting resources, identify the source of the problem first.
Start with Docker's storage report:
docker system df -v
This provides a more detailed breakdown of images, containers, local volumes, and build cache.
Next, inspect containers and their writable-layer sizes:
docker ps -a
docker ps -s
On Linux, check overall filesystem usage with:
df -h
Docker's default data directory is commonly /var/lib/docker, although the location and storage layout depend on the installation and configuration.
Never manually delete files inside Docker's internal storage directory. Docker manages those files, and modifying them directly can corrupt its storage state.
9. Docker Desktop and Virtual Disk Storage
Docker Desktop on Windows and macOS runs Docker inside a Linux environment. Its data may be stored in a virtual disk image, so the storage reported by Docker and the space reported by the host operating system may not immediately match.
Cleaning unused Docker resources and reclaiming space from the underlying virtual disk are separate operations.
The exact behavior depends on the Docker Desktop version and storage backend. Use the relevant Docker Desktop storage-management guidance rather than manually modifying its internal disk files.
Conclusion
Docker's disk usage depends on more than the images visible in your image list. Shared image layers, stopped containers, persistent volumes, build cache, logs, and the underlying storage system can all contribute to continued disk usage.
The most reliable approach is to inspect first, identify the source, and remove only the resources you no longer need.



