Skip to content

Instantly share code, notes, and snippets.

@shamil614
Last active December 4, 2015 17:41
Show Gist options
  • Save shamil614/e83af70cdaed6a7c7078 to your computer and use it in GitHub Desktop.
Save shamil614/e83af70cdaed6a7c7078 to your computer and use it in GitHub Desktop.
Quick and dirty bash scripts to streamline repetitive tasks.
#!/bin/bash
# Instructions
# 1. Download to ~/scripts
# 2. chmod u+x ~/scripts/docker_scripts.sh
# 3. Add the /scripts into your PATH by including this in your ~/.bashrc `export PATH="$PATH:/$HOME/scripts"`
# 4. Open a new terminal and execute `docker_scripts.sh`
# 5. Choose from any of the options at the prompt
function stop_containers() {
id=$(docker ps | awk 'NR > 1 {print $1}' | tr '\n' ' ')
# parse string into array
IFS=' ' read -a ids <<< "${id}"
if [ -z "$ids" ]; then
echo "No containers running"
fi
# iterate and stop
for i in "${ids[@]}"; do
echo "Stopping containers $i"
docker stop "$i"
done
}
function delete_containers() {
docker rm $(docker ps -a -q)
}
function delete_images() {
if [ "$dang" == "true" ]
then
docker rmi -f $(docker images -q --filter "dangling=true")
else
docker rmi -f $(docker images -q)
fi
}
PS3='Select a helper script: '
options=("Start Backend Services" "Stop all containers" "Delete all containers" "Delete dangling images" "Delete all images" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Start Backend Services")
svc=$(docker start redis rabbitmq postgres | tr '\n' ' ')
echo "Starting $svc"
exit
;;
"Stop all containers")
stop_containers
exit
;;
"Delete all containers")
stop_containers
delete_containers
exit
;;
"Delete dangling images")
echo "run dang"
stop_containers
dang="true"
delete_images
exit
;;
"Delete all images")
delete_images
exit
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment