Skip to content

Instantly share code, notes, and snippets.

@miodeqqq
Last active December 9, 2016 23:19
Show Gist options
  • Save miodeqqq/c6b0696e540b86ee2827b96fb0883f42 to your computer and use it in GitHub Desktop.
Save miodeqqq/c6b0696e540b86ee2827b96fb0883f42 to your computer and use it in GitHub Desktop.
Docker - remove all the dangling/unused images.
#!/bin/bash
# Remove all the dangling images
DANGLING_IMAGES=$(docker images -qf "dangling=true")
if [[ -n $DANGLING_IMAGES ]]; then
docker rmi "$DANGLING_IMAGES"
fi
# Get all the images currently in use
USED_IMAGES=($( \
docker ps -a --format '{{.Image}}' | \
sort -u | \
uniq | \
awk -F ':' '$2{print $1":"$2}!$2{print $1":latest"}' \
))
# Get all the images currently available
ALL_IMAGES=($( \
docker images --format '{{.Repository}}:{{.Tag}}' | \
sort -u \
))
# Remove the unused images
for i in "${ALL_IMAGES[@]}"; do
UNUSED=true
for j in "${USED_IMAGES[@]}"; do
if [[ "$i" == "$j" ]]; then
UNUSED=false
fi
done
if [[ "$UNUSED" == true ]]; then
docker rmi "$i"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment