basnotes

 

Reclaiming Unused Docker Space

tip

This morning I noticed my server’s disk space was already used more than 90% and trends shown by my InfluxDB dashboards showed, it would be fully depleted soon if I didn’t do anything about it. So, I used du -hn 1 starting from / to track down where the disk space was used, and it showed Docker was the culprit.

Further analysis pointed me to the /var/snap/docker/common/var-lib-docker/overlay2 directory, so I searched the internet for a way to clean up this directory.

docker system prune

My first hit pointed me to docker system prune, which allows to remove all images and containers not currently in use by active containers. I wanted a bit more information on which data would be removed and also found docker system df to tell me about it:

$ sudo docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              148                 13                  75.48GB             73.11GB (96%)
Containers          15                  14                  43.37GB             259.1kB (0%)
Local Volumes       15                  1                   17.07GB             16.85GB (98%)
Build Cache         0                   0                   0B                  0B

The RECLAIMABLE part looked really good and as I had all containers I need up-and-running, I decided to run docker system prune. Checking docker system df again showed a large improvement:

$ sudo docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              53                  13                  12.76GB             9.689GB (75%)
Containers          15                  15                  43.38GB             0B (0%)
Local Volumes       15                  1                   17.07GB             16.85GB (98%)
Build Cache         0                   0                   0B                  0B

This already reclaimed a lot of disk space, pushing down usage from 90%+ to about 65%!

docker volume prune

As can be seen in the last output from sudo docker system df, there is still ’local volumes’ space that can be reclaimed. Apparently docker system prune does not include that.

So, I used docker volume prune to reclaim space occupied by unused volumes, resulting in:

$ sudo docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              53                  13                  12.76GB             9.689GB (75%)
Containers          15                  12                  43.43GB             549.7kB (0%)
Local Volumes       1                   1                   219.1MB             0B (0%)
Build Cache         0                   0                   0B                  0B

Disk space usage is now pushed down to 58%!

Update 2023-09-27:
docker system prune does not reclaim space for all images. To do that, use docker image prune -a which will also remove all images not related to a container.

sudo docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          168       39        51.18GB   36.39GB (71%)
Containers      40        40        528.5MB   0B (0%)
Local Volumes   49        10        923.4MB   406.8kB (0%)
Build Cache     33        0         0B        0B

After running docker image prune only 2 images have been removed:

sudo docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          166       39        51.09GB   36.3GB (71%)
Containers      40        40        528.5MB   0B (0%)
Local Volumes   49        10        923.4MB   406.8kB (0%)
Build Cache     33        0         0B        0B

After running sudo docker image prune -a only 39 images remain worth ~16GB:

sudo docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          39        39        16.37GB   1.293GB (7%)
Containers      40        40        528.5MB   0B (0%)
Local Volumes   49        10        923.4MB   406.8kB (0%)
Build Cache     33        0         0B        0B

Conclusion

Experimenting with running applications in Docker is really fun, but it leaves behind a lot of mess…

Regularly check your disk space usage using docker system df and clean if needed using docker system prune and docker volume prune.

February 2, 2021