Setting up Laravel with Docker : Part 3

“Containers stacked on top of each other.” by frank mckenna on Unsplash

This is part 3 of the series of setting up Laravel with docker post. In part 1, we set up Laravel application using Dockerfile. In part2, we improved on our initial to use docker-compose. Instead of building the container from scratch, we used the official docker containers and put them together to have a working application. If you haven’t read part 2, I strongly suggest you to do so.

If you work in a team and not everyone in the team are familiar with docker, it would be a steep learning curve for the team to use our containers. So, in this post we will improve on what we have done so far and add a small utility script to easily interact with our containers.

Creating the script

#!/bin/bash -e


SRC_DIR=~/laravel
DOCKER="docker-compose run --rm -w /var/www/html app"

function usage {
echo -e "Laravel Docker CLI - Tool to work with laravel docker container.\n"
echo -e "usage: dev [command]"
echo -e " or: dev [command] [arguments]"
echo -e " or: dev [docker_commands] [arguments]\n"
echo "Arguments:"
echo " start Start docker container"
echo " stop Stop docker container"
echo " status Check status of docker container"
echo " ssh SSH into the docker container."
echo " composer Run composer command in the docker container."
echo " php Run php cli in the docker container."
echo " help Display usage"
}

if [ $# -gt 0 ]; then
if [ "$1" == "ssh" ]; then
docker exec -it laravel_app_1 bash
elif [ "$1" == "start" ]; then
docker-compose up -d
elif [ "$1" == "stop" ]; then
docker-compose down
elif [ "$1" == "status" ]; then
docker-compose ps
elif [ "$1" == "php" ] || [ "$1" == "composer" ]; then
$DOCKER "$@"
elif [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage; exit
else
usage; exit
fi
else
usage; exit
fi

Usage

chmod +x container

Now we should be able to execute the script. In your terminal, execute the script by typing:

./container -h

That will output the help for the script. The help message is whatever we are outputting from our usage function.

Laravel Docker CLI - Tool to work with laravel docker container.

usage: dev [command]
or: dev [command] [arguments]
or: dev [docker_commands] [arguments]

Arguments:
start Start docker container
stop Stop docker container
status Check status of docker container
ssh SSH into the docker container.
composer Run composer command in the docker container.
php Run php cli in the docker container.
help Display usage

Now you can interact with your php and composer without having to remember the docker commands.

./container composer install 
./container php -v

Explanation of script

#!/bin/bash -e

We then create some variables that we will be using later in the script.

SRC_DIR=~/laravel 
DOCKER="docker-compose run --rm -w /var/www/html app"

As the name suggests, usage function prints out the usage information of the script. If the user types ./container -h, this function will print this information in the console.

function usage {
echo -e "Laravel Docker CLI - Tool to work with laravel docker container.\n"
echo -e "usage: dev [command]"
echo -e " or: dev [command] [arguments]"
echo -e " or: dev [docker_commands] [arguments]\n"
echo "Arguments:"
echo " start Start docker container"
echo " stop Stop docker container"
echo " status Check status of docker container"
echo " ssh SSH into the docker container."
echo " composer Run composer command in the docker container."
echo " php Run php cli in the docker container."
echo " help Display usage"
}

We then check if the user passed any parameter when invoking the script. If no parameters were passed then usage will be outputted to the terminal.

if [ $# -gt 0 ]; then
...
else
usage; exit;
fi

If any parameters were passed while invoking the script, we check the value of passed parameters. If the parameter passed is ssh then we will invoke the command to ssh them into the container.

if [ "$1" == "ssh" ]; then
docker exec -it laravel_app_1 bash

This is the core of the script and we perform tasks based on the given parameter. If the parameter given is php or composer then we invoke the given command inside the container.

elif [ "$1" == "php" ] || [ "$1" == "composer" ]; then
$DOCKER "$@"

"[email protected]" here means all the parameters that were passed when invoking the container script. Lets say that user types following command in the console.

./container php -v

Above command will be expanded to become following:

docker-compose run --rm -w /var/www/html app php -v

The command will display the version of php installed in the container.

Conclusion

Related posts

Originally published at subash.com.au on January 13, 2018.

Lead Software Engineer at A Cloud Guru

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store