How to clean old OCI/Docker images

sycuredsycured
1 min read

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:

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, 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

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

For Docker

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

Read articles from sycured directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

sycured
sycured

Designing, building, and running secure infrastructure on public, hybrid, and private clouds.