Skip to content

Instantly share code, notes, and snippets.

@sjaveed
Created November 1, 2017 14:30
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 sjaveed/f0fe2a26239fcf5d328f19310d0cbf44 to your computer and use it in GitHub Desktop.
Save sjaveed/f0fe2a26239fcf5d328f19310d0cbf44 to your computer and use it in GitHub Desktop.
bash helper functions for docker
###
# Docker-related functions and aliases
###
# Include these into your ~/.bash_profile; don't replace your ~/.bash_profile with this
alias dc="docker-compose"
alias dcr="docker-compose run --rm"
alias dce="docker-compose exec"
function dcrspec() {
spec_file=$1
dcr web bundle exec rspec spec/${spec_file#spec/}
}
function get_cache_prefix() {
full_path_without_dir_separators=${PWD///}
echo ${full_path_without_dir_separators// }
}
function _get_docker_container_names() {
docker-compose config | egrep '^ [a-z]*:' | grep -v '{}' | tee $cache_file
}
function get_docker_container_names() {
cache_file=/tmp/`get_cache_prefix`_get_docker_container_names.cache
# Cache which expires in 24 hours
if [ -f $cache_file ]; then
cache_modification_stamp=`date -r $cache_file +"%s"`
let cache_expiration_stamp=`date +"%s"`-86400
if [ "$cache_expiration_stamp" -ge "$cache_modification_stamp" ]; then
_get_docker_container_names
else
cat $cache_file
fi
else
_get_docker_container_names
fi
}
function is_container_name() {
container=$1
for possible_container in `get_docker_container_names`; do
if [ "$container" == "${possible_container//:}" ]; then
return 0
fi
done
return 1
}
function get_fully_qualified_container_name() {
container=$1
prefix=${PWD##*/}
prefix=${prefix//[^a-zA-Z]/}
echo ${prefix}_${container}
}
function is_container_running() {
container=$1
fully_qualified_container_name=`get_fully_qualified_container_name $container`
running_containers_found=`docker ps --filter name=$fully_qualified_container_name | grep -v 'CONTAINER ID' | wc -l`
if [ "0" == "${running_containers_found// /}" ]; then
# container is not running
return 1
else
# container is running
return 0
fi
}
function get_container_name() {
container=$1
is_container_name $container
if [ "0" == "$?" ]; then
echo $container
return 0
else
echo web
return 1
fi
}
function get_run_type() {
container=$1
is_container_running $container
if [ "0" == "$?" ]; then
echo "exec"
return 0
else
echo "run --rm"
return 1
fi
}
function dcb() {
container=`get_container_name $1`
if [ "0" == "$?" ]; then
shift
fi
run_type=`get_run_type $container`
echo Container: $container
echo Command: bundle $@
echo Run Type: $run_type
docker-compose $run_type $container bundle $@
}
function dcbe() {
container=`get_container_name $1`
if [ "0" == "$?" ]; then
shift
fi
run_type=`get_run_type $container`
echo Container: $container
echo Command: bundle exec $@
echo Run Type: $run_type
docker-compose $run_type $container bundle exec $@
}
function dcrails() {
container=`get_container_name $1`
if [ "0" == "$?" ]; then
shift
fi
run_type=`get_run_type $container`
echo Container: $container
echo Command: bundle exec rails $@
echo Run Type: $run_type
docker-compose $run_type $container bundle exec rails $@
}
function dcrake() {
container=`get_container_name $1`
if [ "0" == "$?" ]; then
shift
fi
run_type=`get_run_type $container`
echo Container: $container
echo Command: bundle exec rake $@
echo Run Type: $run_type
docker-compose $run_type $container bundle exec rake $@
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment