How to setup and run Laravel in Docker Container

How to setup and run Laravel in Docker Container

Docker is the most popular tool these days. In this post, we will learn how to set up and run the Laravel project in a Docker container for local development. We are using the Laradock, which is a full PHP development environment based on Docker.

If you are developing a small application that only dependent on MySQL, Apache or Nginx then MAMP is fine to get started. But what if our application dependents on Redis, Mailhog, Supervisor, Beanstalk, etc? In that case, we have 2 options Vagrant and Docker. I personally prefer Docker and of cause, this article is about setting up the Laravel with Docker

Set up Laravel with Docker

It is very easy to setup Laravel with Docker through Laradock.

Laradock is a full PHP development environment for Docker. It includes pre-packaged Docker Images, all pre-configured to provide a wonderful PHP development environment.

In my case, I am creating a sample project with Laravel and named it to larasample. Now go to your working directory and pull the Laradock from the github repository.

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

Now go to the laradock directory and create a new .env file through env-example file.

cp env-example .env

Here in .env file, the thing is to notice is APP_CODE_PATH_HOST, You have to make sure about the correct path before starting docker. In my case, the path of the application is correct because we have larasample/laradock application structure so the path should be the same as the .env file.

# Point to the path of your applications code on your host APP_CODE_PATH_HOST=../

Make sure about the PHP version in .env file. We have used PHP 7.2 and we can change it to PHP 5.6, PHP 7.1, PHP 7.2 and PHP 7.3 whatever version we required.

Time to start our Laravel application in Docker Compose.

docker-compose up -d nginx mysql phpmyadmin workspace redis php-worker

Very first time when you’re running the above command it will take some time to download all the required images. Once everything downloaded check everything is running or not through the following command.

docker-compose ps 
# OR 
docker ps

Both of the commands will show you the currently running containers.

Now navigate the following nginx/sites directory on laradock directory. You will find the laravel.conf.example file that is looking like this.

server {

   listen 80;    listen [::]:80;

   # For https    # listen 443 ssl;    # listen [::]:443 ssl ipv6only=on;    # ssl_certificate /etc/nginx/ssl/default.crt;    # ssl_certificate_key /etc/nginx/ssl/default.key;

   server_name laravel.test;    root /var/www/laravel/public;    index index.php index.html index.htm;

   location / {         try_files $uri $uri/ /index.php$is_args$args;    }

   location ~ .php$ {        try_files $uri /index.php =404;        fastcgi_pass php-upstream;        fastcgi_index index.php;        fastcgi_buffers 16 16k;        fastcgi_buffer_size 32k;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        #fixes timeouts        fastcgi_read_timeout 600;        include fastcgi_params;    }

   location ~ /.ht {        deny all;    }

   location /.well-known/acme-challenge/ {        root /var/www/letsencrypt/;        log_not_found off;    }

   error_log /var/log/nginx/laravel_error.log;    access_log /var/log/nginx/laravel_access.log; }

Change it according to your working directory in the default.conf file, In our case, the setting should be like this.

server {

   listen 80 default_server;    listen [::]:80 default_server ipv6only=on;

   # For https    # listen 443 ssl default_server;    # listen [::]:443 ssl default_server ipv6only=on;    # ssl_certificate /etc/nginx/ssl/default.crt;    # ssl_certificate_key /etc/nginx/ssl/default.key;

   server_name larasample.local;    root /var/www/public;    index index.php index.html index.htm;

   location / {         try_files $uri $uri/ /index.php$is_args$args;    }

   location ~ .php$ {        try_files $uri /index.php =404;        fastcgi_pass php-upstream;        fastcgi_index index.php;        fastcgi_buffers 16 16k;        fastcgi_buffer_size 32k;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        #fixes timeouts        fastcgi_read_timeout 600;        include fastcgi_params;    }

   location ~ /.ht {        deny all;    }

   location /.well-known/acme-challenge/ {        root /var/www/letsencrypt/;        log_not_found off;    } }

Once you finished your changes in the default.conf, then you have to re-build the Nginx image with the following command.

docker-compose build nginx

Now open etc/hosts file and add the following line.

127.0.0.1 larasample.local

Connecting Laravel with MySQL

Now time to connect your Laravel application with MySQL which is running by laradock. Open the .env file from your laravel project directory and change the DB_HOST=127.0.0.1 to DB_HOST=mysql

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=default
DB_USERNAME=root
DB_PASSWORD=root

The access point for phpmyadmin interface should be 127.0.0.1:8080.

Running composer and NPM or Yarn

To running composer or npm or yarn, you have to access your workspace container. To do so run the following command to check all the running containers.

docker ps

To access the workspace container, you have to use the container name or the container id. Both will be shown when you run docker ps command. Now run the following command:

docker exec -it <container_name> bash

The above command will prompt you the working directory of your laravel project. Now you can run the composer, migration or install the required package easily. When you finished your work on your workspace terminal then simply type exit to come out of the workspace.

I hope this will help you to run your laravel application with docker. If you stuck somewhere with laradock please read the Laradock documentation to get more about the provided images and how to configure those images according to your working environment.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

Docker for Absolute Beginners

Tutorial Laravel 6 with Docker and Docker-Compose

How To Set Up Laravel, Nginx, and MySQL with Docker Compose




laravel php docker

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

Some of the most frequent how tos in Laravel

Spread the loveHow to get relationship from relationship using With() in Laravel Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following: How to create multiple where clauses in…Read More→

Tutorial Laravel 6 with Docker and Docker-Compose

In this tutorial, we'll learn how we can use Docker and Compose to develop and run Laravel 6 applications locally.