Skip to content

Instantly share code, notes, and snippets.

@kleytonmr
Created July 13, 2024 23:50
Show Gist options
  • Save kleytonmr/63cbb989ab3aeeb79ead470d2cc7f0b0 to your computer and use it in GitHub Desktop.
Save kleytonmr/63cbb989ab3aeeb79ead470d2cc7f0b0 to your computer and use it in GitHub Desktop.
Clean unused images

docker images -a

docker images -f "dangling=true"

docker ps -a --format '{{.Image}}' | sort | uniq -c | sort -nr

docker ps -a --filter ancestor=<image_name_or_id>

docker image prune -a

bin/bash

# List all images
all_images=$(docker images -q)

# List all used images
used_images=$(docker ps -a --format '{{.Image}}' | sort | uniq)

# Check each image
for image in $all_images; do
  count=$(docker ps -a --filter "ancestor=$image" --format '{{.Image}}' | wc -l)
  if [ $count -eq 0 ]; then
    echo "Image $image is not used."
  else
    echo "Image $image is used $count times."
  fi
done

chmod +x check_unused_images.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment