Instantly share code, notes, and snippets.

@gkmngrgn /do.sh
Last active Sep 8, 2018

Embed
What would you like to do?
Docker helper script
#!/usr/bin/env bash
BLUE="\\033[1;34m"
GREEN="\\033[1;32m"
NORMAL="\\033[0;39m"
RED="\\033[1;31m"
print_help() {
echo -e "${BLUE}Available environments${NORMAL}"
echo " - DEV (default)"
echo " - PROD"
echo " - STAGING"
echo ""
echo -e "${BLUE}Available commands${NORMAL}"
echo -e " > build \t\t Build or rebuild services"
echo -e " > django [:command] \t Run a django-specific command"
echo -e " > docker [:command] \t Run a docker command"
echo -e " > logs [:service] \t View output from containers"
echo -e " > runserver \t\t Run development server"
echo -e " > shell \t\t Connect to a BASH session from the service inside"
echo -e " > start \t\t Create and start containers"
echo -e " > status \t\t List containers"
echo -e " > stop \t\t Stop services"
}
docker_command() {
command="docker-compose -f docker-compose.yml "
if [ ${ENV} = "PROD" ]
then
file_prefix="prod"
elif [ ${ENV} = "STAGING" ]
then
file_prefix="staging"
else
file_prefix="dev"
fi
command+="-f docker-compose.${file_prefix}.yml ${@}"
echo -e "${BLUE} Command: ${NORMAL}${command}"
echo -e ""
eval ${command}
}
build() {
docker_command build
}
django() {
docker_command run --rm backend python manage.py ${@}
}
docker() {
docker_command ${@}
}
logs() {
docker_command logs -f --tail 50 ${@}
}
runserver() {
docker_command run --rm --service-ports backend
}
shell() {
docker_command run --rm backend bash
}
status() {
docker_command ps
}
start() {
docker_command up -d
}
stop() {
docker_command stop
}
if [ -z ${1} ]
then
print_help
else
case ${ENV} in
"PROD")
env_str="production"
;;
"STAGING")
env_str="staging"
;;
*)
env_str="development"
;;
esac
echo -e "${BLUE}Environment:${NORMAL} ${env_str}"
${@}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment