Optimizing Docker Storage
Optimizing Docker Storage
Managing Docker images, containers, and volumes efficiently is crucial to
maintaining disk space and system performance. Over time, unused Docker objects
accumulate, leading to storage bloat and inefficiencies. By default, Docker stores
its files under /var/lib/docker. This guide walks you through the docker prune
commands to clean up unused Docker resources, keeping your environment
optimized.
Optimizing Docker Storage: Clearing /var/lib/docker with Prune
Step-by-Step Guide:
Note: If your user is not a part of a docker group, you have to run all these
commands with sudo.
1. Display disk usage statistics for Docker objects, including images,
containers, volumes, and the build cache
docker system df
[ALT Text: The image displays a terminal window showing the output of the command "docker system df." It presents a table with columns labeled "TYPE," "TOTAL," "ACTIVE," "SIZE," and "RECLAIMABLE." The values include the numbers of images, containers, local volumes, and build cache, along with their respective sizes and reclaimable amounts. A red arrow points to the "RECLAIMABLE" column, indicating the percentage of reclaimable space for images, containers, and volumes.]
space that can be freed up by removing unused Docker objects, such as
images, containers, volumes, or build cache.
2. In order to free up space you can use
Command | What it removes |
docker system prune | Only dangling (unreferenced) images, stopped containers, networks, and cache. |
docker system prune -a | All unused images (even if they are not dangling) along with stopped |
docker builder prune | build cache |
docker image prune | unused images |
docker container prune | stopped containers |
3. Unused volumes can consume considerable storage. To remove them, run:
docker system prune -a --volumes
4. If needed you can run commands with -f flag to prune with “force”
docker system prune -a --volumes -f
5. After pruning, check your system’s disk usage with:
docker system df
[ALT Text: A terminal window displaying the output of the command "docker system df." The output includes a table with columns labeled TYPE, TOTAL, ACTIVE, SIZE, and RECLAIMABLE. The highlighted row shows "Images" with values: TOTAL 59, ACTIVE 59, SIZE 26.76GB, and RECLAIMABLE 2.305GB (8%). Under "Containers," the SIZE is 19.88MB, and under "Local Volumes," it is 193.1MB. The command prompt shows "ubuntu@node1:$" at the bottom.]
Conclusion
Regularly cleaning up unused Docker objects helps maintain a lean and efficient
system. The docker system prune command is a powerful tool for reclaiming
storage but use it carefully to avoid deleting necessary resources.
Disclaimer: This guide provides general recommendations for managing Docker
storage and cleanup. Always review your system requirements and backup
important data before running any cleanup commands.
References:
https://docs.docker.com/reference/cli/docker/system/prune/
https://docs.docker.com/reference/cli/docker/container/prune/
https://docs.docker.com/reference/cli/docker/image/prune/