Skip to content

Instantly share code, notes, and snippets.

@abicky
Created February 13, 2019 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abicky/d44aeaf3dee9b8116867b757d1dc4f41 to your computer and use it in GitHub Desktop.
Save abicky/d44aeaf3dee9b8116867b757d1dc4f41 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eo pipefail
image_id=$1
if [ -z "$image_id" ]; then
echo "Usage: $(basename $0) IMAGE_ID"
exit 1
fi
newer_image_ids=$(docker images -qf since=$image_id | uniq)
dependent_image_ids=$(for i in $newer_image_ids; do
docker history $i | (grep -q $image_id && echo $i) || true
done | sort -u)
echo $dependent_image_ids
for image_id in $dependent_image_ids $image_id; do
echo "Remove containers derived from $image_id and the image"
container_ids=$(docker ps -qaf ancestor=$image_id)
if [[ -n $container_ids ]]; then
echo "Remove containers: $(echo $container_ids | tr -d \n)"
docker rm -v $container_ids
fi
echo "Remove image: $image_id"
docker rmi $image_id
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment