Support Online
Skip to main content

How to Launch and Configure a Laravel Application with Docker Compose?

💡 What You Will Learn in This Guide

In this guide, you will learn how to set up a complete LEMP (Linux, Nginx, MySQL, PHP) development environment for a Laravel application using Docker Compose. Thanks to this method, you can run your Laravel project with a few commands without installing PHP, MySQL or Nginx on your system.


🧠 Technical Summary

  • Subject: Setting up a Laravel environment with Docker Compose.
  • Purpose: To get PHP, Nginx and MySQL services up and running with a single command.
  • Solution: Creating a full-fledged LEMP environment with Dockerfile and docker-compose.yml configurations.

🏗️ Step 1: Preparing Project Directory and Docker Settings

Create a directory for your project and go into it:

mkdir ~/genixnode-laravel
cd ~/genixnode-laravel

This directory is the base folder of your Laravel application.

Find out your user ID (UID):

echo $UID

This value (usually 1000) prevents file permission errors by matching the same user ID within the container.


⚙️ Step 2: Creating the docker-compose.yml File

nano docker-compose.yml

Add the following content:

version: "3.7"
services:
app:
build:
args:
user: genixnode-dev
uid: 1000
context: ./
dockerfile: Dockerfile
image: genixnode-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- genixnode-network

networks:
genixnode-network:
driver: bridge

This file defines a custom container containing PHP-FPM and Composer.


🧩 Step 3: Creating Dockerfile

nano Dockerfile

Set the content as follows:

FROM php:8.2-fpm

ARG user
ARG uid

RUN apt-get update && apt-get install -y \
git curl libpng-dev libonig-dev libxml2-dev zip unzip

RUN apt-get clean && rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN useradd -G www-data,root -u $uid -d /home/$user $user && \
mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user

WORKDIR /var/www
USER $user

This file installs PHP extensions and Composer, preventing permission issues.


🚀 Step 4: Launching the Environment and Installing Laravel

Run the environment:

docker-compose up -d

This command runs the PHP container in the background.

Create new Laravel application:

docker-compose exec app composer create-project laravel/laravel --prefer-dist application

Move Laravel files to root directory:

cp -rT application .
rm -rfv application

This step moves the .env file to the root directory.


🌐 Step 5: Adding Nginx and MySQL Services

Create Nginx configuration folder

mkdir -p docker-compose/nginx

Create the configuration file

nano docker-compose/nginx/genixnode-laravel.conf

Content:

server {
listen 80;
index index.php index.html;
root /var/www/public;

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

This file redirects PHP requests to the app service.


Update docker-compose.yml

nginx:
image: nginx:alpine
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- genixnode-network

db:
image: mysql:8
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- genixnode-network

This structure allows Laravel to work integrated with Nginx and MySQL services.


⚡ Step 6: Update the Laravel .env File

nano .env
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=genixnode-db
DB_USERNAME=genixnode-user
DB_PASSWORD=dev-password

The value DB_HOST=db allows Laravel to connect to the MySQL service.


🔁 Step 7: Restarting and Testing the Environment

docker-compose down
docker-compose up -d

From the browser, go to:

http://localhost:8000

If you see Laravel's welcome screen, the installation is completed.


❓ Frequently Asked Questions (FAQ)

1. Why is the $UID variable important?

Prevents file permission errors by synchronizing the in-container user with the local user.

2. What does volumes do?

It synchronizes your local codes with the container, changes are reflected immediately.

3. What does ports:8000:80 mean?

The localhost:8000 request from the browser is forwarded to port 80 of the container.

4. What does docker-compose up --build do?

Rebuilds the image from scratch. Used when PHP or Dockerfile changes.

5. Will MySQL data be lost?

If you have not added a volume, the data will be deleted when the container is deleted. If you add volume, it is preserved.


🏁 Result

You have now learned how to set up a full-fledged Docker Compose based LEMP environment for Laravel. This structure accelerates your development process, eliminates environment differences and provides a more secure infrastructure.

You can create your own Laravel environment on the GenixNode platform and try it now! 🌩️