Skip to content

Instantly share code, notes, and snippets.

@jespada
Created May 13, 2014 15:38
Show Gist options
  • Save jespada/5bd1d7a3f1097e3d46b5 to your computer and use it in GitHub Desktop.
Save jespada/5bd1d7a3f1097e3d46b5 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Cleanup docker files: untagged containers and images.
#
# Use `docker-cleanup -n` for a dry run to see what would be deleted.
untagged_containers() {
# Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
docker ps -a | awk '$2 ~ "[0-9a-f]{12}" {print $'$1'}'
}
untagged_images() {
# Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
# NOTE: intermediate images (via -a) seem to only cause
# "Error: Conflict, foobarid wasn't deleted" messages.
# Might be useful sometimes when Docker messed things up?!
# docker images -a | awk '$1 == "<none>" {print $'$1'}'
docker images | awk '$1 == "<none>" {print $'$1'}'
}
# Dry-run.
if [ "$1" = "-n" ]; then
echo "=== Containers with uncommitted images: ==="
untagged_containers 0
echo
echo "=== Uncommitted images: ==="
untagged_images 0
exit
fi
# Remove containers with untagged images.
echo "Removing containers:" >&2
untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
# Remove untagged images
echo "Removing images:" >&2
untagged_images 3 | xargs --no-run-if-empty docker rmi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment