Create a gist now

Instantly share code, notes, and snippets.

@adrian7 /runlocal.sh
Last active Feb 11, 2017

What would you like to do?
runlocal - Build & run local environments (alternative to eb local run)
#!/usr/bin/env bash
###########################################################################################
# runlocal - Build & run a local environment using docker (alternative to eb local run) #
# Also cleans up containers and images built; #
# #
# usage: runlocal <path> [options...] #
# #
# @author: adrian7 <adrian.silimon.eu> #
# @version: 1.1 #
###########################################################################################
#Version
SCRIPTNAME="$(basename $0)"
VERSION="1.1"
#Text formatting
t_underline=$(tput sgr 0 1) # Underline
t_bold=$(tput bold) # Bold
t_red=$(tput setaf 1) # Red
t_green=$(tput setaf 2) # Green
t_blue=$(tput setaf 6) # Blue
t_white=$(tput setaf 7) # White
t_reset=$(tput sgr0) # Reset
usage() {
read -r -d '' HELP << EOM
$SCRIPTNAME version $VERSION
Usage: $SCRIPTNAME <path> [options...]
path Dockerfile folder path; defaults to current folder
optional arguments:
-h, --help displays this help message
-e VAR=VALUE set docker run env variables
-v VOLUME set docker volumes/mounts
-l CONTAINER link docker containers
-p PORT map container ports: host:container
-t TAG tag for the image
-n NAME container name
-w DELAY delay for cleaning up the container and tag; e.g. 45s, 10m, 12h
-f follow container logs (same as docker logs -f)
EOM
echo "$HELP" 1>&2; exit 1;
}
warning() {
printf "%s\n" "${t_bold}${t_red}(!) ${1} ${t_reset}"
}
info() {
printf "%s\n" "${t_bold}${t_blue}(!) ${1}${t_reset}"
}
success() {
printf "%s\n" "${t_bold}${t_green}(!) ${1}${t_reset}"
}
while getopts ":e:v:l:p:t:n:w:f" o; do
case "${o}" in
e)
e="$e -e ${OPTARG}"
;;
v)
v="$v -v ${OPTARG}"
;;
l)
l="$l --link ${OPTARG}"
;;
p)
p="$p -p ${OPTARG}"
;;
t)
t="${OPTARG}"
;;
n)
n="${OPTARG}"
;;
w)
w=${OPTARG}
;;
f)
f="yes"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
#Path to Dockerfile
DOCKERFILE_PATH="$1"
#Tag
TAG="$t"
#Container name
CNAME="$n"
#Timeout (for cleaning up)
WAIT="$w"
#Display logs
FOLLOW_LOGS=1
#Docker env vars
DOCKER_ENV="$e"
#Docker volumes
DOCKER_VOLUMES="$v"
#Linked containers
LINKED_CONTAINERS="$l"
#Mapped ports
MAPPED_PORTS="$p"
if [ "$DOCKERFILE_PATH" == "help" ]; then
#Display help
usage
fi
RAND=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-z0-9' | fold -w 12 | head -n 1)
#Init defaults
if [ -z "$DOCKERFILE_PATH" ]; then
DOCKERFILE_PATH="."
fi
if [ -z "$TAG" ]; then
TAG="local/$RAND"
fi
if [ -z "$CNAME" ]; then
CNAME="test_$RAND"
fi
if [ -z "$WAIT" ]; then
WAIT=3s
fi
if [ -z "$f" ]; then
FOLLOW_LOGS=0
fi
#Startup
STATLINE=$(info "$SCRIPTNAME v$VERSION")
printf "\n%s\n\n" "$STATLINE"
#Build & Run
info "Building image $TAG ...";
docker build -t "$TAG" "$DOCKERFILE_PATH"
info "Launching container $CNAME ...";
docker run -d -P ${DOCKER_ENV} ${DOCKER_VOLUMES} ${LINKED_CONTAINERS} ${MAPPED_PORTS} --name "$CNAME" "$TAG"
#Get container ports
#//TODO display forwarded ports
#IPADDR=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CNAME`
#Display logs
if [ ${FOLLOW_LOGS} = 1 ]; then
info "Following logs... "
docker logs -f "$CNAME"
fi
#Wait timeout
info "Waiting $WAIT before cleaning up ...";
sleep $WAIT
#Cleanup
info "Cleaning up ...";
docker stop "$CNAME"
docker rm "$CNAME"
docker rmi "$TAG"
success "Cleanup completed.";
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment