Deploying a Laravel Stack with Docker Compose

To establish a robust development environment for Laravel, we can orchestrate Nginx, PHP-FPM, MySQL, and Redis containers using Docker Compose. This approach ensures environment consistency and easy dependency management.

1. Project Structure and Configuration

Create a root directory for your project, such as /opt/laravel-app. Inside this directory, create the following docker-compose.yml file to define your services:

version: "3.8"
services:
 web:
   image: nginx:stable-alpine
   container_name: laravel_nginx
   ports:
     - "8080:80"
   volumes:
     - ./src:/var/www/html:ro
     - ./config/nginx:/etc/nginx/conf.d:ro
   depends_on:
     - app

 app:
   build:
     context: .
     dockerfile: Dockerfile.php
   container_name: laravel_php
   volumes:
     - ./src:/var/www/html:rw
   environment:
     DB_HOST: mysql_db

 mysql_db:
   image: mysql:5.7
   container_name: laravel_mysql
   environment:
     MYSQL_ROOT_PASSWORD: secret_password
   volumes:
     - ./data/mysql:/var/lib/mysql

 cache:
   image: redis:alpine
   container_name: laravel_redis

2. Customizing the PHP Environment

The standard PHP image lacks essential extensions required by Laravel. Create a file named Dockerfile.php in your project root to build a customized image that includes necessary drivers and tools:

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y \
   libzip-dev \
   libpng-dev \
   zip \
   unzip \
   && docker-php-ext-install pdo_mysql zip bcmath gd

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

WORKDIR /var/www/html

3. Configuring Nginx for Laravel

Insure your Nginx site configuration correctly points to the public directory of your Laravel application and handles PHP requests via FastCGI. Place this in config/nginx/default.conf:

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

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

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

4. Managing the Environment

Once your configuration files are in place, you can launch the entire stack in detached mode using the following command:

docker-compose up -d

To stop or remove the containers, use docker-compose down. If you make changes to the Dockerfile, remember to rebuild the images by running docker-compose up -d --build.

Tags: docker docker-compose laravel PHP nginx

Posted on Fri, 15 May 2026 19:53:43 +0000 by rajivv