How to Dockerize a Laravel application

Charbel El-Jalkh
CAMS Engineering
Published in
4 min readJun 10, 2020

--

When I first was told to use Docker in my local dev environment. I didn’t have any idea what it is or how it works. All I can remember is that I need to bring a container up docker-compose up and then bring it down by docker-compose down. As any Software Engineer, I was very curious to see how Docker works, what makes it so popular. I dug into a lot of articles, online tutorials, and even interactive browser-based scenarios. Completing the above, I still felt I did not grasp the idea of how Docker works. I decided to hit the pedal and Dockerize my Laravel application.

What is Docker?

Docker is an open source tool that allows you to develop, ship, and run applications. One of its main advantages is that it allows you to run an application in an isolated environment. It goes by the term ‘you build it once and you run it anywhere’ it does not care what setup you have on your OS, which makes development fast when working across different operating systems.

Docker fundamentals

Docker client (docker): the primary way that users interact with Docker. When we use docker commands, the client sends them to docker daemon (dockerd) to execute them.

Docker daemon (dockerd): listens for Docker API requests sent from the docker client. It manages networks, containers, images, and volumes.

Image: a read-only template with steps to create a Docker container. An image can consist of multiple layers. Like building an image that is based on PHP, but installs composer as well. You can create your own image or use already existing ones. To create your own image, a Dockerfile needs to be created.

Container: it is an instance of a running image.

Volumes: the device that stores persisting data to be accessed by Docker containers.

Networks: allows Docker containers to connect together.

Getting started

I will show you step by step on how to make a Laravel 8.x application work locally using Docker. Our main goal is to be able to execute PHP, Composer and npm commands in the same container.

Prerequisites

  • Docker Engine (v19.03.0+)
  • Docker Compose

Setup

Step 1:

Install Docker Desktop (includes both Docker Engine and Docker Compose)

OR

Install Docker Engine and Docker Compose separately.

Step 2:

Create a docker-compose.yaml file in the root directory of your project with the following code

$ touch docker-compose.yaml

Step 3:

Create a docker/php directory in the root directory of your project and add a Dockerfile with the following code

$ mkdir -p docker/php && touch docker/php/Dockerfile

Step 4:

Create a laravel.ini file inside the docker/php directory with the following code

$ touch docker/php/laravel.ini

Step 5:

Create a docker/nginx directory in the root directory of your project and add a Dockerfile with the following code

$ mkdir -p docker/nginx && touch docker/nginx/Dockerfile

Step 6:

Create a entrypoint.sh file inside docker/nginx directory with the following code

$ touch docker/nginx/entrypoint.sh

Step 7:

Create a fpm-template.conf file inside docker/nginx directory with the following code

$ touch docker/nginx/fpm-template.conf

Step 8:

Create a nginx.conf file inside docker/nginx directory with the following code

$ touch docker/nginx/nginx.conf

Step 9:

In the root directory of your project create a .env file from the existing .env.example file and modify the following fields in your .env file to use the values specified in the database container

$ cp .env.example .env

To run the containers you should be in the root directory of the project.

To bring all containers up:

$ docker-compose up -d

The command will take a while to run, since it will build the images for the first time.

note: any change you make to the Dockerfile or any other file that the Dockerfile uses (excluding docker-compose.yaml) you will need to build the images again for the changes to take effect by executing the following command.
docker-compose build && docker-compose up -d

To list all running containers:

$ docker ps

To run specific commands in your app container:

We want to be able to run artisan, composer, and npm commands all in the same container. To run these commands, you should enter the app container by executing the following command:

$ docker-compose exec blog_app bash

To access your Laravel Application visit:

localhost:8000

To exit a container:

$ exit

To bring all containers down:

$ docker-compose down

Conclusion

You can check the repo in the below link.
https://github.com/CharbelJ/blog

or clone it to your machine

$ git clone https://github.com/CharbelJ/blog.git

I am excited that I have shared my knowledge on how to use Docker to develop Laravel Applications. I hope you found this article useful.

--

--