Forked from eduardocardoso/gist:82a629882ddb02ab3677
Last active
March 14, 2017 10:25
-
-
Save avitsidis/2c33371a2ae2d59cd80ac3e5f33c703c to your computer and use it in GitHub Desktop.
Script to delete exited containers and untagged/unused images from docker
This file contains 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 -o errexit | |
echo "Removing exited docker containers..." | |
docker ps -a -f status=exited -q | xargs -r docker rm -v | |
echo "Removing dangling images..." | |
docker images --no-trunc -q -f dangling=true | xargs -r docker rmi | |
echo "Removing unused docker images" | |
images=($(docker images | tail -n +2 | awk '{img_id=$1; if($2!="<none>" && $2!="latest")img_id=img_id":"$2;print img_id}')) | |
containers=($(docker ps -a | tail -n +2 | awk '{print $2}')) | |
containers_reg=" ${containers[*]} " | |
remove=() | |
for item in ${images[@]}; do | |
if [[ ! $containers_reg =~ " $item " ]]; then | |
echo "Mark image $item for deletion" | |
remove+=($item) | |
fi | |
done | |
remove_images=" ${remove[*]} " | |
echo "Remove images marked for deletion" | |
echo ${remove_images} | xargs -r docker rmi | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
since docker 1.13 use
docker system prune