Created
June 10, 2014 21:24
-
-
Save lamdor/68f95250ddd58f584dc9 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/sh | |
# Cleanup docker files: untagged containers and images. | |
# | |
# Use `docker-cleanup -n` for a dry run to see what would be deleted. | |
# adapted from https://github.com/blueyed/dotfiles/blob/master/usr/bin/docker-cleanup | |
# NOTE: i had to change the xargs | |
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 | tail -n +2 | 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 docker rm --volumes=true | |
# Remove untagged images | |
echo "Removing images:" >&2 | |
untagged_images 3 | xargs docker rmi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment