Setting up Laravel with Docker - Part 3
In previous posts of this series we setup Laravel application with docker and docker compose. This this final part of series, we add a utility script to simplify the deployment.
By Subash Adhikari · · 4 min read
Updated . Written in 2018. Commands, prices, and links in this piece may be out of date.

This is part 3 of my series on setting up Laravel with Docker. In part 1, we set up a Laravel application using a Dockerfile. In part 2, we improved on that by using docker-compose. Instead of building the container from scratch, we took the official Docker images and put them together into a working application. If you haven't read part 2 yet, I'd strongly suggest doing that first.
If you work in a team and not everyone is familiar with Docker, there's a steep learning curve to using our containers. So in this post we'll build on what we have and add a small utility script that makes it easy to interact with the containers.
Creating the script#
In the root of your application (~/laravel), create a file called container and paste the following code into it. I'll explain what the script does later in the post.
#!/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
fiUsage#
Make the script executable by running this in your terminal.
chmod +x containerNow you can run it. Try this:
./container -hThat prints the help for the script, which is whatever the usage function outputs.
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 usageNow you can use PHP and Composer without having to remember the Docker commands.
./container composer install
./container php -vExplanation of script#
The shebang on the first line tells the system to run this as a bash script.
#!/bin/bash -eThen we set up a couple of variables that we use later in the script.
SRC_DIR=~/laravel
DOCKER="docker-compose run --rm -w /var/www/html app"As the name suggests, the usage function prints out how to use the script. If you type ./container -h, this is what shows up 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"
}Next we check whether any parameters were passed to the script. If there weren't any, we print the usage to the terminal.
if [ $# -gt 0 ]; then
...
else
usage; exit;
fiIf parameters were passed, we check what they are. If the first one is ssh, we run the command that drops you into a shell inside the container.
if [ "$1" == "ssh" ]; then
docker exec -it laravel_app_1 bashThis is the core of the script. We do different things depending on the parameter. If it's php or composer, we run that command inside the container.
elif [ "$1" == "php" ] || [ "$1" == "composer" ]; then
$DOCKER "$@""$@" here means all the parameters that were passed to the container script. Say you type this in the console:
./container php -vThat gets expanded into:
docker-compose run --rm -w /var/www/html app php -vAnd it prints the version of PHP installed in the container.
Conclusion#
This is a very simple example, but you can take the same idea and use similar scripts to manage much more complex applications. It's especially useful in a team. Not everyone has to know or remember the Docker commands. Just give them scripts that do everything they need inside the container.
Related posts#
Setting up laravel with docker: Part 1
Setting up laravel with docker: Part 2
Related posts
Thanks for reading. You can also follow along on X or with RSS.