How to reclaim disk spaces from /var/lib/docker/overlay2/

:small_blue_diamond: What is overlay2?

overlay2 is the default storage driver for Docker on most modern Linux systems (Ubuntu included). It uses the OverlayFS filesystem to efficiently layer files from multiple Docker image layers.


:small_blue_diamond: Purpose of overlay2:

  • Stores image layers, container file systems, and merged views.
  • Enables Docker to share base image layers across containers, saving space.

:small_blue_diamond: Cleanup overlay2:

  • overlay2 can consume a lot of space over time.
  • You can check space usage:
sudo du -sh /var/lib/docker/overlay2
  • You can clean up unused layers/containers:
docker system prune -a

:small_blue_diamond: What docker system prune -a does:

It removes:

  1. All stopped containers
  2. All unused images (not just dangling ones)
  3. All unused networks
  4. All build cache
  5. All unused volumes (if you add --volumes)

:white_check_mark: Safe to run if:

  • You don’t need old containers that were stopped.
  • You don’t need old images not used by any container.
  • You want to reclaim disk space from Docker bloat.
  • You understand that deleted items cannot be recovered unless rebuilt/pulled again.

:warning: Be careful if:

  • You have images or containers you built or pulled that you might use later.
  • You use volumes for persistent data (e.g., databases), and you add --volumes (this will delete your data).
  • You have containers paused, not stopped—they will also be removed.

:small_orange_diamond: Safer alternative:

If you want to clean up without deleting unused images:

docker system prune

(Without -a, it only removes dangling images, not all unused ones.)


:magnifying_glass_tilted_left: Preview before running:

You can preview what will be deleted:

docker system df