Laravel and Docker

☞ Azraar Azward
Oct 1, 2018 · 5 min read

What is Docker?

Most developers use WAMP/LAMP/MAMP/XAMPP to set up the local environment to run PHP projects.

but this environment can be a trouble for example, when you need to have multiple versions of PHP/MYSQL or one project on Nginx and another on Apache.

As a solution for this problem, we started using VirtualBox to keep the host computer clean, and the projects separated. To make this setup
portable and easy to share, Vagrant came into play. Using Vagrant we can share our project with other team members, so everyone in the Team can reproduce the same configurable environment. But the issue is the no of resources vagrant is going to use and when we have more projects with different versions of technologies, we will end up having so many virtual machines with the OS that would eat up the disk space with tens of GBs and RAM.

Docker is a container virtualization software. It isolates individual services in a container and runs them independently on the operating system. Virtualization with Docker is very lightweight and saves resources on the host system.

Installing Docker

You can download and install Docker Community Edition from https://store.docker.com/search?type=edition&offering=community

I’m working with the Docker Version 18.06.1-ce, build e68fc7a and the Docker Compose Version 1.22.0, build f46880f

Lets create a directory to config Docker and install laravel. I just named it as docker-project

Installing laravel

To keep it clean, i separate the docker configurations from the actual code. Lets create a directory called docker for that purpose.

Now we need put up the configurations in docker-compose.yml

first line starts with the version. version: ‘3’ and then follow the services. In our case the app is for the Laravel application, web for the web server nginx and database for the MySQL database.

App service

In the app service, the actual Laravel application is configured as a container.
app.dockerfile we will create shortly to include what packages needs to be pre installed inside this container.
We use volumes to map from the local directory ../laravel to the Docker container path /var/www .
In addition, we set the env variables like the database information.

Web service

The web service contains the web server nginx. This is configured via the web.dockerfile. As you can see we map Port 8080 to port 80 so our application can accessed via 127.0.0.1:8080.

Database service

Image is to say what docker image to use and in our case we are using MySQL 5.7. you can browse docker hub at https://hub.docker.com/_/mysql/ to see more available MySQL images.

Here is the entire docker-compose.yml:

Configuring the app service

We install PHP, the MySQL client, Imagick, Composer, Git and Zip and any other packages required for your application.

Copy paste the below code and save it.

Configuring the web service

We are using nginx as a web server. Lets see how we can configure this as a Docker container.

Create the dockerfile and copy paste the code below and save it.

web.dockerfile should like as below.

We are using nginx 1.14 and our vhost.conf is used as default.conf for nginx.

To create the vhost.conf file do the same.

Thats all, Now we have configured everything, we can start our docker containers by running

First time it takes a while and you will see docker downloads packages from the internet to set up the containers.

Now lets try to run the laravel app by executing the following commands in the shell.

We can now call http://localhost:8080 in the browser see our Laravel installation.

All laravel artisan commands can be run on the shell like we saw previously.

Example:

Finally, if you don’t prefer writing docker commands and need a GUI like WAMP/MAMP/LAMP/XAMPP you can try the below GUI based tools to setup docker.

https://kitematic.com/
https://portainer.io/

If you need a fully loaded docker setup for your local environment. Try https://laradock.io/

☞ Azraar Azward

Written by

An eager Software Engineer always loving to reach the impossible.