What to do when /Var/Lib/Docker/Overlay2 gets bloated in Docker

Introduction

I ran out of storage on my docker build server. I need to delete unnecessary files, but there is a command to safely allocate space.

What to do first

grasp of the situation

Identify what is putting pressure on the disk in docker.

Use docker system df command, you can identify the disk usage related to docker.

# docker system df
TYPE                    TOTAL      ACTIVE     SIZE       RECLAIMABLE
Images               9              1              2.014GB   1.962GB (97%)
Containers           2              0              0B              0B
Local Volumes        4              2              824.6MB   781.2MB (94%)
Build Cache          0              0              0B              0B

cope

It will remove stopped containers, unused volumes, unused networks, and unused images.

docker system prune

However, the option to delete unused volumes and images is required.

# docker system prune --help

Usage:	docker system prune [OPTIONS]

Remove unused data

Options:
  -a, --all             Remove all unused images not just dangling ones
      --filter filter   Provide filter values (e.g. 'label=<key>=<value>')
  -f, --force           Do not prompt for confirmation
      --volumes         Prune volumes

Delete Volumes

I was able to reduce the size of the file by 355.4MB.

553d15cd6b78ad0f21788a22e9ce16a7295c4bab97609973
deleted: sha256:7ae9338aed73a0f33568db53740431038d3a1f779c4dae40d27433984e1cd97c
deleted: sha256:b1be54c8cadff1e50b87b93559320a1ae57b8d0dd326507148f7ca81d707beed
deleted: sha256:86d78f10b9718618eaae056f5dfa1edae518949aee4578e4147268e9db2e75f0

Total reclaimed space: 355.4MB

However, this does not remove the storage.

use –volumes optiion. you can also delete storage.

docker system prune --volumes
WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

Deleting Images

If you choose –all option. you can also delete images.

# docker system prune --all
WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all images without at least one container associated to them
        - all build cache
Are you sure you want to continue? [y/N]

After work

When you finish deleting unnecessary files, it will be zero.

# docker system df
TYPE    TOTAL   ACTIVE  SIZE    RECLAIMABLE
Images  0    0    0B   0B
Containers    0    0    0B   0B
Local Volumes 0    0    0B   0B
Build Cache   0    0    0B   0B