Last active
July 19, 2016 13:07
-
-
Save jakzal/f1c0a54c11bfb869162ceef015b8d5c1 to your computer and use it in GitHub Desktop.
Run an aliased or a regular command on a docker container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -u | |
set -o pipefail | |
CONTAINER_OR_ALIAS=${1:-""} | |
COMMAND=${2:-""} | |
ARGUMENTS="${@:3}" | |
ALIASES=( | |
"build:docker build -t jakzal/php:1.0 ." | |
"run:docker_run" | |
"bash:docker_run bash" | |
"composer:docker_run composer" | |
"phpunit:docker_run ./vendor/bin/phpunit" | |
"behat:docker_run ./vendor/bin/behat" | |
"web:docker exec -it web" | |
"db:docker exec -it db" | |
"deploy:docker-compose -f var/docker/deploy/docker-compose.yml run --rm deploy" | |
) | |
function docker_run() { | |
docker run -it --rm -v $(pwd):/project -v $HOME/.composer/cache:/root/.composer/cache -w=/project -p 8000:8000 jakzal/php:1.0 "$@" | |
} | |
function tests() { | |
docker_run bash -c './vendor/bin/phpunit && ./vendor/bin/behat --format=progress' | |
} | |
function help() { | |
echo -n "Usage: $0 " | |
{ printf "%s\n" "${ALIASES[@]}" | sed -e 's/\([^:]*\):.*/\1/g'; compgen -A function; } | tr "\\n" "|" | sed 's/|$//' | |
} | |
if [ -z "$CONTAINER_OR_ALIAS" ]; then | |
help | |
exit 1 | |
fi | |
for alias in "${ALIASES[@]}" ; do | |
KEY=${alias%%:*} | |
VALUE=${alias#*:} | |
if [[ "$CONTAINER_OR_ALIAS" == "$KEY" ]]; then | |
DOCKER_COMMAND="$VALUE" | |
fi | |
done | |
if [[ -z ${DOCKER_COMMAND+x} && "$(type -t $CONTAINER_OR_ALIAS)" == "function" ]]; then | |
DOCKER_COMMAND=$CONTAINER_OR_ALIAS | |
fi | |
if [ -z ${DOCKER_COMMAND+x} ]; then | |
help | |
exit 1 | |
fi | |
$DOCKER_COMMAND $COMMAND $ARGUMENTS | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples: