Created
February 13, 2019 13:56
-
-
Save abicky/d44aeaf3dee9b8116867b757d1dc4f41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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