How To Free Up Disk Space From Docker Overlay2
One of my Linux servers which is part of a Docker swarm cluster was running out of space. After doing some troubleshooting, I realized that Docker was using up all the space on the disk, particularly the /var/lib/docker or /var/lib/docker/overlay2 directory.
In this guide, I will show you how I was able to address the issue and reclaim system disk space consumed by unused Docker objects stored in the overlay2 directory, on a Linux server. This guide explains a clean way to remove unused data from Docker to free up space on your Linux system.
Check File system Disk Space Usage
First, I used the following dh command to report the file system disk space usage on the server. From the screenshot that follows, it shows the server was running out of space with the total amount of space being 97G and the total amount of space used being 86G. So the server was only left with 6G of space.
-h flag is used to display disk space size in a human-readable format:
# df -h
Check Disk Space Usage by Files/Directories
I used the following command to show the top five directories using the most space on the server disk:
# du -ahx / sort -rh | head -5
After running the above command, I discovered that the
/var/lib/docker/overlay2) directory by far had the biggest estimated space usage. This prompted me to check the amount of space used by Docker on the server as shown in the next section.
Check Docker Disk Usage
After seeing the high amount of space used by the
/var/lib/docker/overlay2 directory, I also check the Docker disk usage with the following command:
# docker system df
Why does Docker’s Overlay2 Directory/Folder Keep Growing in Size?
Below, I have described some of the key reasons why Docker’s overlay2 directory keeps growing in size:
- Layer accumulation: Docker employs a layered architecture to create images and manage containers. When a new layer is added to a container, it is saved in the overlay2 directory. The accumulation of layers might lead the directory to grow huge over time.
- Cached data: To speed up image build and container startup, Docker stores data in the overlay2 directory as a cache. Files and directories that the container or any processes running inside of it produced can be included in this data. The overlay2 directory may have more cached data if images are being built off the same system and/or when more containers are running on the host.
- Image size: The size of the overlay2 directory is also impacted by the size of the Docker images used to create containers. When containers are constructed from larger images, they will result in larger overlay2 directories.
- Unused layers: When a container is stopped or destroyed, its layers may remain in the overlay2 directory. These unused layers may pile up over time and contribute to the total size of the directory.
In a nutshell, Docker’s overlay2 directory keeps growing in size because of unused data or Docker objects. In the next section, I will show how to remove unused data in Docker to reclaim system disk space.
How To Remove Unused Data in Docker
To remove unused data in Docker, run the following Docker daemon management command. You will be prompted to accept the actions to be executed. So enter y for yes as shown in the following screenshot.
#docker system prune
This screenshot of the command’s output shows deleted containers.
The next screenshot shows deleted networks and images.
This screenshot shows build cached data.
At the end of my operation, I was able to reclaim 45.81GB of space from Docker overlay2, as shown in the following screenshot.
Congratulations! You have just learned how to reclaim system space used by Docker overlay2 or remove Docker’s unused data on a Linux system. Share your thoughts with us via the feedback form below.
Reference about Docker overlay2: https://docs.docker.com/storage/storagedriver/overlayfs-driver/