Skip to content

Instantly share code, notes, and snippets.

@iamsortiz
Created August 21, 2016 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamsortiz/364de6d4d103e4e74b52728392aab193 to your computer and use it in GitHub Desktop.
Save iamsortiz/364de6d4d103e4e74b52728392aab193 to your computer and use it in GitHub Desktop.
Docker tomcat bash utils
IMAGE=tomcat
IMAGE_TAG=8-jre8
CONTAINER_NAME='tomcat'
CONTAINER_APPS_FOLDER='/usr/local/tomcat/webapps/'
function docker-app_container-run() {
echo $PWD
docker run -d -v $PWD/target:$CONTAINER_APPS_FOLDER --name $CONTAINER_NAME -p 8080:8080 $IMAGE:$IMAGE_TAG
}
alias docker-app_container-start="docker start $CONTAINER_NAME"
alias docker-app_container-stop="docker stop $CONTAINER_NAME"
alias docker-app_container-kill="docker rm -f $CONTAINER_NAME"
alias docker-app_container-logs="docker logs -t $CONTAINER_NAME"
alias docker-app_container-logs_live="docker logs -t -f $CONTAINER_NAME"
function docker-app_container-run_safe() {
echo -e "$(echo $COLOR_INFO)docker-app_container-run_safe()$COLOR_DEFAULT"
CONTAINER_ID=$(docker ps -a | grep -E "$IMAGE:.*$CONTAINER_NAME$" | awk '{print $1}')
if [ -z $CONTAINER_ID ];then
echo -e "$COLOR_INFO* Running $CONTAINER_NAME.$COLOR_DEFAULT"
docker-app_container-run
else
echo -e "$COLOR_INFO* Container $CONTAINER_NAME already created$COLOR_DEFAULT"
# Bash prompt from: http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script
read -p $'\e[93mDo you want to \e[91mkill\e[93m the container and start a new one?\e[39m ' yn
case $yn in
[Yy]* ) docker-app_container-kill; docker-app_container-run;;
[Nn]* ) echo "Ok then :D";;
* ) echo "Please answer yes or no.";;
esac
fi
}
function docker-app_container-status() {
echo -e "$(echo $COLOR_INFO)docker-app_container-status()$COLOR_DEFAULT"
CONTAINER_ID=$(docker ps -a | grep -E "$IMAGE:.*$CONTAINER_NAME$" | awk '{print $1}')
if [ -z $CONTAINER_ID ];then
echo -e "$(echo $COLOR_DANGER)* Container $CONTAINER_NAME not created.$COLOR_DEFAULT"
echo -e "$(echo $COLOR_INFO) * To run the container: $ docker-app_container-run $COLOR_DEFAULT"
else
CONTAINER_ID=$(docker ps | grep -E "$IMAGE:.*$CONTAINER_NAME$" | awk '{print $1}')
if [ -z $CONTAINER_ID ];then
echo -e "$(echo $COLOR_WARNING)* Container $CONTAINER_NAME stoped.$COLOR_DEFAULT"
echo -e "$(echo $COLOR_INFO) * To start the container: $ docker-app_container-start $COLOR_DEFAULT"
else
echo -e "$(echo $COLOR_BG_GREEN)* Container $CONTAINER_NAME started.$COLOR_BG_DEFAULT"
echo -e "$(echo $COLOR_INFO) * To stop the container: $ docker-app_container-stop $COLOR_DEFAULT"
echo -e "$(echo $COLOR_INFO) * To kill the container: $ docker-app_container-kill $COLOR_DEFAULT"
fi
fi
}
COLOR_DEFAULT='\e[39m'
COLOR_INFO='\e[34m'
COLOR_WARNING='\e[93m'
COLOR_DANGER='\e[91m'
COLOR_BG_DEFAULT='\e[49m'
COLOR_BG_RED='\e[41m'
COLOR_BG_GREEN='\e[42m'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment