Wrangling in the Ridiculousness


We start a helper script to wrangle all the complexity that Docker brings to our application.

The docker-compose command is still a pain to use because we have so much boilerplate to add.

Let's make a helper script to wrangle it all in.

Helper Script

We'll create a new bash script. I just call it develop:

touch develop
chmod +x develop
vim develop

And we can start here:

#!/usr/bin/env bash

# Set environment variables for dev
export APP_PORT=${APP_PORT:-80}
export DB_PORT=${DB_PORT:-3306}

echo $APP_PORT

We set two environment variables. These can be over-ridden if the variable already exists. If the variable is not yet set, we have a default (port 80 or 3306).

We can test that out:

./develop # echo's 80

APP_PORT=8888 ./develop # echo's 8888

Helping docker-compose

Let's use docker-compose in this helper:

#!/usr/bin/env bash

export APP_PORT=${APP_PORT:-80}
export DB_PORT=${DB_PORT:-3306}

if [ $# -gt 0 ];then
    docker-compose "$@"
else
    docker-compose ps
fi

Now any arguments we pass to the develop command will get passed through to docker-compose, and of course our environment variables will be set with whatever we set (or the defaults).

# Pass-thru to `docker-compose ps`
./develop 

# call `docker-compose ps` ourselves
./develop ps 

# Start our containers
./develop up -d

# Fancier commands - list our the working directory
# from our application container ("app" service)
./develop run --rm -w /var/www/html app ls -lah

This is much better, but we still have a lot of boiler plate for commands like artisan make:controller. So, we'll take this helper script up a notch in the next video.