-
-
Save goliatone/1158a69f19f94227060c3a345a334baa to your computer and use it in GitHub Desktop.
docker cleanup
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 | |
| if [ "$(echo $USERNAME)" != "root" ]; then | |
| echo "this script needs to be run by root" | |
| exit 1 | |
| fi | |
| DOCKER_VERSION=$(docker version| grep -m 1 'Version' | perl -pe 's/.* ([1-9]{2})..*/$1/g;') | |
| if [ "$DOCKER_VERSION" -gt "16" ]; then | |
| docker system prune --all --force | |
| else | |
| while $(docker images -a --no-trunc | grep -v IMAGE); do | |
| for image in $(docker images -a --no-trunc | perl -pe 's/\s+ / /g;' | cut -d ' ' -f 3 | grep -v IMAGE); do | |
| docker rmi -f --no-prune $image | |
| done | |
| done | |
| fi |
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 | |
| if [ "$(echo $USERNAME)" != "root" ]; then | |
| echo "this script needs to be run by root" | |
| exit 1 | |
| fi | |
| if | |
| case "${1}" in | |
| 'images') | |
| while $(docker images -all --no-trunc | grep -vE '^[A-Z].*'); do | |
| for image in $(docker images --all --no-trunc | grep -vE '^[A-Z].*' | perl -pe 's/.*sha256:([0-9a-z]{1,})\s+ [0-9]{1}.*/$1/g;'); do | |
| docker rmi --force $image | |
| done | |
| done | |
| ;; | |
| 'containers') | |
| for container in $(docker ps --all --no-trunc --size | grep -vE '^[A-Z].*' | perl -pe 's/^([0-9a-z]{1,})\s+ [0-9]{1}.*/$1/g;'); do | |
| docker rm --volumes --force $container | |
| done | |
| ;; | |
| esac | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment