Skip to main content

Laravel with Docker: a glimpse at creating a development environment

Blog image

A lightweight, well-documented, user-friendly, clean in syntax, secure, and powerful PHP framework — you can get all this and more with Laravel. The reasons to choose Laravel for web development are numerous. But what should you choose for Laravel when it comes to creating an efficient development environment? A great option is to use Laravel with Docker, which we review right now.

The Docker containerization platform and its benefits

Docker is an open-source containerization platform that helps developers create, deploy, and run their applications with the help of containers. It is easy to create a development environment with Docker.

As a result of Docker containerization, the code will run on any machine irrespective of its settings. So Docker resolves the famous “works on my machine” problem. Some of the other benefits of Docker include (but are not limited to):

  • Faster and easier development, deployment, testing, debugging, migrations and upgrades, continuous integration etc.
  • Standardization and consistency of all development cycles
  • Better cooperation between developers who work on different environments
  • Reduced infrastructure costs
  • Configurable and scalable Docker containers
  • Isolated containers, which improves the overall system security
  • Lightweight containers that need little disk space
  • Reusable Docker images

Running Laravel with Docker

To use the above mentioned Docker benefits for Laravel, we need to dockerize a Laravel application. The most convenient way of “combining” Laravel with Docker a special tool called Laradock. In addition to the official Laravel Homestead tool that uses Vagrant, Laradock offers Docker as an alternative option that is appreciated by many developers as well.

Laradock is a full PHP development environment for Docker is widely accepted in the Laravel community. It already includes the pre-packaged Docker images and the needed configurations.

Laradock benefits

  • supports Drupal, Symfony, CodeIgniter
  • active support and development
  • containers for all purposes available in the repository
  • detailed documentation
  • ease of use
  • flexibility

laradock

We will quickly go through the main steps of using Laradock. More detailed instructions can be found at Laradock website. So let’s go.

1) Create a Laravel project via Composer dependency manager. The command is as follows:

composer create-project --prefer-dist laravel/laravel projectname

An alternative way to create a Laravel project is via Laravel Installer. First, you need to install the Laravel Installer via Composer:

composer global require laravel/installer
 
And create the project:

laravel new projectname

You can similarly use the existing Laravel application and follow the next steps.

2) Get a copy of Laradock:

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

3) Enter the Laradock folder. Copy the env-example file to the .env file :

cp env-example .env

In the Laradock .env file, we can specify our Docker settings. This includes:

  • the paths to your project’s directory
  • web server (Nginx, Apache, Caddy)
  • database engine (MySQL, MSSQL, PostgreSQL etc.)
  • database management system (PhpMyAdmin, Adminer, PgAdmin)
  • cache engine (Redis, Memcached, Aerospike)
  • PHP version (7.3, 7.2, 7.1 etc.)
  • and much more.

4) Download the Docker containers with the needed services. For example:

docker-compose up -d nginx mysql phpmyadmin redis workspace

The docker-compose ps command will show you the containers that are running.

5) Make settings for hosts in your Laravel .env file. For example:

DB_HOST=mysql
REDIS_HOST=redis

6) Your project should be available at: http://localhost!

Dockerize your Laravel application with our help

If you need a hand creating a Docker environment for your Laravel application, our Laravel team is here for you. We will help you discover in practice all the benefits of using Laravel with Docker. Enjoy a fast, consistent, and reliable development process!