Docker Community Forums

Share and learn in the Docker community.

Control Disk use of /var/lib/docker/overlay2

What is the best way to maintain disk usage by Docker? Namely, how do you prevent var/lib/docker/overlay2 from endlessly filling up? I’ve seen plenty of insights on this, but no definitive answer. I am using Docker 20.10.2 on Ubuntu 20.04.1 running 2 containers: a web server (Nginx + Gunicorn + Django) and a postgreSQL Database. NOTE: I am also using Docker with Dokku here.

I have:

  • List item

used docker system prune and pruned images and containers separately as well (with each combination of flags). These reclaimed minimal space.

  • tried to manually delete some of the directories in overlay2 (broke Docker installation)
  • uninstalled, reinstalled Docker and rebuilt my containers with new images, and overlay2 started to balloon again immediately.

When I see what the largest files are inside one of these /<hash/ directories:

It looks like the directory contains images, yet the docker image commands weren’t helping me reclaim anything.

How can I safely reclaim overlay2 right now? and Is there any way to prevent this issue from arising in the future?

The steps below outline how to configure the overlay2 storage driver. If you need to use the legacy overlay driver, specify it instead.

Stop Docker.

$ sudo systemctl stop docker
Copy the contents of /var/lib/docker to a temporary location.

$ cp -au /var/lib/docker /var/lib/docker.bk
If you want to use a separate backing filesystem from the one used by /var/lib/, format the filesystem and mount it into /var/lib/docker. Make sure add this mount to /etc/fstab to make it permanent.

Edit /etc/docker/daemon.json. If it does not yet exist, create it. Assuming that the file was empty, add the following contents.

{
“storage-driver”: “overlay2”
}
Docker does not start if the daemon.json file contains badly-formed JSON.

Start Docker.

$ sudo systemctl start docker
Verify that the daemon is using the overlay2 storage driver. Use the docker info command and look for Storage Driver and Backing filesystem.

$ docker info

Containers: 0
Images: 0
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true

Docker is now using the overlay2 storage driver and has automatically created the overlay mount with the required lowerdir, upperdir, merged, and workdir constructs.

Continue reading for details about how OverlayFS works within your Docker containers, as well as performance advice and information about limitations of its compatibility with different backing filesystems.