I got some docker containers running on AWS EC2, the /var/lib/docker/overlay2 folder grows very fast in disk size.
I'm wondering if it is safe to delete its content? or if docker has some kind of command to free up some disk usage.
UPDATE:
I actually tried docker system prune -a
already, which reclaimed 0Kb.
Also my /docker/overlay2 disk size is much larger than the output from docker system df
After reading docker documentation and BMitch's answer, I believe it is a stupid idea to touch this folder and I will try other ways to reclaim my disk space.
I found this worked best for me:
By default Docker will not remove named images, even if they are unused. This command will remove unused images.
Note each layer in an image is a folder inside the
/usr/lib/docker/overlay2/
folder.I used "docker system prune -a" it cleaned all files under volumes and overlay2
also had problems with rapidly growing
overlay2
/var/lib/docker/overlay2
- is a folder where docker store writable layers for your container.docker system prune -a
- may work only if container is stopped and removed.in my i was able to figure out what consumes space by going into
overlay2
and investigating.that folder contains other hash named folders. each of those has several folders including
diff
folder.diff
folder - contains actual difference written by a container with exact folder structure as your container (at least it was in my case - ubuntu 18...)so i've used
du -hsc /var/lib/docker/overlay2/LONGHASHHHHHHH/diff/tmp
to figure out that/tmp
inside of my container is the folder which gets polluted.so as a workaround i've used
-v /tmp/container-data/tmp:/tmp
parameter fordocker run
command to map inner/tmp
folder to host and setup a cron on host to cleanup that folder.cron task was simple:
sudo nano /etc/crontab
*/30 * * * * root rm -rf /tmp/container-data/tmp/*
save and exit
NOTE:
overlay2
is system docker folder, and they may change it structure anytime. Everything above is based on what i saw in there. Had to go in docker folder structure only because system was completely out of space and even wouldn't allow me to ssh into docker container.WARNING: DO NOT USE IN A PRODUCTION SYSTEM
Ok, let's first try system prune
Not so great, seems like it cleaned up a few megabytes. Let's go crazy now:
Nice! Just remember that this is NOT recommended in anything but a throw-away server. At this point Docker's internal database won't be able to find any of these overlays and it may cause unintended consequences.
Docker uses /var/lib/docker to store your images, containers, and local named volumes. Deleting this can result in data loss and possibly stop the engine from running. The overlay2 subdirectory specifically contains the various filesystem layers for images and containers.
To cleanup unused containers and images, see
docker system prune
. There are also options to remove volumes and even tagged images, but they aren't enabled by default due to the possibility of data loss.Backgroud
The blame for the issue can be split between our misconfiguration of container volumes, and a problem with docker leaking (failing to release) temporary data written to these volumes. We should be mapping (either to host folders or other persistent storage claims) all of out container's temporary / logs / scratch folders where our apps write frequently and/or heavily. Docker does not take responsibility for the cleanup of all automatically created so-called EmptyDirs located by default in
/var/lib/docker/overlay2/*/diff/*
. Contents of these "non-persistent" folders should be purged automatically by docker after container is stopped, but apparently are not (they may be even impossible to purge from the host side if the container is still running - and it can be running for months at a time).Workaround
A workaround requires careful manual cleanup, and while already described elsewhere, you still may find some hints from my case study, which I tried to make as instructive and generalizable as possible.
So what happened is the culprit app (in my case
clair-scanner
) managed to write over a few months hundreds of gigs of data to the/diff/tmp
subfolder of docker'soverlay2
So as all those subfolders in
/diff/tmp
were pretty self-explanatory (all were of the formclair-scanner-*
and had obsolete creation dates), I stopped the associated container (docker stop clair
) and carefully removed these obsolete subfolders fromdiff/tmp
, starting prudently with a single (oldest) one, and testing the impact on docker engine (which did require restart [systemctl restart docker
] to reclaim disk space):I reclaimed hundreds of gigs of disk space without the need to re-install docker or purge its entire folders. All running containers did have to be stopped at one point, because docker daemon restart was required to reclaim disk space, so make sure first your failover containers are running correctly on an/other node/s). I wish though that the
docker prune
command could cover the obsolete/diff/tmp
(or even/diff/*
) data as well (via yet another switch).It's a 3-year-old issue now, you can read its rich and colorful history on Docker forums, where a variant aimed at application logs of the above solution was proposed in 2019 and seems to have worked in several setups: https://forums.docker.com/t/some-way-to-clean-up-identify-contents-of-var-lib-docker-overlay/30604