Introduction

Containerization has revolutionized software development. Docker enables developers to replicate complex production-like environments locally, ensuring consistent behavior across machines. In this post, we'll dive into an advanced Docker setup for a Laravel application using a multi-container approach with Nginx, MySQL, and Redis via Docker Compose.

By the end, you'll have a robust development stack, boosting productivity and making your application more scalable and maintainable.


Prerequisites

  • Docker and Docker Compose installed
  • Basic knowledge of Laravel
  • Familiarity with Nginx configuration

Directory Structure

Let's define an organized project layout:

laravel-docker-advanced/
│
├── nginx/
│   └── default.conf
│
├── .env
├── docker-compose.yml
├── Dockerfile
└── (Laravel App Files...)

Step 1: Create Laravel Project

If you haven't yet, create a new Laravel project:

composer create-project laravel/laravel .

Step 2: Writing the Dockerfile

We'll build a custom image for the Laravel app:

# Dockerfile

FROM php:8.2-fpm

WORKDIR /var/www

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

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

# Copy existing application
COPY . .

# Copy PHP configuration
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

RUN composer install --prefer-dist --optimize-autoloader --no-dev

# Permissions
RUN chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache

Step 3: Setting up Nginx

Create an Nginx configuration in nginx/default.conf:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;

    root /var/www/public;

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

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

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

Step 4: Configure Docker Compose

Create docker-compose.yml to orchestrate the services:

version: "3.8"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: laravel_app
    restart: unless-stopped
    volumes:
      - ./:/var/www
    environment:
      - DB_HOST=db
      - REDIS_HOST=redis

  webserver:
    image: nginx:1.25-alpine
    container_name: nginx_server
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app

  db:
    image: mysql:8.0
    container_name: mysql_db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
    ports:
      - "33060:3306"
    volumes:
      - db-data:/var/lib/mysql

  redis:
    image: redis:alpine
    container_name: redis_cache
    restart: unless-stopped
    ports:
      - "63790:6379"
    volumes:
      - redis-data:/data

volumes:
  db-data:
  redis-data:

Step 5: Environment Configuration

Update your Laravel .env file accordingly:

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

CACHE_DRIVER=redis
REDIS_HOST=redis
REDIS_PORT=6379

Step 6: Bringing up the Stack

Run the following command to start everything:

docker-compose up -d --build
  • Laravel available at http://localhost:8080
  • MySQL accessible on port 33060
  • Redis accessible on port 63790

Step 7: Running Artisan Commands

Shell into the app container for artisan and composer:

docker-compose exec app bash
php artisan migrate
php artisan key:generate

Step 8: Troubleshooting Tips

  • Permissions: If you encounter permission issues, check the owner/group of storage and bootstrap/cache.
  • MySQL Connection: Ensure your .env is pointing to the service name (db) as host.
  • Logs: Use docker-compose logs to check service logs.

Conclusion

You've built a production-realistic environment for Laravel using Docker Compose with dedicated Nginx, MySQL, and Redis services. This multi-container setup isolates concerns and sets a solid foundation for advanced workflows, continuous integration, and deployment.

Happy coding!


Further Reading