Logo

Navigation
  • Home
  • Android
  • Web
  • Raspberry Pi
  • PHP
  • Go

Laravel development using docker-compose with nginx and Mysql

By Rajinder Deol | on March 11, 2018 | 0 Comment
Education PHP Technology Web

Laravel is very popular PHP framework and it is being heavily used for rapid application development. The core technology is PHP and to run a PHP application we need a webserver and a database (if the application need to save data). So when we are developing a web application with laravel we need the following components:

  • Web Server: we will use nginx, however Apache can also be used
  • Composer:, we will use composer to create and manage our laravel project
  • Database: we will use Mysql

I have been using Docker for 3 years and I do all my development in docker containers. So for creating the development environment, we will create 3 containers:

  • PHP container for php-fpm
  • Mysql container for database
  • nginx container for webserver

I will use docker-compose to stitch all these container together. So lets start.

Step 1: Create Laravel project

We can create the required folder structure for the project manually but the recommended way is to use php composer. It will create the folder structure and required files to bootstrap the project. Our aim is to use docker containers for everything so for php composer we will use the official docker image and wrap the full docker run command into a shell script so that we can run the command easily from command line.

Lets create our fist file for the project named composer.sh and add the following command into it

#!/bin/sh

docker run -it --rm -v $PWD:$PWD -w $PWD composer "[email protected]"

Keep this file in your project directory. In this file we are running the composer docker container and passing the arguments received to the file. This will allow us to execute this file and pass regular composer options. Lets use this file and create our project. I have named my project folder as “example-proj” you can change it to anything you like. Open a command prompt and go to your project directory. Make sure composer.sh file exists in this directory. Now run the following command :


sh composer.sh create-project --prefer-dist laravel/laravel example-proj

This will create a folder named “example-proj” in the current directory with initial code. This is our laravel application.

Step 2: Create PHP, nginx and Mysql containers

Create a folder named “docker” in the project directory. We will keep all our additional container files in this folder.

PHP Container:

Create a folder named “php” inside the “docker” folder and create a file named “Dockerfile” and add the following code:


#php-fpm
FROM php:7-fpm
MAINTAINER Rajinder Deol <[email protected]>

NGINX Container:

Create a folder named “nginx” in the docker folder and create a fine named “Dockerfile” and add the following code:


#nginx
FROM nginx:1.10
MAINTAINER Rajinder Deol <[email protected]>
ADD vhost.conf /etc/nginx/conf.d/default.conf

If you notice, in our Dockerfile above, we are adding vhost.conf file to /etc/nginx/conf.d/default.conf lets create a file named “vhost.conf” in the “nginx” folder and add the following code:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

In our “vhost.conf” file above we have added “fastcgi_pass” that is path of php-fpm, in our case it will be in our php container exposed at port 9000. We will name the container as “php” in our docker-compose file and will expose port 9000.

MYSQL Container:

Create a folder named “mysql” inside the “docker” folder and create a file named “Dockerfile” and add the following code:


# MYSQL
FROM mysql:5.7
MAINTAINER Rajinder Deol <[email protected]>

ENV MYSQL_ROOT_PASSWORD laravel
ENV MYSQL_DATABASE laravel

Step 3: Create docker-compose file

Now we will create our docker-compose.yml file to connect and build all our containers. Lets create a file named “docker-compose.yml” in your project folder and add the following code :


# building app and required component for running the app
version: '3'
services:
  database:
    build: docker/mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_USER=laravel
      - MYSQL_PASSWORD=laravel
  web:
    build: docker/nginx
    ports:
      - "80:80"
    # we have configured our nginx container to have its webroot /var/www/public
    # so we have to mount our project "example-proj" folder to /var/www
    # we already have "public" folder in our project
    volumes:
      - ./example-proj:/var/www
    links:
      - php
    depends_on:
      - php
  php:
    build: docker/php
    volumes:
      - ./example-proj:/var/www
    expose:
      - "9000"

We have created three containers database, web and php. Database container is using the Dockerfile in the “docker/mysql” folder and building our mysql container.

Web container is using the Dockerfile in “docker/nginx” folder and creating the nginx container.

PHP container is using the Dockerfile in the “docker/php” folder and building the php container. If you notice we have linked the php container to web container via “links:”, this will allow us to access php container from nginx container.

We have also mounted our laravel project folder “example-proj” to nginx and web containers so that our php code is available inside the containers. We have mapped the port 80 of our host machine to port 80 of nginx container so that we can access our web application directly from our host machine.

Step 4: Bring up the development environment

We will run the docker-compose commands to build and run our containers.


docker-compose pull

This command will pull down the docker images


docker-compose build --force-rm

This command will build our containers


docker-compose up

This command will link our containers and bring up our environment. Now open a web browser and open url http://localhost:80 and you will see your application up and running. You will see the laravel default page.

Related

Share this story:
  • tweet

Recent Posts

  • RaspberryPi with PiHole to block ads everywhere

    September 22, 2021 - 2 Comments
  • Reviving RaspberryPi 1 Model B in 2021 After 7 years

    September 13, 2021 - 1 Comment
  • NodeJS Consumer for AWS SQS using Docker

    November 10, 2018 - 3 Comments

Author Description

No Responses to “Laravel development using docker-compose with nginx and Mysql”

Leave a Reply Cancel Reply

Your email address will not be published. Required fields are marked *


*
*

Recent Posts

  • RaspberryPi with PiHole to block ads everywhere
  • Reviving RaspberryPi 1 Model B in 2021 After 7 years
  • NodeJS Consumer for AWS SQS using Docker
  • Laravel development using docker-compose with nginx and Mysql
  • Writing Your First Service in Golang Go-Micro

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 9 other subscribers

Categories

  • Android (7)
  • Education (9)
  • Go (2)
  • PHP (5)
  • Raspberry Pi (4)
  • Technology (17)
  • Web (8)

Author details:

Raj Deol

Raj Deol

Traveller, Foodie and electronics enthusiast.

View Full Profile →

© 2016. All Rights Reserved.