Created
October 21, 2017 12:43
-
-
Save neshmi/590408c996e0fa0059bcc406d498c09f to your computer and use it in GitHub Desktop.
File to clean docker images, containers, and stop running containers
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
#!/usr/bin/env bash | |
set -e | |
STOP=0 | |
CONTAINERS=0 | |
IMAGES=0 | |
while getopts ":sci" opt; do | |
case $opt in | |
s) | |
STOP=1 | |
;; | |
c) | |
CONTAINERS=1 | |
;; | |
i) | |
IMAGES=1 | |
;; | |
\?) | |
echo "Invalid option -$OPTARG" >&2 | |
;; | |
esac | |
done | |
# Stop all running containers | |
if [ $STOP -eq 1 ]; then | |
docker kill $(docker ps -q) | |
fi | |
# Delete all containers | |
if [ $CONTAINERS -eq 1 ]; then | |
docker rm $(docker ps -a -q) | |
fi | |
# Delete all images | |
if [ $IMAGES -eq 1 ]; then | |
docker rmi $(docker images -q) | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment