Overview

This Bash script prunes unused Docker images that have no associated containers. It uses the -a flag to remove all unused images (not just dangling ones) and -f to skip confirmation.

Script

#!/bin/bash

# Prune unused Docker images
docker image prune -af --filter "until=24h"

# Indicate completion of image cleanup
echo "Docker image prune complete."

Usage

  1. Save the script to /usr/local/bin/cleanup_unused_docker_images.sh.
  2. Make it executable:
    sudo chmod 700 /usr/local/bin/cleanup_unused_docker_images.sh
    sudo chown root:root /usr/local/bin/cleanup_unused_docker_images.sh
    
  3. Create a symbolic link in /etc/cron.daily to run the script daily:
     sudo ln -s /usr/local/bin/cleanup_unused_docker_images.sh /etc/cron.daily/cleanup_unused_docker_images