This guide will walk you through the process of running laravel from Docker containers for our project.
To accomplish our goal we need a total of three resources:
And of course this tutorial will require you to have a working installation of Docker (if compatible with your OS) or Docker Toolbox
Currently, the development branch is located at gast499/faqbot . Download the repo as a zip or clone it into a directory that you feel comfortable working in for the rest of this tutorial.
Note that this project uses Laravel 5.6
The next thing we have to is install Laravel dependencies. To install Laravel dependencies we are going to
make use of the composer
Docker image. This way you don't have to worry about installing composer
on your machine. But if you wish to use an installation of composer on your host machine you can use that
as well.
By creating an alias we can shorten the command to run programs through docker. If you are using Docker Toolbox or Docker on Linux/Mac you can create aliases to make using docker images as applications easier. If you are using Docker on Windows 10 Pro you can accomplish the same thing through powershell see here. Aliases are temporary if they are set in a terminal session, so using a file that contains these aliases can be utilized to save them for the future. See LINK HERE to learn more about saving aliases.
Run the commands in this tutorial using Docker Quickstart Terminal if you are using Docker Toolbox and a plain terminal if you are using Linux/Mac.
Linux/Mac users make sure that the Docker dameon is running before you run any commands using Docker. Docker Toolbox users need to make sure to follow the instructions for linking drives to the boot2docker VM
Alias command to run composer through docker:
alias composer='docker run --rm -v `pwd`:/app composer'
Note that using composer through docker is not necessary if you have it installed on your system
Now that we have an alias for composer
we need to use it to install the dependencies of our project.
Navigate to the directory where you have downloaded the project source code (if you are new to using a
terminal see here) and run the following commands.
If you are running the
composer
command with Docker for the first time it may take a few seconds to download, but after downloaded should run smoothly.
Add Botman
package:
$ composer require botman/installer
Add Predis
package:
$ composer require predis/predis
Install the rest of the packages listed in composer.json with:
$ composer install
Note that this step is not necessary if you have php installed on your system
Before we can run our project we need to change its settings. To accomplish that we use a file named
.env
. In our project source we have a file named .env.example
. We'll use that as a base.
Either make a copy of .env.example
or rename it so that you end up with a file named .env
with
the default values contained in .env.example
Alias for php:
alias php='docker run --rm -v `pwd`:/p -w /p php:7-fpm-alpine php'
Generate APP_KEY:
$ php artisan key:generate
Although we use
artisan
here with a temporary container, in the future we are going to useartisan
through a different container that will be connected to our running project and database.
The rest of the values should be set correctly if you got .env.example
from the project source.
To run the project we are going to make use of Docker Compose to allow us to start our containers for php, nginx, postgres, redis, and elastic-search.
For those using Docker for Windows or Docker Toolbox you should already have the command
docker-compose
. Other operating systems should refer here for instructions to install Compose.
To get started place the docker-compose.yml file attached to this tutorial in your project folder. You also need to place php.dockerfile in that same directory for Compose to work correctly.
Once that is done, you can run:
$ docker-compose up
If you've done everything correctly up to this point, then you should see the console start downloading the images for the containers, extensions needed, and finally the standard output of each container with a label telling you which container the output is comming from.
Docker Compose may take a while, but should start up much faster after it's first run.
To view the project use your browser and enter localhost
in your address bar. You should now see the
Laravel defualt landing page.
If you are using Docker Toolbox however, you will need to type:
docker-machine ip
And enter that ip address in your address bar in place of localhost
If Laravel gives you an error, check that you have correctly setup your .env file.
To stop all the containers from running use the key bind Ctrl-C
. If you want to run docker-compose in
the background (without standard output) use the command:
$ docker-compose up -d
To stop the running containers in the background use this command:
$ docker-compose stop
This alias will be used to run artisan
commands that interface with our database as well as phpunit
:
alias phpcont="docker exec -w /code php_project"
Migrating to database:
$ phpcont php artisan migrate
Seeding database:
$ phpcont php artisan db:seed
Running phpunit:
$ phpcont ./vendor/bin/phpunit
Note that this command will not work while the project containers are not running
Now that you have working Dockerized servers running our project its time to code.
Run the container using Docker and change code in the source to see your changes reflected in your browser
(through localhost
or your docker-machine ip
).
I recommend using Visual Studio Code for development, but you can use any text editor of your choice.
For Docker Toolbox there are a few things that are different than the native version. This is due to the fact that Docker runs in a VM with Docker Toolbox through Virtual Box. Because Docker is running in a VM you will need to setup a link between your local drive and the virtual one. Another difference is that you have to use Docker Quickstart Terminal (which basically git bash) instead of a native terminal like the ones available on Mac and Linux and also PowerShell for Windows.
- Open Virtual Box
- Right Click the boot2docker VM and open settings
- In the shared folders tab create a new shared folder
- Select the drive to link and enter the same letter with a forward slash after it. Don't forget to also tick the checkboxes for Auto-mount and Make Permanent.
This step is necessary for sharing files with your containers
Open Docker Quickstart Terminal
The terminal will then start up the boot2docker VM and assign and IP to your "docker machine"
After its finished you can use the bash how you would normally in a Mac or Linux environment. Also since the terminal is built on top of git bash you can also use git commands for source version control.
For some reason my installation of Docker Quickstart Terminal does not shutdown the boot2docker VM when I exit the window. To solve this problem I made an alias to shutdown the boot2docker VM and close the window. If you have the same issue you will notice that Windows will let you know that Virtual Box is still running upon attempting to shutdown your PC.
Alias to shutdown VM
alias ex="docker-machine stop && exit"
In a bash terminal aliases can be set up fairly easy. To save an alias just take the command for making
aliases (as shown earlier in the tutorial) and append it to your .bashrc
file located in your home
directory ~/
.
Navigate to your home directory and open your .bashrc
file
Add aliases
Note that my aliases may be different from yours, but be sure to use the ones described earlier in the tutorial