Created
June 30, 2017 22:36
-
-
Save blotta/2f804c2cc0053313211c8016b38fe18f to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Stops and removes docker containers, along with its related | |
# volumes and custom networks. If no custom networks are being used, | |
# might be better to use "docker rm -v <cont>" instead | |
usage(){ | |
echo -e "\n Usage: $(basename $0) CONTAINER [CONTAINER...]\n" | |
echo -e " Remove one or more containers, along with its assigned volumes and network interfaces\n" | |
exit 1 | |
} | |
inarr(){ | |
v=$1 | |
shift | |
declare -a arr | |
while [ $# -gt 0 ];do | |
arr[${#arr[@]}]=$1 | |
shift | |
done | |
for a in "${arr[@]}"; do | |
if [ "$v" == "$a" ]; then | |
# echo found | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
if [ $# -lt 1 ]; then | |
usage | |
fi | |
declare -a conts | |
while [ $# -gt 0 ]; do | |
conts[${#conts[@]}]=$1 | |
shift | |
done | |
all_conts=($(docker ps -aq --format="{{.ID}} {{.Names}}")) | |
for cont in ${conts[@]}; do | |
if ( ! inarr $cont "${all_conts[@]}" ); then | |
echo "$cont not valid" | |
exit 1 | |
fi | |
done | |
volfmt='{{range $_, $v := .Mounts }} {{($v).Name}}{{end}}' | |
netfmt='{{range $k, $_ := .NetworkSettings.Networks }} {{$k}}{{end}}' | |
runfmt='{{.State.Running}}' | |
echo "Gathering info" | |
uvols=() | |
unets=() | |
for cont in ${conts[@]}; do | |
echo " $cont" | |
running=$(docker inspect --format="$runfmt" $cont) | |
echo " - Running: $running" | |
vols=($(docker inspect --format="$volfmt" $cont)) | |
echo " - Volumes:" | |
for v in ${vols[@]}; do | |
echo " $v" | |
done | |
uvols=($(echo "${uvols[@]} ${vols[@]}" | tr " " "\n"|sort|uniq|tr "\n" " " )) | |
nets=($(docker inspect --format="$netfmt" $cont)) | |
echo " - Networks:" | |
for n in ${nets[@]}; do | |
echo " $n" | |
done | |
unets=($(echo "${unets[@]} ${nets[@]}" | tr " " "\n"|sort|uniq|tr "\n" " " )) | |
done | |
echo | |
echo "Stopping containers..." | |
for cont in ${conts[@]}; do | |
running=$(docker inspect --format="$runfmt" $cont) | |
if [ $running = 'true' ]; then | |
echo " Stopped $cont" | |
docker stop $cont 1>/dev/null | |
else | |
echo " $cont already stopped" | |
fi | |
done | |
echo | |
echo "Removing containers..." | |
for cont in ${conts[@]}; do | |
docker rm $cont 1> /dev/null | |
echo " $cont removed" | |
done | |
echo | |
echo "Removing volumes" | |
for v in ${uvols[@]}; do | |
echo " $v removed" | |
docker volume rm $v 1> /dev/null | |
# docker volume inspect $v | |
done | |
echo | |
echo "Removing networks" | |
for n in $unets; do | |
if ( ! inarr $n 'bridge' 'none' 'host' ); then | |
docker network rm $n 1> /dev/null | |
echo " $n removed" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment