#How to Check Disk Space Usage for Docker Images & Containers – CloudSavvy IT

Table of Contents
“#How to Check Disk Space Usage for Docker Images & Containers – CloudSavvy IT”

With containerized applications running in isolated environments, it can be harder than usual to track down problems with applications using too much storage space. Fortunately, Docker provides commands for managing container disk usage.
Doing a Quick Check
And if you want to check how much space Docker is using, you can use the built in command docker system df
, as well as the Linux command du
to get the size of the entire directory.
docker system df sudo du -sh /var/lib/docker/
This command shows static images, containers that have made changes to their filesystem (e.g., log files), and volumes bound to the containers.
However, this isn’t entirely accurate—here, I have many containers running, but they’re all storing data in bind mounts on the host OS, not volumes.
Cleaning Docker Images
Docker Images are different than running containers; they’re the files that you download from the Docker Hub to launch a container from. They’re usually pretty big, containing all the necessary code to run a basic OS and your application.
Each version of an image is separate, but it’s stored in layers, so multiple new versions won’t take up twice as much storage space. You can view all images with image ls
:
docker image ls
Cleaning these is easy; you don’t want to remove images of running containers, of course, but removing old images is fine—they’ll simply be re-downloaded when needed.
You can prune all images, or manually delete one by ID:
docker image prune -a docker image rm 3a8d8f76e7f8f
Checking Running Container Usage
Containers are a bit trickier to track down, since they can use data in many different ways:
- Underlying image: each container will need to store its image, but this is reused across containers.
- Modification layer: if a container writes to its filesystem, such as log files, it will be saved in a new layer on top of the underlying image. This is unique to each container.
- Volumes: containers can have virtual drives mounted to them, which store data directly on disk outside the Docker storage system.
- Bind Mounts: containers can optionally access directories on the host directly.
Everything except bind mounts will show up in docker system df
output. If you want to view stats for each container, Docker provides a flag for the ps
command to list the usage:
docker ps --size
Here, this shows the size on disk, as well as the virtual size (which includes the shared underlying image). Since these containers aren’t using any storage outside their bind mounts, the size is zero bytes.
Debugging Mounts (Binds and Volumes)
To view mount usage, for both direct bind mounts and managed volumes, you’ll have to get the size of them from the host OS. If you don’t know where they are, you can rundocker container ls
to get a container’s ID, and then run docker inspect
to grab the mount info:
docker inspect a1c904020044 -f '{{json .Mounts}}' [{"Type":"bind","Source":"/home/daemon-data/921ff235-5075-4d64-b977-8d02cc3dacc9","Destination":"/home/container","Mode":"","RW":true,"Propagation":"rprivate"}]
Then, you can check the total size with du -sh
:
sudo du -sh /path/to/mount/
Pruning Containers And Volumes
Docker never removes containers or volumes (unless you run containers with the --rm
flag), as doing so could lose your data. However, you may have old data backed up that needs to be garbage collected.
Much like images, Docker provides a prune
command for containers and volumes:
docker container prune docker volume prune
Manually Debugging
If you have direct access to the server running Docker, you can pop open a shell in the container:
sudo docker exec -it containerID /bin/bash
and run du -sh
on the entire thing, which will return all data, including the image size, data on bind mounts, and data in volumes.
sudo du -sh /
If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.
For forums sites go to Forum.BuradaBiliyorum.Com
If you want to read more like this article, you can visit our Technology category.