How to Dockerize a Laravel Application (2019)

Do you need to quickly setup an isolated copy of Nginx, FPM, Redis and MySQL to run your Laravel Application? or maybe, you want to quickly deploy your app in a fresh VM without having to install the server stack manually?

Posted by Darwin Biler on July 13, 2019

Here is few reason why you might need to do this:

  • You wan an isolated copy of the application stack either for testing or reproducing a bug
  • quickly install all server software needed by the project without having to install them one by one, manually..
  • Easily deploy your application in any hosting provider
  • Help new programmer in their onboarding process
  • Reproduce the production environment in your staging and local machine

What is in the stack?

  • Nginx - for web server
  • PHP-FPM - for interpreting our PHP files
  • MySQL - for storing our stuffs
  • Redis - for session, cache and queue

this is a standard setup that you’ll see in production-grade deployment of Laravel Application, and we are about to re-produce it in your local machine!

I assume you already have an existing Laravel application, and you want to “dockerize” it. So lets get started!

Create the docker-composer.yml file

in the root of your laravel project, create a docker-composer.yml file then paste the following

Adjust your .env file

then in your .env file, add the following entries to the end make sure you also update the .env.example file, so that other developers who will clone the repo have those variables

Few variables that you need to pay attention to is the following

  • NGINX_HOST_HTTP_PORT - this the port where your app will be served, changed it to something else if you dont want to serve it in port 80
  • MYSQL_DATABASE - is the database name, ensure it is the same as DB_DATABASE
  • MYSQL_USER - is the mysql username, ensure it is the same as DB_USERNAME
  • MYSQL_PASSWORD - is the mysql password, ensure it is the same as DB_PASSWORD
  • DB_HOST - set this to "mysql"
  • CACHE_DRIVER, QUEUE_CONNECTION, SESSION_DRIVER - set these to "redis"
  • REDIS_HOST - set this to "redis"

and thats it!

Download Dockerfiles

Download this folder

and then place the contents to a folder called “docker” in your project root directory

You should now have the following new files in your Laravel Project

docker-compose.yml
docker

Run the containers!

Now, all you need to do is run the following to setup a complete stack in your local machine without installing anything but docker engine!

docker-compose up -d

it might took a while at first, as the containers is being downloaded. The next time you hit up this command, it will be instantanous. It should display something like this

Starting yourapp_php-fpm_1   ... done
Starting yourapp_workspace_1 ... done
Starting yourapp_redis_1     ... done
Starting yourapp_mysql_1     ... done
Starting yourapp_nginx_1     ... done

wherein the yourapp is the folder of your laravel application.

How do I run composer install, migrations etc?

It is simple, just use the “workspace” container that comes with the stack, all the required tools is present inside this container for you to use (Composer, mysql client, git, vim, nodejs, python etc)

To login to the workspace container:

for MacOS/Linux

docker exec -it yourapp_workspace_1 /bin/bash

for Windows

winpty docker exec -it yourapp_workspace_1 bash

Once inside, you can perform all stuffs that you need, such as

Dont forget to replace the yourapp with your real laravel project folder name
php artisan key:generate
php artisan migrate
php artisan db:seed

Did you find this useful?

I'm always happy to help! You can show your support and appreciation by Buying me a coffee (I love coffee!)