How to clean old OCI/Docker images

To save disk space, use these example scripts for podman and Docker to remove outdated OCI/Docker images from your servers.

Why doing it?

I utilize Drone Agent for building Docker images, which results in an accumulation of numerous legacy images.

1 image = space disk used for nothing

For example, I build big images:

quay.io/sycured/latex-builder   2.69 GB
quay.io/sycured/buildah-vfs     474 MB

When I rebuild latex-builder, it’s 2.69 GB, so when you have 5 or 6 old images, you lose more than 10 GB of disk space, and my VM for Drone Agent is just 50 GB of disk space.

Cleaning it is required but also in prod because you lose a lot of space disk for nothing.

KISS is the best: cron

Keep It Simple Stupid, cron is the best solution to do it.

Insert the appropriate snippet into /etc/cron.daily/clean_old_images and apply executable permissions using chmod +x /etc/cron.daily/clean_old_images. This approach is ideal if operating as the root user. Otherwise, you can add it to the user crontab.

For Podman

#!/bin/bash
podman images | awk '/none/ {system("podman rmi -f " $3)}'

For Docker

#!/bin/bash
docker images | awk '/none/ {system("docker rmi -f " $3)}'

Tags: