Skip to content

Instantly share code, notes, and snippets.

@neshmi
Created October 21, 2017 12:43
Show Gist options
  • Save neshmi/590408c996e0fa0059bcc406d498c09f to your computer and use it in GitHub Desktop.
Save neshmi/590408c996e0fa0059bcc406d498c09f to your computer and use it in GitHub Desktop.
File to clean docker images, containers, and stop running containers
#!/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