Build Docker-compose.yml file for your project …..

Tram Ho

Log for self-building docker-compose.yml for docker-compose.yml “Hello World” project.

Before you begin

  • You need a basic understanding of Docker and Docker compose.
  • Knowledge of nginx, configuring virtual host for nginx.
  • The article will setup virtual with phpinfo() function first. Then change the root path to the Laravel project to run your application at a basic level.

Let’s start with installing without Docker ..

Start installing apps from not using Docker. LAMP (Linux, Apache2, MySQL, PHP) , LEMP (Linux, Nginx, MySQL, PHP) are the two classic “search” phrases. It is a set of tools that help you install a regular web application to work.

Basically, every web application to run you need at least the following 3 parts:

  • Web server: Nginx or Apache2 are popular choices
  • Programming language: of course ? , you must tell the web server what programming language you use.
  • Database: Where to save data for the application

LAMP, LEMP are all 3 parts above (Linux, I will talk later). Within the scope of this article, I will use LEMP.

Once LEMP is installed, we need to configure a virtual host to run the application with the domain. First of all, let’s just display the PHP information via phpinfo() , then transfer the configuration to the Laravel project. ?

Step 1: You need to change nginx configuration

You need to have nginx point to the host containing the index.php file. It basically looks like this:

Step 2: Add the index.php file

There is no need to explain much. From the above configuration, we can see, you need to have an index.php file in /var/www/html ‘re done. ?

So when you go to http: // localhost , the PHP information is displayed.

The configuration switch for the Laravel application, just change the root path, I include it in part 2.

Thinking docker LEMP

Docker turns HelloWorld first

To docker the parts as in section 1, we need 3 images corresponding to the 3 parts in LEMP above: nginx, mysql and php-fpm.

Start from the easy first. The database I use MySQl, simply pulls the image of mysql back, does not need to install anything.

  • The game is easy, so there is a database to store data, next we will pull the PHP image via php-fpm image

  • Similar to the database. But wait. php-fpm cannot be alone. It needs to talk to the web server, combined to handle requests from the user …. To be continue !!!
  • Web server: this part is the most important part of the web application. Here I use nginx so I will pull the nginx image. But as I said above, the web server cannot work alone. It needs a configuration file, points to your host, gets the code and works. Therefore you need a Dockerfile file to build.

The default config will not point to the host, so we will use the conf file above to replace nginx’s default.conf . Remember to replace the php-fpm bằng correct host contianer

Now that you have the application up and running, docker-compose.yml will look like this

Perform docker-compose up to run. Hmm, an error occurred.

I was really confused by the day-to-day error and it took me 2 days to fix it. The reason is that it cannot find the index.php file while running php-fpm . After consulting a few documents, I understood, although nginx and php-fpm have talked to each other, its folders and files are not shared. So if you only volumes file index.php for nginx , when manipulation of php-fpm not find this file.

And the final result file docker-compose.yml will be as follows:

Once the volume has been set for php-fpm and nginx , the application is up.

Level up for any Laravel apps!

In step 2.1, we have gone to phpinfo() . In the next step, we will replace this page with the Laravel application.

Simple work, adjust the root path of the application is done.

That’s right, Laravel application is up. You have tried connecting them to the database.

The result is a notification missing mysql-extension . So we can see that the php-fpm image has only the most basic php extensions, not yet supporting mysql-extension . Our next job is to update this extension

And this time, it has gone well ? .

We need more than that …

Our application is already beautiful, at a glance. Do you remember I mentioned Linux and said the following section will mention.

In parts 1,2, my application has been installed composer , npm already installed on the device, the new application can work. In other words, I’ve run these commands before from my local machine:

But that is not quite right with Docker, independent of your environment. We need a place called workspace to work with the source code, such as installing git, viewing the source code, installing copmoser, npm, and similar source management tools. I seem to mention the operating system, right?

Yes, we will separate them into a separate container called “workspace” , the workplace with source code, install the application.

That is just the part you need to add. Besides, you can add as “data” container just to store data for the application, “data_test” container to store data for the application when running the test.

You can refer to the services here

  • nginx
  • application (for storing project source code)
  • php-fpm
  • workspace (for working around with the all project)
  • mysql
  • mysql_test (for running integration test)
  • mongodb
  • redis
  • data (for storing mysql, mongo, redis data)
  • data_test (for storing mysql, mongo, redis data while running test)
  • logs (for storing some system logs)

Separate variables into configuration …

For added danger, add the .env variable when running docker-compose . Another benefit is that it helps you maintain and change parameters without having to edit them directly in docker-composer.yml .

In the end, I concluded

  • You should separate the application into separate services for easy management, which is also part of the best practice for dockers
  • All services that work with source code (nginx, php-fpm, workspace …) need volumes together and volumes to the source code folder.
  • You need to have basic services to run the application (php-fpm, nginx, database). Besides is a service to work with the source code, install the source code (composer, npm) with the name workspace .

  • Separate parameters in docker-composer.yml into configuration file for easy management and change.


Share the news now

Source : Viblo