MicroK8S cleanup unused images

Problem: docker on its own does not clear images that were downloaded, and, as a result, sooner or later you will be out of disk space

To clean up unused images you may want to run something like this:

#!/usr/bin/env bash

all_images=$(microk8s ctr images list | awk {'print $1'})
running_images=$(microk8s ctr containers list | awk {'print $2'} | tail -n +1 | sort | uniq)

for image in $all_images
do
        can_be_removed=1
        for running_image in $running_images
        do
                if [ "$image" == "$running_image" ]
                then
                        can_be_removed=0
                        break
                fi
        done
        if [ "$can_be_removed" == "1" ]
        then
                microk8s ctr images remove --sync $image
        fi
done