Here we build up our helper script to accomplish the following:
- Pass-thru any undefined commands to
docker-compose
- Run
docker-compose ps
if we don't pass any arguments to thedevelop
script - Create a series of commands such as
artisan
,composer
,yarn
, and so on, setting the script up to allow us to pass in any arguments to those commands.
The commands we create all allow us to run the corresponding commands within our running containers!
#!/usr/bin/env bash
if [ $# -gt 0 ]; then
if [ "$1" == "start" ]; then
docker-compose up -d
elif [ "$1" == "stop" ]; then
docker-compose down
elif [ "$1" == "artisan" ] || [ "$1" == "art" ]; then
shift 1
docker-compose exec \
app \
php artisan "$@"
elif [ "$1" == "composer" ] || [ "$1" == "comp" ]; then
shift 1
docker-compose exec \
app \
composer "$@"
elif [ "$1" == "test" ]; then
shift 1
docker-compose exec \
app \
./vendor/bin/phpunit "$@"
elif [ "$1" == "npm" ]; then
shift 1
docker-compose run --rm \
node \
npm "$@"
elif [ "$1" == "yarn" ]; then
shift 1
docker-compose run --rm \
node \
yarn "$@"
elif [ "$1" == "gulp" ]; then
shift 1
docker-compose run --rm \
node \
./node_modules/.bin/gulp "$@"
else
docker-compose "$@"
fi
else
docker-compose ps
fi
We can use this helper script like so:
# Run docker-compose ps
./develop ps
# Run docker-compose exec app bash
./develop exec app bash
# Run docker-compose exec app php artisan make:controller SomeController
./develop art make:controller SomeController
# Run docker-compose exec app composer require predis/predis
./develop composer require predis/predis
# Run docker-compose exec app ./vendor/bin/phpunit
./develop test
# Run commands against the node container:
./develop yarn ...
./develop npm ...
./develop gulp ...
And of course you can make your own commands, or make it more complex. For example, Laravel Vessel runs exec
if the containers are running and run
if the containers are not running, among other more complex use cases.