Deploy a Laravel Application on Google Cloud Run

In this tutorial, I am going to tech you how to deploy or upload your Laravel 8 (PHP) application to Google Cloud Run.

Docker Containerization

Google Cloud Run is a serverless hosting that stores applications through docker containers, so you will need to create the necessary docker files so that your laravel application can run on Cloud Run.

Dockerfile

This file will be executed when you deploy your application to Google Cloud Run and it will generate the container where your application will run, so we must write the necessary commands to create the docker server in Cloud Run.

In this case we will create an apache server with a configuration file located in the /docker/000-default.conf folder which will expose the port 8080 of our server and this will allow our website to run on that port.

On the one hand, the dockerfile file will copy our app in a temporary folder called /app and install the necessary dependencies using composer to later create the apache server with its respective dependencies and copy our temporary folder to the main folder of the server /var/www as well as our configuration file for our website.

Finally, port 8080 will be exposed and the necessary permissions will be enabled, such as writing the /var/www/storage folder that corresponds to the storage of our application.

In the next code, I show you the dockerfile that you must add to the root of your application.

FROM composer:2.1.10 as build WORKDIR /app COPY . /app RUN composer install && composer dumpautoload FROM php:8.1.0RC5-apache-buster RUN docker-php-ext-install pdo pdo_mysql EXPOSE 8080 COPY --from=build /app /var/www/ COPY docker/000-default.conf /etc/apache2/sites-available/000-default.conf RUN chmod 777 -R /var/www/storage/ && \ echo "Listen 8080">>/etc/apache2/ports.conf && \ chown -R www-data:www-data /var/www/ && \ a2enmod rewrite
Dockerfile

Additionally, the configuration file for the apache server that you must place in the folder /docker/000-default.conf of your app.

<VirtualHost *:8080> ServerAdmin webmaster@localhost DocumentRoot /var/www/public/ <Directory /var/www/> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
000-default.conf

Deployment

To deploy or upload our container to google cloud, you will only have to install the google cloud suite on your computer and follow the steps that will appear when executing the following command in the root of your project once you have logged into your account.

gcloud run deploy --source .
Command to Deploy in Cloud Run

If you do not know how to login your account from the console, you can execute the following command and enter the data of your Google Cloud account

gcloud auth login
Command to Login to Google Cloud through the Console

If you want your application to be deployed using integration and continuous deployment CI/CD, you can follow the tutorial on ¿How to Configure a Cloud Run Service with Cloud Build for Integration and Continuous Deployment?

MySQL Database

Once you have deploy your Laravel application, we are going to create and connect the MySQL database.

If you have not created your database yet, I recommend you follow this tutorial about ¿How to Create a MySQL Database on Google Cloud? and also, this other one about ¿How to Connect a MySQL Database to a Laravel Application within a Cloud Run Service?

Important

Each container version is independent from the others, so the changes that you generate in files within the container will only be available in the container, so if your application allows the uploading files to your server, all of these must be manage for Google Cloud Storage.