Docker has become an indispensable tool in the developer world due to its ability to ensure consistent environments across different systems. By using Docker, applications run identically across various machines, from local development setups to production environments. This guide will walk you through the process of dockerizing a Laravel application with Nginx and Php-FPM, as well as deploying it to GCP Cloud Run via Cloud Build.
Prerequisites
This article assumes you already have basic knowledge of Docker and Laravel, and it is specifically targeted at those who are working with Laravel applications that need custom dependencies. We will also assume you’re using Nginx and Php-FPM to serve your Laravel application. If you’re new to Docker, consider checking out the Docker Get Started guide.
Application Setup
In this guide, we’ll begin with a default Laravel installation (version 7.x) and cover the necessary Docker configuration for hosting it using Nginx and Php-FPM.
To get started, clone the sample repository:
git clone https://github.com/dop3ch3f/laravel-docker-tutorial.git
1. Nginx Configuration
First, set up your Nginx configuration to handle requests for the Laravel application. Here’s the configuration file you can use:
deploy/conf.d/nginx.conf
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$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
2. PHP Configuration
If your Laravel application requires custom PHP settings, you can configure php.ini
. This is necessary for setting parameters such as memory limits, file upload sizes, and other custom PHP configurations.
Create the deploy/local.ini
file with your desired PHP settings.
3. Dockerfile Configuration
The heart of this setup is the Dockerfile
, which defines the image and build process for your Laravel application.
Here’s the Dockerfile to build your Laravel application:
# Use PHP-FPM as the base image
FROM php:8.0-fpm
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
nginx \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
libzip-dev \
git \
nodejs \
npm \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd zip pdo pdo_mysql
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy application files and configurations
COPY . /var/www
COPY deploy/nginx.conf /etc/nginx/sites-available/default
COPY deploy/local.ini /usr/local/etc/php/conf.d/
# Set file permissions
RUN chown -R www-data:www-data /var/www
RUN chmod -R 755 /var/www
# Install application dependencies
RUN npm install && npm run dev
RUN composer install
# Expose port 80 for Nginx
EXPOSE 80
# Set up entrypoint for Nginx and PHP-FPM
ENTRYPOINT ["/bin/bash", "/var/www/post_deploy.sh"]
This Dockerfile does the following:
- Base Image: We use PHP-FPM as the base image. Adjust the PHP version to match your Laravel version.
- Install Dependencies: We install Nginx, Node.js, Composer, and other necessary PHP extensions.
- Copy Application Files: It copies the Laravel application files along with custom configuration files (Nginx and PHP).
- Install Dependencies: Runs
npm install
for Node.js dependencies andcomposer install
for PHP dependencies. - Expose Port 80: Nginx listens on port 80, making it accessible for HTTP requests.
- Entrypoint: Runs the
post_deploy.sh
script to start the services.
4. Post Deployment Script
The post_deploy.sh
script starts Nginx and PHP-FPM services when the Docker container starts.
post_deploy.sh
#!/bin/bash
# Start PHP-FPM and Nginx services
service php7.4-fpm start
service nginx start
# Keep the container running
tail -f /dev/null
Make sure to give it executable permissions:
chmod +x post_deploy.sh
5. Build and Run Docker Image
Once the configuration files are set up, you can build and run your Docker container:
docker build -t laravel-app .
docker run -p 8080:80 laravel-app
Visit http://localhost:8080
to check if your application is running as expected.
Deployment to GCP Cloud Build and Cloud Run
Now, for the bonus section on deploying your application to Google Cloud Platform (GCP) using Cloud Build and Cloud Run.
- Cloud Build Configuration
Create a cloudbuild.yaml
file to define the build and deployment steps.
cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/laravel-app', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/laravel-app']
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'laravel-app', '--image', 'gcr.io/$PROJECT_ID/laravel-app', '--platform', 'managed', '--region', 'us-central1', '--allow-unauthenticated']
- Trigger the Build
To enable Continuous Integration and Continuous Deployment (CI/CD), set up a build trigger in Google Cloud Build. Every time you push new changes to your repository, Cloud Build will automatically rebuild the container image and deploy it to Cloud Run.
- Cloud Run Deployment
Once the Docker image is built and pushed to Google Container Registry (GCR), you can deploy it to Cloud Run. Cloud Run will manage the deployment and scaling of your application.
Conclusion
By following this guide, you should now have a fully dockerized Laravel application running with Nginx and Php-FPM. Additionally, you’ve learned how to deploy your Laravel application to Google Cloud Run using Cloud Build for continuous deployment.
Feel free to leave comments or ask questions about specific steps or configurations. Happy coding!