June 15, 2018

The Workflow

We finish building our helper script to finalize our development workflow.

Previous: Dev Workflow Intro

Here we build up our helper script to accomplish the following:

  1. Pass-thru any undefined commands to docker-compose
  2. Run docker-compose ps if we don't pass any arguments to the develop script
  3. 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.

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics