deploying-a-laravel-application-on-docker-with-cicd-pipelines.html

Deploying a Laravel Application on Docker with CI/CD Pipelines

Deploying a Laravel application can be a daunting task, especially when aiming for a scalable, maintainable, and efficient setup. Enter Docker and Continuous Integration/Continuous Deployment (CI/CD) pipelines—powerful tools that simplify the deployment process, enhance productivity, and ensure consistent environments. In this article, we will walk you through deploying a Laravel application using Docker and setting up CI/CD pipelines for seamless updates.

What is Docker?

Docker is a platform that uses containerization technology to package applications and their dependencies into a standardized unit called a container. This ensures that applications run smoothly across different computing environments, making it an invaluable tool for developers.

Benefits of Using Docker

  • Environment Consistency: Docker containers run the same way regardless of where they are deployed.
  • Scalability: Easily scale your applications by running multiple containers.
  • Isolation: Each container runs in its environment, reducing conflicts between applications.
  • Efficiency: Docker images are lightweight and share the host OS kernel, leading to faster start-up times.

What is CI/CD?

Continuous Integration (CI) and Continuous Deployment (CD) are practices that automate the process of integrating code changes and deploying applications. CI/CD pipelines ensure that code is tested and deployed automatically, reducing manual errors and accelerating delivery.

Benefits of CI/CD

  • Faster Time to Market: Automated pipelines streamline the deployment process.
  • Improved Quality: Automated testing catches bugs early in the development cycle.
  • Better Collaboration: Developers can focus on writing code rather than worrying about deployment.

Step-by-Step Guide to Deploying a Laravel Application on Docker

Prerequisites

Before we dive into the deployment process, ensure you have the following:

  • Docker and Docker Compose installed on your machine.
  • A basic understanding of Laravel and its directory structure.

Step 1: Set Up Your Laravel Application

If you don't already have a Laravel application, create one using the following command:

composer create-project --prefer-dist laravel/laravel my-laravel-app
cd my-laravel-app

Step 2: Create a Dockerfile

In your Laravel application root directory, create a file named Dockerfile. This file will define the environment in which your application will run.

# Use the official PHP image
FROM php:8.0-fpm

# Set the working directory
WORKDIR /var/www

# Install system dependencies
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev

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

# Copy the application code
COPY . .

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

# Expose the port
EXPOSE 9000

CMD ["php-fpm"]

Step 3: Create a Docker Compose File

Next, create a docker-compose.yml file in the same directory. This file will define how your application services interact.

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: my-laravel-app
    container_name: laravel_app
    ports:
      - "9000:9000"
    depends_on:
      - db
    volumes:
      - ./:/var/www

  db:
    image: mysql:5.7
    container_name: mysql_db
    restart: always
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata:

Step 4: Build and Run Your Containers

With your Dockerfile and docker-compose.yml set up, you can now build and run your containers.

docker-compose up -d

This command builds your Docker image and starts your application along with a MySQL database.

Step 5: Access Your Application

Once the containers are running, you can access your Laravel application at http://localhost:9000.

Step 6: Set Up CI/CD Pipeline

To automate the deployment process, you can utilize GitHub Actions as your CI/CD tool. Create a .github/workflows/deploy.yml file in your repository:

name: Deploy Laravel Application

on:
  push:
    branches:
      - main

jobs:
  build:
    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 Docker Hub
        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: your-docker-username/my-laravel-app:latest

      - name: Deploy to Server
        run: ssh user@your-server-ip 'docker pull your-docker-username/my-laravel-app:latest && docker-compose up -d'

Conclusion

Deploying a Laravel application on Docker with CI/CD pipelines streamlines your development workflow, ensuring that your application is consistently deployed and maintained. By following the steps outlined in this guide, you can set up a robust development environment that leverages the power of Docker and CI/CD, allowing you to focus on what matters most—building great applications.

As you continue to refine your deployment process, consider integrating monitoring tools and further automating your CI/CD pipelines to enhance your workflow even more. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.