Symfony is without doubts one of the most popular PHP frameworks. It has amazing flexibility and is written in an applaudable modular fashion. Some Symfony components even power other prominent frameworks like Laravel. Running Symfony is a bit tricky at times, especially the permissions of the cache and logs folder, this blog post demystifies running the Symfony Demo App in a docker container and hosting it serverless on Google Cloud Run.
- You have done at least one project with Symfony (of course you are comfortable with PHP and composer)
- You have a good working knowledge of git and Github
- Know a fair bit of docker, maybe even multi-stage build
- Have a working Google cloud account (they give you $300 credit 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. If you are fairly new Git or Docker, please do read the above-mentioned post thoroughly.
Before going to the steps, the repo is hosted on my Github 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 repo. That is a very easy step, isn’t it? :)
2. Click on the Google Cloud button #
Then click on the
Run on Google Cloud blue button, as shown below: 02google-cloud-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
- Choose the project –
Choose a project to deploy this application
- Choose the region –
Choose a region to deploy this application, I usually choose
Then wait for the container to be built and deployed, you can see my process below:
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:
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:
- The Symfony Demo app has been dockerized, have a look at this Dockerfile and the Apache config too. The Dockerfile uses multistage build, on stage to install dependencies with composer and another one with Apache to run the application.
- If you want to run it locally, just run
docker-compose upthen go to
http://localhost:8080. 8080 is the required port needed for Google Cloud Run.
- Thanks to the Google Cloud Run button and the Cloud Shell script that does the automagic deployment.
To make the most out of this tutorial, you can fork the repo, add your own button then explore more with it.
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 software scalability with unparallel ease to make it run. I have written a much longer guide to run Laravel on Google Cloud Run too.
I would recommend Cloud Run for any pet project or even production application.