LARAVEL

Laravel Docker Deployment Guide

April 1, 2024 20 min read

Introduction

Docker provides consistent environments for Laravel development and deployment. This guide covers containerizing Laravel applications from development to production.

Dockerfile Setup

Laravel Dockerfile

# Dockerfile
FROM php:8.2-fpm

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

# PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

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

# Set working directory
WORKDIR /var/www

# Copy application
COPY . .

# Install dependencies
RUN composer install --no-dev --optimize-autoloader

# Set permissions
RUN chown -R www-data:www-data /var/www \
    && chmod -R 755 /var/www/storage

EXPOSE 9000

CMD ["php-fpm"]

Nginx Dockerfile

# nginx/Dockerfile
FROM nginx:alpine

COPY nginx/default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Docker Compose

Development Setup

# docker-compose.yml
version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    volumes:
      - .:/var/www
    depends_on:
      - redis
      - mysql

  nginx:
    build: ./nginx
    ports:
      - "8080:80"
    depends_on:
      - app

  mysql:
    image: mysql:8.0
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
    volumes:
      - mysql_data:/var/lib/mysql

  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

volumes:
  mysql_data:

Development Setup

Development Workflow

# Build and start containers
docker-compose up -d --build

# View logs
docker-compose logs -f app

# Run commands
docker-compose exec app composer install
docker-compose exec app php artisan migrate

# Stop containers
docker-compose down

Production Deployment

Production Docker Compose

# docker-compose.prod.yml
version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile.prod
    environment:
      - APP_ENV=production
      - APP_DEBUG=false
    restart: unless-stopped

  nginx:
    build: ./nginx
    restart: unless-stopped

  mysql:
    image: mysql:8.0
    restart: unless-stopped
    volumes:
      - mysql_data:/var/lib/mysql

Production Dockerfile

# Dockerfile.prod
FROM php:8.2-fpm-alpine

# Production optimizations
RUN apk add --no-cache \
    nginx \
    supervisor

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

WORKDIR /var/www

COPY . .

RUN composer install --no-dev --optimize-autoloader \
    && php artisan config:cache \
    && php artisan route:cache \
    && php artisan view:cache

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

Optimization

Docker Optimization Tips

  • Multi-stage builds - Reduce image size
  • Layer caching - Order Dockerfile commands optimally
  • .dockerignore - Exclude unnecessary files
  • OPcache - Enable PHP OPcache
  • Supervisor - Manage queue workers

.dockerignore

.git
node_modules
.env
.env.*
!env.example
vendor
storage/*.key
*.md
docker-compose*.yml
Dockerfile*
.idea
.vscode

Summary

Docker provides consistent, reproducible deployments for Laravel applications. Proper setup enables easy scaling and management in production environments.

For more Laravel tutorials, check out Laravel Performance Optimization and Laravel Queues.