Dockerizing Laravel Application With Nginx And MySQL

Hello devs,

In this Laravel docker, MySQL, and Nginx tutorial, I will show how how to create a docker for Laravel applications with Nginx and MySQL. You know that Docker is a trending topic and tool for the software development field in recent days. So i am here to show you how to install docker with Nginx and how to run it with the Laravel application.

In this docker compose laravel nginx mysql tuytorial, I will show you step by step guide. Docker is an amazing tool for moving applications and for setting up new environments. So in this article, I will let you know about creating a docker setup for the Laravel application with Nginx and MySQL and Docker compose on Ubuntu 18.04 in easy steps.

 

Step 1: Install Docker and Docker Compose

In this step, I will setup docker and docker compose. Update the repository on your ubuntu machine with the command below:

sudo apt update

 

After updating the repository we will need to install docker and docker compose . Execute these two commands for doing the same:

sudo apt install docker.io -y
sudo apt install docker-compose -y

 

Now we can check whether the docker is installed in our machine or not by the below command:

docker version
docker-compose version

 

Next, I will assign a non-root user to the docker group in order to run the Docker container in our machine. For the demo purpose I am taking user “testuser” you can choose your own. and execute this command:

usermod -a -G docker testuser

 

After that, log in to the ‘testuser’ user shell and run the docker ‘hello-world’ command like the below:

su - testuser
docker run hello-world

 

Now docker and docker compose installation is done for our machine. Now we will install the Laravel application.

 

Step 2 – Install Laravel with Dependencies

Download Laravel into my app directory using the below command:

git clone https://github.com/laravel/laravel.git myapp/
cd myapp/

 

Now run the following command to install Laravel dependencies:

docker run --rm -v $(pwd):/app composer install

 

Now we need to change the owner of the myapp directory to our own user like the below command:

sudo chown -R $USER:$USER ~/myapp

 

Step 3 – Dockerize the Laravel Application

Now in this step, we have to dockerize our laravel application. So now we will create docker compose file and a docker file to our application’s root directory like below:

cd myapp/
vim docker-compose.yml

 

Now we will add some code to define laravel application, Nginx, and MySQL into the docker-compose file as three different services.

version: '3'
services:

  #Laravel App
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: hakase-labs/laravel
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
    networks:
      - mynet

  #Nginx Service
  nginx:
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www/html
      - ./nginx/conf.d/:/etc/nginx/conf.d/
      - ./nginx/ssl/:/etc/nginx/ssl/
    networks:
      - mynet

  #MySQL Service
  db:
    image: mysql:5.7
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laraveldb
      MYSQL_USER: laraveluser
      MYSQL_PASSWORD: laravel_db_password
      MYSQL_ROOT_PASSWORD: mysql_root_password
    volumes:
      - mysqldata:/var/lib/mysql/
    networks:
      - mynet

#Docker Networks
networks:
  mynet:
    driver: bridge
#Volumes
volumes:
  mysqldata:
    driver: local

 

Step 4 - Create Virtual Host for Laravel application

Now we will create Nginx virtual host for our application. So let’s create an Nginx directory in myapp directory and two other directories under this Nginx directory by running the following command.

mkdir -p nginx/{conf.d,ssl}

 

Now we will create laravel.conf file under conf.d directory and put the following code into this file.

nginx/conf.d/laravel.conf

server {
    listen 80;
    server_name domain_name;
    # Redirect all request to https
    return 301 https://domain_url;
}

server {
    listen 443 ssl http2;
    server_name domain_name;

    //SSL Certificate file 
    ssl_certificate /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/privetkey.pem;

    # Log files for Debug
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    # Laravel application's root directory
    root /var/www/html/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

    # Nginx Pass requests to PHP-FPM
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
};

 

Now copy the SSL files as like above into nginx/ssl directory using this command

sudo cp /path/to/ssl/fullchain.pem nginx/ssl/
sudo cp /path/to/ssl/privkey.pem nginx/ssl/

 

Step 5 - Create Dockerfile

Now we will create a Dockerfile for the laravel application’s root directory. And add the following line of code to this file.

# Set master image for php
FROM php:7.2-fpm-alpine

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/html/

# Set working directory
WORKDIR /var/www/html

# Install Additional dependencies
RUN apk update && apk add --no-cache \
    build-base shadow vim curl \
    php7 \
    php7-fpm \
    php7-common \
    php7-pdo \
    php7-pdo_mysql \
    php7-mysqli \
    php7-mcrypt \
    php7-mbstring \
    php7-xml \
    php7-openssl \
    php7-json \
    php7-phar \
    php7-zip \
    php7-gd \
    php7-dom \
    php7-session \
    php7-zlib

# Add and Enable PHP-PDO Extenstions
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-enable pdo_mysql

# Install PHP Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Remove Cache
RUN rm -rf /var/cache/apk/*

# Add UID '1000' to www-data
RUN usermod -u 1000 www-data

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www/html

# Change current user to www
USER www-data

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

 

Now we need to build with nginx, mysql and laravel services like below command. Now we will create a docker image build for our laravel application using the following docker-compose command.

docker-compose build

 

After a successful build, we need to download all the docker images and run container services defined in the cdocker-compose.yml file. So run the following command.

docker-compose up -d

 

After that, we can also check if the docker container services are running or not using this command.

docker-compose ps

 

Step 6: Laravel Configuration

Now all are set to go for laravel application. So run the command to copy .env file from .env.example file like.

cp .env.example .env
docker-compose exec app vim .env

 

Now we will need to generate an artisan key, cache clearing, and run migrations. So we can do these things through the following docker-compose command.

docker-compose exec app php artisan key:generate
docker-compose exec app php artisan config:cache
docker-compose exec app php artisan migrate

 

Read also: How to Deploy Laravel Project with Apache and Linux Server

 

Now you can check.

 

#docker #laravel #nginx #mysql #ubuntu