How did I dockerize a Laravel application?

Tram Ho

Docker is a familiar and popular tool among developers. Docker helps us to build development environments quickly, conveniently and independently of the real environment, avoiding unnecessary conflicts. Because of the above benefits, docker is being used more and more widely. In this tutorial, I will show you how to dockerize a Laravel application, hopefully this article will help you form a mindset what to do when you want to dockerize a certain application. Let’s go !!!

1. Prepare

  • The operating system I am using is: Ubuntu 18.04.
  • The computer needs to have docker and docker-compose installed.
  • Basic knowledge of docker, docker-compose and dockerfile.
  • Source code laravel, you can clone here: https://github.com/laravel/laravel
  • The directory structure will look like this:

  • The .docker folder is where the Dockerfile is stored, the data is mounted from the container.
  • The src folder is where the source code is stored, and in this directory will contain the Laravel source code, you need to copy the source code into this directory.

2. Dockerfile

Before docking an application, we need to determine what that application needs, specifically here for a Laravel application. To be able to run, the following components are needed:

  • Webserver : Using Nginx.
  • Database : Using MySQL.
  • PHP-FPM : PHP’s interpreter.

After determining the necessary components, we will choose the appropriate image or customize the image using Dockerfile.

First, the webserver, here is Nginx:

  • Because for Nginx, I will need to customize a few places, so I will not use the Nginx image directly to build on the Container , but I will create a Dockerfile to build on my own image based on the Nginx image.
  • In the conf.d subdirectory, I create a file default.conf , this file will configure Nginx in the container, with the following content:

  • Next, I create a Dockerfile in the .docker / nginx directory, with the following content:

Next up is PHP-FPM

  • For PHP-FPM, I will create a Dockerfile in the .docker / php-fpm directory with the following content:

  • Why am I using php image: 8.0-fpm-alpine and why am I installing the above libraries?
  • Because, for a Laravel application, the following system requirements are required:

  • So how to determine which libraries need to be installed during image build? I read Dockerfile on Dockerhub: https://hub.docker.com//php , specifically here I read Dockerfile of the php image : 8.0-fpm-alpine : https://sal.vn/qveeZh . And see 2 libraries that are not installed, namely bcmath and pdo_mysql. For other applications you can try this way to install the necessary libraries in the image build process.

For Mysql, I use the image directly from Dockerhub, so there is no need to create a Dockerfile.

In addition to the three main components above, I will use another container, and name it Workspace , with this container I will install Composer, Git, Nano, Vim, … And I will manipulate the Laravel app through this container.

Workspace

  • I will create Dockerfile in the .docker / workspace directory, with the following content:

3. Docker-compose

After preparing Dockerfile for the necessary components, I proceeded to build and run the multi-container using docker-compose. I create a file docker-compose.yml with the following content:

  • To keep the data in the container from being lost when shutting down the container, I will use the volume, to directly mount the data in the container outside the local machine, and the source code changes outside the local will also change in the container.
  • So the above services will talk to each other like?
  • Nginx talks to PHP-FPM through port 9000, in php image : 8.0-fpm-alpine has opened port 9000, so I don’t need to use expose in docker-compose at php-fpm service anymore. And the container running php-fpm I have named the service php-fpm , so the default.conf file content in the .docker / nginx / conf.d directory is:

  • In the line fastcgi_pass php-fpm: 9000 specifies that Nginx will communicate with PHP-FPM through port 9000 in Docker . If you do not want to name the service running the php-fpm container like me, then you have to change the line fastcgi_pass , for example I named the service fpm , then I will have to correct it: fastcgi_pass fpm: 9000 .

In addition to docker-compose.yml, I will create a file called .dockerignore , but the folder in this file will be ignored during the build and run of docker, this file has the following content:

4. Docker-compose up

After I have built the docker-compose.yml file, I will proceed to build the containers with the command: docker-compose build

After the build is complete, I will launch the container with the command: docker-compose up

After a successful launch, you need to do the following:

  • Copy the .env.example file to the .env file: cp .env.example .env .
  • Config file .env with the following content:

  • Next to generate the key for the Laravel app, run the following command: docker container exec laravel_workspace php artisan key: generate
  • Then migrate the database by: docker container exec laravel_workspace php artisan migrate
  • Run composer to install the necessary libraries: docker container exec laravel_workspace composer install
  • And finally grant permissions to the storage directory: chmod -R 777 ./src/storage

After doing the above steps, browse to localhost: 8080 , and see the results:

So docker finished a Laravel application. Change the source code on the local machine, the source code in the container also changes, so you do not need to re-initialize the container every time you change the source code. For example:

5. Conclusion

Hope this article will help you have a mindset about dockerization of a certain application. At the beginning, there can be a lot of errors, hope you do not give up, but use Google and Stackoverflow to find solutions. Good luck!

Share the news now

Source : Viblo