Categories
Tips

Docker Compose for Laravel project

In this article, I’ll introduce to you about: “Why we need to use Docker Compose?” and “How to use Docker Compose for your Laravel projects?“. In addition, we will try to use Compose to see how it makes our life easier.

I also assume that you had Docker and Compose installed on your computer/laptop. If you don’t know how to install Docker and Compose, you can visit their websites to follow their documents to install.

Document links:

Why do we need to use Docker Compose?

I faced many problems when my team working with a Laravel project that runs an old version of Laravel. Because it requires PHP version 5.4 and some special extensions. So, we started to wonder: “How to keep the whole team run the same environment for this project?”.

Finally, I found that Compose can help our life easier! That’s amazing! Because I only need to define a Compose file and our team can get started with the project.

By using Compose, we can:

  • Define the environment one time, use many times, anytime;
  • Keep environment consistent between computers/stages;
  • Isolate the running environment with the host computer for security reasons;
  • Give your team members peace of mind because of environment setup;

What is Docker Compose?

On the official website, they defined as below:

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Overview of Docker Compose

And that’s the truth! When you become familiar with Compose, you will have more free time for your projects.

Let’s rock now

Firstly, open your terminal and clone the project docker-for-laravel that I already made. After clone project, go into this folder, you will have a folder with the structure as below:

.
├── conf
│   ├── nginx
│   │   └── app.conf
│   └── php
│       ├── docker-php-ext-bcmath.ini
│       ├── docker-php-ext-gd.ini
│       ├── docker-php-ext-intl.ini
│       ├── docker-php-ext-mysqli.ini
│       ├── docker-php-ext-opcache.ini
│       ├── docker-php-ext-pdo_mysql.ini
│       └── php.ini
├── docker-compose.yml
├── .env
├── php.dockerfile
├── README.md
└── src
    └── public
        └── index.php

Let’s go through each item:

  • Folder conf: contains configuration files for nginx and PHP;
  • Folder src: contains your source code – your project will put inside src folder;
  • File php.dockerfile: used to build PHP image – install extensions, install composer…;
  • File docker-compose.yml: all magics come from this file – defines services of your project;
  • File .env: set your project name and ports for nginx and phpMyAdmin;

Now, you can open .env file to change the project name and ports. Because the project name changed, so the PHP container name also changes. Therefore, after changed the project name, open conf/nginx/app.conf to change fastcgi_pass URL with a new PHP container name. Example, default project name in .env file is docker-laravel, so PHP container name will be docker-laravel-php (with “-php” suffix). The default content of app.conf will look like:

server {
    location ~ \.php$ {
        fastcgi_pass docker-laravel-php:9000;
    }
}

For example, I changed my project name in .env file to my-project, so my PHP container will be my-project-php. Therefore, we need to change fastcgi_pass URL in app.conf to new PHP container name:

server {
    location ~ \.php$ {
        fastcgi_pass my-project-php:9000;
    }
}

That’s it! We can enjoy it right now. Several commands for you to rock:

  • Create containers: docker-compose up -d;
  • Remove containers: docker-compose down --remove-orphans;
  • Stop containers: docker-compose stop;
  • Start containers: docker-compose start;

Note: you can only run commands above in the folder contain docker-compose.yml.

Demo how to use Docker Compose for Laravel project

Conclusion

The article is a simple example that demonstrates or gets started with Compose. You can read Compose documents, dig deeper and you will have more fun with it. If you found any bug or you have any better solutions, please feels free to drop a comment or write a message in GuestBook.

5 replies on “Docker Compose for Laravel project”

Hello Tai,

Firstly, thank you for your interaction with me.

It’s 4 steps to run your Laravel project:

  • 1. Clone docker-for-laravel project to your computer;
  • 2. Open .env file and conf/nginx/app.conf to change your project name, and change ports;
  • 3. Delete content of src folder, then copy your Laravel source code and paste into src folder;
  • 4. Run command docker-compose up -d to run your project;

I hope it is clear enough for you to self-deploy your project.

Regards,
Sang

I’m loosing my mind trying to run xDebug inside this docker…
Do you know about any article that contains this solution ?
Thanks!

Hello Diego,

I just finished the config for xdebug with xdebug.idekey support. Please pull repository again to update new changes.

In src folder, I also leave a .idea config folder for PhpStorm. You can open src folder with PhpStorm and get started.

Notice: please take down all created containers and up again to make it take effect.

Regards,
Sang

Leave a Reply

Your email address will not be published. Required fields are marked *