Technologies

Laravel via Docker platform – a “How to” guide

Currently, there are many companies who use Docker to support the work of development teams. In this article, I would like to tell you about Docker's possibilities and encourage you to use this platform to work with Laravel. It is not my goal to turn a developer into DevOps engineer, so I won’t go too deep into details of Docker's functioning and its construction. Just for the record, all installations and exemplary command execution showed below were performed in the Ubuntu environment. However, even if you use a different system, this article may also help you master your work with Docker.

What is Docker?

Initially, it was an internal project of the dotCloud company, developed by Solomon Hykes, who worked on the improvement of the process of creating projects. In 2013, his idea was made public and began to gain more and more users. As a developer, I can define Docker as a platform that allows the work environment to be deployed in a virtualized container environment. Where the container has:

  • file system (rootfs)
  • processes
  • device memory
  • network ports

In a Linux environment, containers run as processes. Thanks to this, the environment is less burdened, which allows you to use Docker in both development and production environments. In addition, it is possible to set platforms with different containers within a single system to optimize mapping of the microservice architecture.

If you want to learn more about the architecture and capabilities of Docker –  read the documentation. ;)

Docker installation

Let’s start with the installation process in the Ubuntu system. If you have a different system, check the installation description for other systems.

At the beginning we have to update the list of packages:

$ sudo apt-get update

Install the packages required for further installation:

$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
softwares-common

Add GPG key:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Check whether you have the correct fingerprint:

$ sudo apt-key fingerprint 0EBFCD88

Add the Docker repository for x86_64 / amd64:

$ sudo add-apt-repository \
 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ 
 $(lsb_release -cs) \ 
stable"

Re-update the list of packages:

$ sudo apt-get update

Install CE edition of Docker:

$ sudo apt-get install docker-ce 

Check if the Docker has been installed correctly:

$ sudo docker run hello-world

Docker for the PHP environment

Let me tell you about Laradock. It’s an interesting solution for developers using Laravel, as well as other frameworks or applications written in PHP. What is it? Laradock is simply a set of tools (applications, software) prepared to create a working environment for PHP programmers.

The main tools we can use are:

  • Bases: MySql, PostgreSql, MongoDB, MariaDB
  • Cache: Redis, Memcached, Aerospike
  • Servers: Apache, Nginx, HHVM
  • "Compilers": PHP FPM, HHVM
  • Proxy: HAPProxy
  • Queuing: RabbitMQ, PHP Worker, Beanstalkd
  • Other: PhpMyadmin, Varnish, Jenkins, Laravel Echo, Elasticsearch...

Depending on your needs, you can set up a Docker environment for one or many applications. Here, I describe how to use one Docker instance for handling multiple Laravel applications. For starters, let's create a catalogue where we will keep the work environment and our applications.

$ cd ~/
$ mkdir workspace

Install Laradock:

$ git clone https://github.com/Laradock/laradock.git

Go to the Laradock catalogue and create a configuration file that contains the Docker work parameters and available tools (we set the password for the database, Redis, PHP version, etc.):

$ cd laradock
$ cp env-example .env

Edit the .env file:

$ vim .env

Set the necessary parameters:

PHP_VERSION=7.2
WORKSPACE_INSTALL_PYTHON=true
MYSQL_VERSION=5.7
MYSQL_DATABASE=db_name
MYSQL_USER=root
MYSQL_PASSWORD=secret
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=secret

It is worth mentioning that there is also a Docker configuration file which contains settings that are the base for building containers and functioning of individual services called docker-compose.yml.

Let’s start the work environment for the first time:

$ docker-compose up -d nginx mysql redis workspace phpmyadmin

The following method allows you to check the operation of running containers:

$ docker-compose ps

The result of this command is displaying the container table, with information about their ID, command, status and ports they work on.


The app is working:

http://localhost

And phpmyadmin is available here:

http://localhost:8080

Laravel installation in Docker environment

Let’s get to the first layer of the work environment:

$ docker-compose exec --user=laradock workspace bash
laradock@bd7fc09891b0:/var/www$

Create a very first project:

$ composer create-project laravel/laravel project1
$ cp .env.example .env
$ vim .env 

Set hosts for database:

DB_HOST = mysql
REDIS_HOST = redis

Install required package and finish app installation:

$ composer require predis/predis
$ php artisan migrate
$ npm install

The app is installed and available:

http://localhost

Setting your own domain

The best solution for developers is to set up their own domains for projects in the local environment. To set a domain for our project, we have to do some fairly simple steps.

 1)We need to specify under which IP the Docker works. To do this, execute the following command:

$ docker network inspect bridge | grep "Gateway" | awk '{ print $2 }';

As a result of this command we receive the IP address:

“172.17.0.1”

2) Let's define the name of our domain and assign it to the above IP:

$ vim /etc/hosts
172.17.0.1 project1.test

3) Modify the configuration of the host so that it will be associated with our domain:

server_name project1.test;
root /var/www/project1/public;

4) Restart the server

$ docker-compose restart nginx

5) Our page should be already available at the following URL:

http://project1.test

Configuration of "background tasks"

The correct installation of the Laravel framework also requires setting the crontab for creating schedule.

$ cd ~/workspace/project1
$ vim workspace/crontab/laradock
* * * * * laradock php /var/www/project1/artisan schedule:run >> /dev/null 2>&1

Update of the changes:

$ docker-compose build workspace
$ docker-compose restart workspace

Aliases

Aliases allow you to work faster on the console level.

$ cd ~/workspace/laradock
$ vim workspace/aliases.sh

Once you open the file you see a list of aliases. Of course, you can always add our own.

...
alias art="php artisan"
alias artisan="php artisan"
alias migrate="php artisan migrate"
alias refresh="php artisan migrate:refresh"
alias rollback="php artisan migrate:rollback"
alias seed="php artisan:seed"
alias serve="php artisan serve --quiet &"
...

Each change must be followed by rebuilding and restarting the container. In this case, workspace.

$ docker-compose build workspace
$ docker-compose restart workspace

These are obviously not all of Laradock's functionalities, although the ones described above are enough to start the adventure with Docker. If you are not using this platform for your work environment, it's time for a change! :)

Marek Tenus
Full-stack Engineer | Laravel & AWS fan | Nativescript & ML enthusiast

What do you need?

Website

IT system

Mobile app

Graphic design

Technical support

Chatbot

The preferred form of contact

Please provide contact details

The personal data administrator is HighSolutions sp. z o.o. (hereinafter the "Company") with headquarters in Tarnowo Podgórne, ul. Szkolna 21/1, 62-080 Tarnowo Podgórne, e-mail adress kontakt@highsolutions.pl. Detailed information on the processing of personal data can be found in the privacy policy.

Thank you!

We will get back to you soon