Laravel on Google Cloud Run

About Google Cloud Run

Google launch a month ago Google Cloud Run, something between Google App Engine with flexible environment and Kubernetes. With cost optimization, GAE flex can’t have 0 instance for running the service. It’s zero-op and doesn’t have the complexity of Kubernetes.

The idea is to submit a docker image that Google Cloud Run will deploy within a minute (or two).

  • Still Beta
  • Only one region available (us-central) UPDATE: Not anymore, I’ve seen european region available

The pricing model seems interesting, the ability to have 0 instance running can be useful. I’m not sure how the price would scale in heavy production load.

Cloud Run

About the sample Laravel app

  • Laravel
  • Mysql
  • Controller and Model Log. The simplest entity that I cloud think of.
  • Api route, no auth.

You can get the code on Github. Look for the cloud-run tag on the repository as the project is kind of a sandbox and can move.

This is not a Laravel tutorial, generate a resource LogController, or just the index and store as I done. The idea is to store some logs and read them.

POST http://localhost:8080/api/log content='foobar'
GET http://localhost:8080/api/log


Cloud run needs a dockerfile and an application that run on 8080 port.

  • The gcloud has a ignore file with vendor and .env in it.
FROM composer:1.6.5 as build
COPY site /app
RUN composer install

FROM php:7.2-apache
RUN docker-php-ext-install pdo pdo_mysql

COPY --from=build /app /var/www/
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY site/ /var/www/.env
RUN chmod 777 -R /var/www/storage/
RUN echo "Listen 8080" >> /etc/apache2/ports.conf
RUN chown -R www-data:www-data /var/www/ \
    && a2enmod rewrite

Few comments on it

  • I put all the Laravel project in a site/ folder, I would’t do like this anymore, it’s just a sandbox project.
  •, maybe not the best practice but it’s a test.
  • Port 8080 is a Google Cloud Run requirement.

Build the image

Let’s build the image.

You might need to setup permission, you will be prompt with a link on the google dock to setup permission.

gcloud builds submit --tag .

It will submit the build, and perform the following steps

  • Create a tarball of your sources
  • Upload them on the google servers
  • Build the docker image
  • Stock the image on a Google Registry

It should look like this

Cloud Build

Cloud Registery

Deploy on Google Run

On the cloud registry, choose the google cloud run deployment like this (or any other way)

Cloud Run

The deployment is fast.

Cloud Run

Cloud Run