Deploying Laravel with Docker: CI/CD Workflow Guide

Deploying Laravel applications can be streamlined using Docker and a CI/CD workflow. This guide provides a step-by-step approach to setting up a robust deployment pipeline that ensures consistency, automation, and efficiency.

Understanding the Basics

Laravel is a popular PHP framework for building web applications, and Docker is a containerization platform that helps package applications with their dependencies. Combining these tools allows for portable, reproducible environments suitable for automated deployment processes.

Prerequisites

  • Basic knowledge of Laravel and Docker
  • Docker installed on your development and deployment machines
  • Git repository for your Laravel project
  • CI/CD platform (e.g., GitHub Actions, GitLab CI, Jenkins)
  • Docker Hub or private registry for storing images

Creating a Dockerfile for Laravel

Start by creating a Dockerfile in your Laravel project root. This file defines the environment for your application.

Sample Dockerfile:

FROM php:8.2-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    zip \
    unzip \
    git

# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install pdo pdo_mysql gd

# Set working directory
WORKDIR /var/www

# Copy existing application directory contents
COPY . /var/www

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

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

# Set permissions
RUN chown -R www-data:www-data /var/www

EXPOSE 9000

CMD ["php-fpm"]

Building and Pushing Docker Images

Automate image building in your CI/CD pipeline. Use commands like:

docker build -t yourusername/laravel-app:latest .
docker push yourusername/laravel-app:latest

Setting Up CI/CD Workflow

Configure your CI/CD platform to automate testing, building, and deploying your Laravel application. Here’s a typical flow:

  • Checkout code from repository
  • Run tests to ensure code quality
  • Build Docker image
  • Push image to registry
  • Deploy to server

Sample GitHub Actions Workflow

Here’s an example workflow file for GitHub Actions:

name: CI/CD Laravel with Docker

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: yourusername/laravel-app:latest

      - name: Deploy to server
        run: |
          ssh user@yourserver "docker pull yourusername/laravel-app:latest && docker-compose up -d"

Deploying the Application

On your server, use Docker Compose to manage containers. Example docker-compose.yml:

version: '3.8'

services:
  app:
    image: yourusername/laravel-app:latest
    container_name: laravel_app
    restart: unless-stopped
    ports:
      - 80:80
    environment:
      - APP_ENV=production
      - APP_KEY=base64:YOUR_APP_KEY
    volumes:
      - .:/var/www
    command: php artisan serve --host=0.0.0.0 --port=80

Conclusion

Integrating Docker with Laravel deployment and CI/CD workflows enhances automation, consistency, and scalability. By following this guide, developers can streamline their deployment process and focus more on building features rather than managing environments.