Shopping cart

    Subtotal $0.00

    View cartCheckout

    Containerise Laravel App with Docker: A Step-by-Step Guide

    laravel_docker_image

    Introduction

    Containerising a Laravel application has become a best practice for modern web development. It ensures seamless deployment, scalability, and consistency across different environments. Docker for Laravel simplifies the setup by packaging everything needed to run the application, including PHP, Nginx, and MySQL. In this guide, we will walk you through setting up Laravel in Docker efficiently.

    Why Use Docker for Laravel?

    Key Benefits:

    • Consistency: It works the same across all environments.
    • Scalability: You can easily scale your Laravel application.
    • Efficient Deployment: It simplifies CI/CD pipelines.
    • Quick Setup: There is no need to manually configure dependencies.
    Image depicting containerisation of laravel app

    Prerequisites

    Before getting started, make sure you have:

    Setting Up Docker for Laravel

    1. Create a Dockerfile

    Inside your Laravel project, create a Dockerfile at the root directory.

    # Use official PHP image with necessary extensions
    FROM php:8.3-fpm
    
    # Set working directory
    WORKDIR /var/www
    
    # Install dependencies
    RUN apt-get update && apt-get install -y \
        libpng-dev \
        zip \
        unzip \
        git \
        curl \
        libonig-dev \
        && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
    
    # Install Composer
    COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
    
    # Copy Laravel application files
    COPY . .
    
    # Set permissions
    RUN chown -R www-data:www-data /var/www
    
    CMD ["php-fpm"]

    2. Create an nginx.conf File

    Next, create an nginx.conf file inside a new docker directory:

    server {
        listen 80;
        server_name localhost;
        root /var/www/public;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }

    3. Create a docker-compose.yml File

    The docker-compose.yml file will help you manage multiple Docker containers for Laravel.

    version: '3.8'
    
    services:
      app:
        build: .
        container_name: laravel_app
        restart: always
        working_dir: /var/www
        volumes:
          - .:/var/www
        depends_on:
          - db
        networks:
          - laravel
    
      webserver:
        image: nginx:latest
        container_name: nginx_server
        restart: always
        ports:
          - "8000:80"
        volumes:
          - .:/var/www
          - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
        depends_on:
          - app
        networks:
          - laravel
    
      db:
        image: mysql:8.0
        container_name: mysql_db
        restart: always
        environment:
          MYSQL_DATABASE: laravel
          MYSQL_ROOT_PASSWORD: root
          MYSQL_USER: laravel_user
          MYSQL_PASSWORD: secret
        volumes:
          - dbdata:/var/lib/mysql
        networks:
          - laravel
    
    networks:
      laravel:
        driver: bridge
    
    volumes:
      dbdata:
        driver: local

    4. Configure Laravel .env File for Docker

    Modify the .env file to match the database settings for Laravel and MySQL in Docker:

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

    5. Build and Run Laravel Containers with Docker

    Now, run the following command to build and start the containers:

    docker-compose up -d --build

    6. Run Laravel Migrations Inside Docker

    Once the containers are running, execute Laravel migrations:

    docker-compose exec app php artisan migrate

    7. Access Laravel Application in Browser

    After setting everything up, open your browser and go to:

    http://localhost:8000

    You should now see your Laravel application running inside a Docker container.

    Optimising Laravel for Production in Docker

    For production environments, consider these optimisations:

    • Use Docker multi-stage builds to reduce image size.
    • Enable Laravel cache optimisations (php artisan config:cache and php artisan route:cache).
    • Use supervisor to manage background jobs.
    • Add a Redis container for improved session and queue handling.

    Conclusion

    In this guide, we explored how to containerise a Laravel application with Docker. We covered the complete setup, from defining a Dockerfile to configuring Nginx, MySQL, and Laravel. We also discussed best practices for production optimisation. By leveraging Laravel, Docker, Nginx, and MySQL, developers can create scalable, maintainable applications with minimal configuration.

    If you need assistance with Laravel and Docker, we at Techliphant can help. Our team specializes in Laravel development, Docker deployments, and scalable cloud infrastructure solutions. Feel free to reach out for expert guidance.

    For more Laravel and Docker tutorials, stay tuned and feel free to share this guide if you found it useful!

    Leave A Comment

    Your email address will not be published. Required fields are marked *