Skip to content

Instantly share code, notes, and snippets.

@adamveld12
Last active September 12, 2016 17:35
Show Gist options
  • Save adamveld12/dd7189b30abf05b403a70fa05387772e to your computer and use it in GitHub Desktop.
Save adamveld12/dd7189b30abf05b403a70fa05387772e to your computer and use it in GitHub Desktop.
Docker Machine Bash Shortcuts
# simplifies using docker-machine
function dm() {
local TARGET=$2
if [ -z "$2" ]; then
TARGET="default"
fi
case $1 in
"start" | "up" )
if [ -z $(docker-machine ls | grep $TARGET) ]; then
CREATE=false
read -e -p "a machine named \"${TARGET}\" doesn't exist (yes to create): " CREATE
if [[ $CREATE == "yes" ]]; then
docker-machine create --driver="virtualbox" $TARGET
else
echo "exiting"
return 0
fi
fi
if [ -z $(docker-machine status $TARGET | grep Running) ]; then
docker-machine start $TARGET
fi
eval $(docker-machine env $TARGET) ;;
"stop" | "halt" )
echo "stopping docker machine..."
docker-machine stop $TARGET ;;
"delete" | "destroy" )
echo "destroying \"$TARGET\" machine..."
docker-machine rm $TARGET ;;
"ip" )
docker-machine ip $TARGET ;;
"list" | "ls" )
docker-machine ls ;;
"clean" )
docker ps -a | grep "ago" | awk '{print $1}' | xargs docker rm ;;
"clean-images" )
if [ -z $2 ]; then
$2 = "weeks"
fi
docker images -a | grep "ago" | awk '{print $3}' | xargs docker rmi ;;
"help" | "-h" | "--help" )
echo "usage: dm COMMAND [arg]"
echo "[machine name] is 'default' if not defined"
echo "Commands:"
echo " help | -h | --help Shows this help text"
echo " start | up [machine name] Brings up a docker machine and adds variables to the environment"
echo " stop | halt [machine name] Stops the specified docker machine"
echo " delete | destroy [machine name] Destroys the specified docker machine"
echo " list | ls Lists available machines"
echo " ip [machine name] Prints the ip address for this machine"
echo " clean [text] Deletes all containers matching [text] in the current machine"
echo " clean-images [text] Deletes all images in the current machine"
echo " * Alias for docker-machine"
;;
* )
docker-machine $@
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment