Skip to content

Instantly share code, notes, and snippets.

@ichaida
Last active August 11, 2016 13:53
Show Gist options
  • Save ichaida/6022de13dacde6642bcc87d49c9f2531 to your computer and use it in GitHub Desktop.
Save ichaida/6022de13dacde6642bcc87d49c9f2531 to your computer and use it in GitHub Desktop.
Build and run dockerize application project
#!/usr/bin/env bash
# Connect to shell to the default machine
eval "$(docker-machine env default)"
# -------------------------------------------------
# Variables
# -------------------------------------------------
# Names to identify images and containers of this app
IMAGE_NAME='NAME'
CONTAINER_NAME="NAME"
PORT=5000
# -------------------------------------------------
# Functions
# -------------------------------------------------
# Output colors
NORMAL='\033[0m'
RED='\033[33;31m'
BLUE='\033[33;36m'
log() {
echo -e "${BLUE} > $1 ${NORMAL}"
}
error() {
echo ""
echo -e "${RED} >>> ERROR - $1 ${NORMAL}"
}
build() {
docker build -t "${IMAGE_NAME}" .
if [[ $? -eq 0 ]]
then
log "Docker image successfully built !" && return 0
else
error "Docker image build failed !" && return 1
fi
}
bash() {
log "BASH"
docker run -it --rm -v $(pwd):/app "${IMAGE_NAME}" /bin/bash
}
stop() {
docker stop "${CONTAINER_NAME}"
}
start() {
docker start "${CONTAINER_NAME}"
}
remove() {
log "Removing previous container ${CONTAINER_NAME}" && \
docker rm -f "${CONTAINER_NAME}" &> /dev/null || true
}
# -------------------------------------------------
# Main
# -------------------------------------------------
log "Building the image: ${IMAGE_NAME}"
build
if [[ $? -eq 0 ]]
then
log "Running the application in production mode..."
docker run -it -d -p ${PORT}:${PORT} "${IMAGE_NAME}"
[ $? != 0 ] && \
error "Failed to run the application!" && exit 1
fi
unamestr=`uname`
if [[ ${unamestr} == "Darwin" ]]; then
# Only for Mac OS, since we get through Virtual Machine to access the LXC module
log "cURL running server..."
curl $(docker-machine ip default):${PORT}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment