How to clean old OCI/Docker images

disk usage graph image

To save disk space, you need to clean old OCI/Docker images for your servers (prod and also CI/CD). In this example, you have a script for podman and docker.

Why doing it?

I use Drone Agent to build images so I can have a lot of old images.

1 image = space disk used for nothing

For example, I build big images:   2.69 GB     474 MB

When I rebuild latex-builder, it’s 2.69 GB, 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.

Just put the right code to /etc/cron.daily/clean_old_images and chmod +x /etc/cron.daily/clean_old_images (perfect if for root user otherwise in user crontab)

For Podman

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

For Docker

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