Debugging Docker Overlay2 Out of Space
“My docker container is using 80+ GB of disk space! My system is out of disk space! Help!” -a cry for help by someone experiencing a run away container eating up disk space. A common issue with using docker is the /var/lib/docker/overlay2 folder may take up gigabytes of space which will eventually cause an out of disk space issue on the host system. A quick google search reveals pieces of information but trying to debug this issue is difficult because it require understanding how the overlay2 file system works and tracing which container the overlay folder belongs to.
What is overlay2?
A detailed explanation of what the docker overlay2 can be found on docker’s website. The short explanation is it’s one of the choices for the container file system that uses a Union File System. In laymen terms, the /var/lib/docker/overlay2 folder represents a file system that every file that the container read or writes can be found within. Since it is a Union File System, the container does not make direct modifications to any original files so there may be two copies of the same file. The base image (original files) are stored in a “lowerdir”, the modified files are stored in “upperdir”, and finally combination of the two is the “merged” folder. For example, there is a file called foo that is 50mb in the “lowerdir”. The container writes to the file which causes a modified file containing the changes to exist in the “upperdir”.
How to investigate
The commands below are extremely useful to diagnose and narrow down which containers are using a lot of disk space.
du -sh /var/lib/docker/overlay2 | sort 10
Du and sort will show the top 10 folders inside the overlay2 that are using the most space.
docker inspect
docker inspect — format=’{{.GraphDriver.Data}}’
Docker inspect shows which overlays folder the docker image is using. These can be found under the GraphicDriver’s Data section.
docker container diff
The docker container diff shows what files are being written and accessed by a specific container.
The Usual Culprits and How to Fix
A misconfigured Docker container
One of the most common issue is misconfigured docker containers where the container is writing files into the container’s filesystem such as log files or a database’s data files. The correct way for container log files and data files is being written into a docker volume.
In addition, volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container.
A bloated container with lots of layers or a large base image
The next common issue comes from a badly written dockerfile that causes the container to be larger than it needs to be. When loading a docker container, each image layer is loaded into the overlay folder. For example, having a single stage may cause issues such as the container having leftover build artifacts or supporting compilation libraries in the container image. Using a multi-stage dockerfile helps with this problem since there is a separation between the build stage and production stage. The final folder or binary is copied over to the production stage while leaving behind all the unnecessary files, which keeps the container lean.
Leftover containers on the system
Sometimes there are leftover docker images of previously ran docker containers that have not been cleaned up. Running the command below will show how much disk space the containers, volumes, and build cache are using.
docker system df
The unused images can be removed by running the command below, which will remove all data of non-running containers.
docker system prune -a --volumes
A docker volume that is growing rapidly
Lastly, even when everything was configured correctly, there could be a docker container that is writing large amounts of data such as logs or database shards into a docker volume. This is an application specific issue that would need to be resolved by changing the application configuration. Running this command below will show verbose details about the disk space usage of docker.
docker system df -v
After identifying the culprit, if the issue is archival related, it’ll require modifying the application configuration to turn off the logs or put them into an archival server/storage.






