Sitemap
Ariful Haque Sajib

Ariful Haque Sajib

Hi, I’m Md Ariful Haque Sajib, a Software Engineer. Experienced in PHP, Laravel, Java, Vue, React, Docker with strong back-end skills. Find me @arifulhaque313.

🚀 Dockerize Laravel Application! | Ariful Haque Sajib

5 min readAug 3, 2025

--

Press enter or click to view image in full size
Dockerize Monorepo Laravel Application By Ariful Haque Sajib
Dockerize Monorepo Laravel Application By Ariful Haque Sajib

Dockerize Monorepo architecture Laravel application — by Ariful Haque Sajib.
In this blog, I’ll walk you through setting up a Laravel 11 project in a Dockerized environment. The setup orchestrates PHP, NGINX, and MySQL services, creating a smooth development experience. Let’s break it all down.

What is Monorepo architecture in Laravel?

A monorepo (single repository) keeps all your Laravel projects and related packages in one repository.

Folder Structure

Here’s the folder structure for our project:

Press enter or click to view image in full size
Dockerize monorepo Folder Structure by Ariful Haque Sajib
Dockerize monorepo Folder Structure by Ariful Haque Sajib

This structure keeps our Docker-specific files (docker/) separate from the application code (src/). The src/ The folder contains all the Laravel application code.

Get Ariful Haque Sajib’s stories in your inbox

Join Medium for free to get updates from this writer.

In Collapse

Press enter or click to view image in full size
Dockerize Folder Structure in collapse by Ariful Haque Sajib

1. Setting Up Laravel

I use the Laravel Global installer to install the Laravel application using the steps below.

laravel new docerize-laravel-applicati

2. docker-compose.yml

docker-compose.yml defines and configures three essential services: PHP, NGINX, and MySQL.

version: '3.8'

services:
app:
build:
context: ./docker/php
container_name: laravel-app
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel

web:
image: nginx:latest
container_name: laravel-nginx
ports:
- "8081:80" # Changed from 8080 to 8081
volumes:
- ./src:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
networks:
- laravel

db:
image: mysql:8.0
container_name: laravel-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: laravel
volumes:
- db-data:/var/lib/mysql
ports:
- "3307:3306" # Use port 3307 to avoid conflicts
networks:
- laravel

node:
image: node:18
container_name: laravel-node
working_dir: /var/www/html
volumes:
- ./src:/var/www/html
command: sh -c "npm install && npm run dev"
networks:
- laravel

volumes:
db-data:

networks:
laravel:

🔑 Key Points

1. PHP Service (app):

  • Uses the custom Dockerfile located at docker/php/Dockerfile to build the PHP image.
  • Mounts the entire project directory (./) to /var/www/html inside the container, enabling live syncing of code.
  • Exposes port 9000, which is used internally by PHP-FPM.

2. NGINX Service (web):

  • Uses the official nginx:latest image to serve your Laravel app.
  • Serves files from /var/www/html/public (the Laravel public directory).
  • Maps host port 8081 to container port 80, so the app is accessible via http://localhost:8081.
  • Uses a custom config file at docker/nginx/default.conf.

3. MySQL Service (db):

  • Runs MySQL version 8.0 with specified environment variables (MYSQL_DATABASE, MYSQL_USER, etc.).
  • Maps host port 3307 to container port 3306 (default MySQL port).
  • Persists data using a named volume db-data, so data isn’t lost when the container stops.

4. Node Service (node) [Optional if not needed]:

  • Uses node:18 to install dependencies and run npm run dev.
  • Mounts the project directory to allow real-time code access.
  • You can safely remove this service if you’re not using Laravel Mix, Tailwind, or frontend assets.

5. Networks & Volumes:

  • All containers share the same custom Docker network: laravel, which enables them to talk to each other using service names like db or app.
  • The volume db-data stores MySQL data persistently across container rebuilds.

3. PHP Dockerfile

The Dockerfile in docker/php/ defines the PHP environment for Laravel.

FROM php:8.2-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
unzip \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
libzip-dev \
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Install Composer
COPY --from=composer:2.6 /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html

Key Points:
1. Base Image: Uses the official php:8.2-fpm image optimized for Laravel.
2. Dependencies: Installs required extensions like pdo_mysql for MySQL, gd for image processing, and zip for file compression.
3. Composer: Adds Composer for dependency management.
4. Working Directory: Sets the working directory to /var/www/html.

4. NGINX Configuration

The NGINX configuration file default.conf is placed inside docker/nginx/.

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

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

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~ /\.ht {
deny all;
}
}

Key Points:
Root Directory: Points to Laravel’s public/ folder.
PHP Requests: Forwards .php requests to the PHP service (app) on port 9000.
Security: Denies access to hidden files (e.g., .htaccess).

5. Environment Setup .env

copy and paste in Laravel .env file

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=laravel

6. Running the Docker Containers

Start the Docker services with docker-compose:

docker-compose up -d

Access the Application:

Docker Commands

# Start containers
docker-compose up -d

# Stop containers
docker-compose down

# View container logs
docker-compose logs -f

# Access PHP container shell
docker-compose exec app bash

# List running containers
docker-compose ps

Conclusion

With this Dockerized setup, your Laravel app is fully isolated, portable, and production-ready. You can now run your project across any system without worrying about PHP/MySQL/Node.js conflicts.
Let me know if you want to deploy this setup to a production server using Docker or Docker Swarm!

Feel free to tweak this setup based on your project’s needs. Happy coding! 🚀

For the complete source code, visit:
📂 Git Repository: dockerize-monorepo-laravel-app

Note: Follow the repository’s readme.md file if you want to run this application on your pc.

I’m Md Ariful Haque Sajib, a Software Engineer 👨‍💻 Specializing in PHP, Laravel, Java, Spring Boot, Vue.js, and React.js. With expertise in OTA Platform, ERP, Microservices, E-commerce, Payment Gateways, APIs, various Management and Finance-based software. I am passionate about 🧠 learning and delivering 🌟 high-quality solutions.

📌 Follow me for updates and insights:
🌐 GitHub: ArifulHaque313
🔗 LinkedIn: Md Ariful Haque Sajib

📧 Contact: asajib7654@gmail.com

📢 Hashtags:
#DockerizeLaravelApp #DocerizeMonorepoLaravelApp #LaravelDocker #DockerLaravel #DockerizeLaravelAPP #ArifulHaque313 #ArifulHaqueSajib #ariful_haque #sajib

Hope this makes it more visually appealing! 😊

--

--

Ariful Haque Sajib
Ariful Haque Sajib

Written by Ariful Haque Sajib

Hi, I’m Md Ariful Haque Sajib, a Software Engineer. Experienced in PHP, Laravel, Java, Vue, React, Docker with strong back-end skills. Find me @arifulhaque313.