Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@igolden
Created September 17, 2018 15:40
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 igolden/3d36978a5159dda068499fc82072c1f3 to your computer and use it in GitHub Desktop.
Save igolden/3d36978a5159dda068499fc82072c1f3 to your computer and use it in GitHub Desktop.
docker-compose wrapper for running commands. Drop this in your bin folder
#!/usr/bin/env bash
##
# bin/run
# author: Ian Golden <ian@iangolden.com>
#
#
# Simple bash script to wrap the docker-compose run
# prefix for running commands in a dockerized rails
# setup. prefixes with docker-compose run bundle
# exec $@
#
# @option -a <SERVICE_NAME> specify service name
# @option -e <RAILS_ENV> specify RAILS_ENV
# @option -c clean command no prefix
# @option -h display usage screen
#
#
# bin/run <commands>
# bin/run rake db:migrate
# bin/run rails c
# bin/run -a postgres echo "hi from postgres"
# bin/run -e test -a postgres echo "test + postgres"
#
# Gotcha: -e must preceed -a option
# Gotcha: -c must preceed -a option
#====================================================
set -e
YELLOW='\033[0;33m'
GREEN='\033[1;32m'
ENDC='\033[0m'
SERVICE_NAME=app
__RAILS_ENV=development
function usage {
USAGE=$(cat <<EOF
bin/run
Wrapper command for 'docker-compose run <service>'
Usage:
========================
bin/run rails c
bin/run rake db:migrate
bin/run -c -a app cat /etc/hosts
bin/run -e test -a rails c
bin/run -a postgres echo "hi from postgres"
EOF
)
echo "$USAGE"
}
# Runs w/ prefixes:
# docker-compose run app RAILS_ENV=development bundle exec <commands>
function run_rails {
# did they spec env?
if [ "$1" == "-e" ]; then
shift
# set env
__RAILS_ENV=$1
shift
fi
# did specify service name?
if [ "$1" == "-a" ]; then
# check not null
if [ -n "$2" ]; then
# set service name
SERVICE_NAME=$2
shift && shift
echo -e "\n\n${GREEN}Running${ENDC} ==> ${YELLOW}docker-compose run $SERVICE_NAME RAILS_ENV=${__RAILS_ENV} bundle exec $@${ENDC}\n\n"
docker-compose run $SERVICE_NAME $@
else
usage
fi
elif [ "$1" == "-h" ]; then
usage
elif [ -n "$1" ]; then
echo -e "\n\n${GREEN}Running${ENDC} ==> ${YELLOW}docker-compose run app RAILS_ENV=${__RAILS_ENV} bundle exec $@${ENDC}\n\n"
docker-compose run app $@
else
usage
fi
}
# Runs w/o rails command prefixes:
# docker-compose run app <commands>
function run_clean {
if [ "$1" == "-a" ]; then
# set service name
SERVICE_NAME=$2
shift && shift
echo -e "\n\n${GREEN}Running${ENDC} ==> ${YELLOW}docker-compose run $SERVICE_NAME $@${ENDC}\n\n"
docker-compose run $SERVICE_NAME $@
else
echo -e "You need to specify a specific service when running clean commands"
usage
fi
}
function run {
# if options present continue, else display usage
if [ $1 ]; then
# handle clean run
if [ "$1" == "-c" ]; then
shift
run_clean $@
else
run_rails $@
fi
else
# check usage
usage
fi
}
run $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment