Dockerizing laravel application with nginx and php-fpm

Updated: Feb 18, 2025

Dockerizing laravel application with nginx and php-fpm

To Dockerize a Laravel application with Nginx and PHP-FPM, you'll need to create several Dockerfiles and configuration files. Here's a step-by-step guide to help you get started:

  1. Create a new Laravel project:

    composer create --prefer-dist laravel/laravel project-name
    
  2. Create a new directory for your Dockerfiles and configuration files:

    mkdir docker
    cd docker
    
  3. Create a Dockerfile for your Laravel application:

    touch Dockerfile
    

    Open the Dockerfile in your favorite text editor and add the following content:

    FROM php:7.4-fpm-alpine
    
    RUN apk add --no-cache --update \
        zip \
        unzip \
        curl \
        git \
        && docker-php-ext-install pdo pdo_mysql zip \
        && apk del apk --purge \
        && rm -rf /var/cache/apk/*
    
    WORKDIR /var/www/html
    
    COPY . /var/www/html
    
    RUN chmod -R 775 /var/www/html
    
    EXPOSE 9000
    
    CMD ["php-fpm"]
    

    This Dockerfile uses the PHP 7.4 FPM Alpine image as the base image and installs the necessary dependencies and extensions for Laravel. It also sets the working directory to /var/www/html and copies the contents of the current directory to that location.

  4. Create a Dockerfile for Nginx:

    touch nginx.Dockerfile
    

    Open the nginx.Dockerfile in your favorite text editor and add the following content:

    FROM nginx:alpine
    
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY . /usr/share/nginx/html
    
    EXPOSE 80
    
    CMD ["nginx", "-g", "daemon off;"]
    

    This Dockerfile uses the Nginx Alpine image as the base image and copies the nginx.conf file and the contents of the current directory to the Nginx HTML directory.

  5. Create a nginx.conf file:

    touch nginx.conf
    

    Open the nginx.conf file in your favorite text editor and add the following content:

    worker_processes 1;
    
    events {
        worker_connections 1024;
    }
    
    server {
        listen 80;
        server_name localhost;
    
        location / {
            index-index.php index.php;
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    This configuration file sets up Nginx to listen on port 80 and serve PHP files using the PHP-FPM server running on port 9000.

  6. Build the Docker images:

    docker build -t project-name .
    docker build -t project-name-nginx .
    
  7. Run the Docker containers:

    docker run -d --name project-name-php project-name-php
    docker run -d --name project-name-nginx -p 80:80 project-name-nginx --link project-name-php:php
    

    This command starts the PHP container and the Nginx container, and links the PHP container to the Nginx container so that Nginx can communicate with PHP-FPM.

  8. Access your Laravel application: Open a web browser and navigate to http://localhost. You should see your Laravel application running.

That's it! You've successfully Dockerized your Laravel application with Nginx and PHP-FPM. This setup provides a lightweight and portable environment for running your Laravel application.