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)

The pricing model seems interesting, the ability to have 0 instance running can be useful.

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

Dockerfile

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
WORKDIR /app
COPY site /app
RUN composer install

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

EXPOSE 8080
COPY --from=build /app /var/www/
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY site/.env.prod /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.
  • .env.prod, 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 gcr.io/nipsandbox/logs .

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

Test the app

It gives you an URL to test the app. Here’s the one I got if you want to test :

https://logs-rxtbe5p4eq-uc.a.run.app/api/log

You can post data if you like, I’ll turn it down sometime soon anyway.

comments powered by Disqus