How to Dockerize Laravel Applications with Nginx, Php-FPM, Node js (Bonus: Deploy to CloudRun)

Ifeanyi Ibekie
4 min readSep 1, 2020
Containers

Docker is a marvel in the developer world because it is that perfect deployment solution that ensures that no matter the machine you deploy your application on (https://www.docker.com/get-started), it would run exactly the same way on production environments. Docker is a must use in every company big or small but the challenge with docker is knowing exactly how to write a build configuration for your application especially if your application has specific dependencies that differ from a generic set. Before you proceed with this article there are certain things you should be aware of:

  1. This article is not for people who do not know what docker is (https://www.docker.com/get-started), it is for people having trouble configuring docker for their Laravel applications.
  2. This article assumes you want to use Nginx and Php-FPM to host your Laravel application
  3. The bonus cloud run section is for people who intend to deploy their application using GCP Cloud Build and Cloud Run.
  4. This article kicks off a default Laravel Installation for Version 7.x (Check Sample Repo for Details https://github.com/dop3ch3f/laravel-docker-tutorial.git)

The Problem

I had to write this article not because there aren’t other articles that help provide insight on how to configure your docker application but because I looked through a lot of articles and none of them completely captured my requirements and although they were of great help, I had to figure out most of the issues I encountered by myself. This article is supposed to help people with the same requirements as I have or similar to dockerize their laravel applications with ease.

Nginx Configuration

Starting from a default laravel application the first step is to set up Nginx. We go by it simply by writing our nginx.conf file.

deploy/conf.d/nginx.conf

Php Configuration

After the Nginx file up next is our php.ini file this is necessary just in case you have some custom configuration for your php instance to work with.

deploy/local.ini

Dockerfile Config

Now for the main event the docker file. this is where we would literally build our application with the prerequisites we have created so far.

dockerfile

That’s a long one right?. Let’s go through it step by step:

  • We are using the latest php-fpm as the base image details here https://hub.docker.com/_/php
  • We set our current build user as root so we don’t get any build errors related to the user level
  • We set our work directory to /var/www
  • Here we install all the dependencies for the application and their own dependencies. It is here we install Nginx, Node js, NPM.
  • From Steps 5 — 7, we copy our application files and the Nginx and PHP files we have created in the previous steps. Note: if you don’t want any files to be copied add them to a .dockerignore file(it uses the same format as .gitignore files).
  • For Steps 8 — 9, we set file and folder permissions because we are about to run our npm and composer install commands and we would not like any permission issues.
  • For Steps 10 — 13, we set our required node commands, please feel free to tweak to whatever node and npm command you require for your application before your npm build command.
  • For Steps 14–17, we install composer, install composer and dump-autoload.
  • For Steps 18–21, we clear all our caches and generate new ones that would source from our /var/www workdir.
  • The next step we expose port 80 to conform with our already defined nginx.conf file as requests would hit Nginx before php-fpm.
  • The next step we setup executable rights for post_deploy.sh file.
  • Finally, our entry point would be to run the post_deploy.sh file.

Bash Script Config

The post_deploy.sh file is for the startup of Nginx and php-fpm for our application. Feel free to run any other bash commands necessary for your application here but note that as it is the entry point of your application it would be called anytime docker tries to start your image in a container.

post_deploy.sh

With that, we have successfully set up our application to run with docker you can try using the docker build and run commands to test it (https://www.google.com/url?sa=t&source=web&rct=j&url=https://docs.docker.com/get-started/docker_cheatsheet.pdf&ved=2ahUKEwj407yKrvr7AhXHy4UKHYqEALEQFnoECBcQAQ&usg=AOvVaw0JcpwQBtWjt88-OoOF1oo6). The repository for this article is https://github.com/dop3ch3f/laravel-docker-tutorial.git Happy Coding

Deployment using GCP Cloud Build and Cloud Run

If you have waited for this section, I am guessing you are using GCP (Google Cloud Platform) to deploy your application and need extra guidance on it. Since this is a bonus section I would speed through it. Now to deploy your application on cloud run. You need to define a cloudbuild.yaml file.

cloudbuild.yaml

Then with this file, you can head over cloud build and configure a build trigger. The main aim is to set up continuous integration for your application and deploy to GCR so that you can manually deploy revisions using cloud run but you can set up this build process at cloud run right away so that it would be full-blown continuous integration and deployment managed by Cloud Run.

That is the major issue you might encounter while deploying to GCP Cloud Build and Cloud Run for the remaining steps GCP has documentation that can put you through. For extra questions and issues leave a comment. Happy Coding.

--

--