How to run Symfony on Google Cloud Run with the demo app [Step-by-Step]
Tags: php, webdev, symfony, cloudrun
Published: 2019-11-03 04:57:01

Symfony is without doubts one of the most [popular](https://raygun.com/blog/top-php-frameworks/) PHP frameworks. It has amazing flexibility and is written in an applaudable modular fashion. Some Symfony components even [power](https://symfony.com/projects/laravel) other prominent frameworks like Laravel. Running Symfony is a bit tricky at times, especially the [permissions](https://symfony.com/doc/current/setup/file_permissions.html) of the cache and logs folder, this blog post demystifies running the Symfony [Demo App](https://github.com/symfony/demo) in a docker container and hosting it serverless on [Google Cloud Run](https://cloud.google.com/run/).

## Prerequisites

1. You have done at least one project with Symfony (of course you are comfortable with PHP and composer)
1. You have a good working knowledge of git and Github
1. Know a fair bit of [docker](https://geshan.com.np/blog/categories/docker/), maybe even multi-stage build
1. Have a working Google cloud account (they give you [$300 credit](https://cloud.google.com/free/) free for 1 yr, no reasons not to have an account)

This post is not going to be a detailed one like the last one about [Laravel on Google Cloud Run](https://geshan.com.np/blog/2019/10/get-laravel-6-running-on-google-cloud-run-step-by-step-with-ci/). If you are fairly new Git or Docker, please do read the above-mentioned post thoroughly.

## Steps

Before going to the steps, the repo is hosted on my [Github](https://github.com/geshan/symfony-demo-google-cloud-run) and it is open-source of course. It might look insanely easy but we will go through some details after the steps. So let’s get started now:

## 1. Go to the repo

Navigate to the open-source [Github](https://github.com/geshan/symfony-demo-google-cloud-run) repo. That is a very easy step, isn’t it? :)

![Repo with Dockerized Symfony Demo App](https://cdn-images-1.medium.com/max/4208/1*7T2nPc6ezWxkxYtx2PM9Kw.png)*Repo with Dockerized Symfony Demo App*

## 2. Click on the Google Cloud button

Then click on the Run on Google Cloud blue button, as shown below: 

![Click on the button](https://cdn-images-1.medium.com/max/4072/1*13jCwq2Ko6k-IyJPGPo0Yg.png)*Click on the button*

## 3. Choose correct settings on Google cloud CLI

After that, given you are logged into your Google account and have Google cloud setup with 1 or more projects, click Proceed. You might need to wait a bit, after that

1. Choose the project — Choose a project to deploy this application
1. Choose the region — Choose a region to deploy this application, I usually choose us-central-1

Then wait for the container to be built and deployed, you can see my process below:

![Deploying Symfony Demo app with Google Cloud shell script](https://cdn-images-1.medium.com/max/4216/1*OJCDDsGJ6TobAYR1YesgjA.png)*Deploying Symfony Demo app with Google Cloud shell script*

If everything goes fine on your Google Cloud Shell, you will see HTTPs URL you can go to in order to see your Symfony demo app running like below:

![Symfony Demo app running on Google Cloud Run](https://cdn-images-1.medium.com/max/4204/1*IWY9h9afx8v8CWJFADHHZw.png)*Symfony Demo app running on Google Cloud Run*

One issue with this setup is that data will not be saved if you click around and add new posts. This happens because the data is saved in the SQLite file, as it is not persisted new containers get the old file and you get the same data pushed in the repo.

## So what just happened?

There are some things you need to consider for this automagic deployment of the Symfony App. Let’s look at what just happened above:

1. The Symfony Demo app has been dockerized, have a look at this [Dockerfile](https://github.com/geshan/symfony-demo-google-cloud-run/blob/master/Dockerfile) and the [Apache config](https://github.com/geshan/symfony-demo-google-cloud-run/blob/master/docker/000-default.conf) too. The Dockerfile uses multistage build, on stage to install dependencies with composer and another one with Apache to run the application.
1. If you want to run it locally, just run docker-compose up then go to http://localhost:8080. [8080](https://cloud.google.com/run/docs/reference/container-contract) is the required port needed for Google Cloud Run.
1. Thanks to the [Google Cloud Run button](https://cloud.google.com/blog/products/serverless/introducing-cloud-run-button-click-to-deploy-your-git-repos-to-google-cloud) and the Cloud Shell script that does the automagic deployment.

To make the most out of this tutorial, you can fork the [repo](https://github.com/geshan/symfony-demo-google-cloud-run), add your own button then explore more with it.

## Conclusion

Running Symfony entails some tricky configuration :) so the Dockerized version is a breeze to run. I really believe that Google Cloud Run is a superior product if you know how to dockerize your web app and run it on port 8080 you can get amazing scalability with unparallel ease to make it run.
> *I would recommend Cloud Run for any pet project or even production application.*

---

*Read more at [https://geshan.com.np](https://geshan.com.np/blog/2019/11/how-to-run-symfony-on-google-cloud-run-with-the-demo-app-step-by-step-guide/) *