DEV Community

HLonare
HLonare

Posted on

Deploy Laravel 9 on Google Cloud Run + Sql + MySql + Cloud Build

Image description

If you do not prefer to read then go on and play this video for the same:
https://www.youtube.com/watch?v=00UqiF4hqNw

If you if you learn how you can deploy your Laravel nine app on Google chakra, right? Okay, so I'm sure you're not interested in looking at my face I'm gonna remove the camera here. Right so first thing is I'm a 90 plus person sure that you might have already seen this video from code jet who actually provides the step by step process how you can do that. But the problem with this approach is it is older so basically is focusing on a Laravel version, which is not correct. And once you deploy this image, you will have this kind of waiting tddft. So basically, if your app is deployed on blue Google run with the steps you mentioned. Then you will have quite a lot of waiting time in on your app. So in order to remove that I have improved his code, obviously. And that's what we are going to learn today. Okay. Right. So couple of things I wanted to mention before we start our journey is right. So the first thing what we are going to do is we are going to create a cloud based GCK GCP.
SQL instance for my sequel and then we will be creating GCP cloud run and obviously, one of our local instance for the app will be running on our local machine, but it also depends on this G CQ SQL instance because it is connected directly with that instance. So this this approach, this approach is good if you're just one developer or you're the only owner of the startup and you're running it by yourself, and you have very less time to go through you know change of code on your sequel side of architecture, etc. But obviously, whenever you want to make changes, you just make changes on this database structure and then it reflect on your local instance as well as on your GCP instance. And then you can push your code directly from here to through you know, your GitHub, and then your GitHub bill, just push or GCP will just bring that code from your GitHub, and then it will update the app code here. And then you can utilize it. Now a couple of things I want to mention is one GCP does not allow volumes in your Docker Docker files. So basically, you cannot create volumes if you create volumes it will not work on your GCP so make sure when you drop your Docker file, you don't have volumes. Okay, so let's get started. So the first thing you want to do is you want to go to the SQL in Well, the first thing you want to do is go to your Google Cloud Platform dashboard. And then you go to your SQL. And then here on your SQL dashboard, you click on Create Instance and I'm going to use the MySQL ones.
Call it
nine SQL, or you can name it whatever you want. You generate the password. You select the version you want. I prefer my SQL eight because my app is fresh and new. And then you select the region, whichever region you want to select. And then you can click on Create Instance once you do that, your instance will be created. And then you have an instance where you will get you know server IP, the connection name, etc and the password. And then after you do that, you go to the connection side and then you allow the IP address of your local machine or your the IP address of your cloud GCP instance so this is how easy it is. So you can create this sequel here. Now let's go to the next part, which is how you create the Docker file. So for example
this is the code so the first thing you want to do is you want to create a file where you will start with PHP 8.1 Female fine, then exactly same as the other video you can run the APK make a directory for nginx and then copy your Docker engine X configuration to your Docker engine X configuration. Now this is important because that is been updated. So inside the Docker folder, the engine X file. This configuration has been updated to run on GCP and to reduce the time to first byte delay. And then once that's done, you copy the whole code into the app. So you must be wondering why there are two lines. Well, that's because you can have these Docker file and Docker folder with all this configuration from the GitHub library, which I'm going to share with you later. And then your app could be installed inside the source folder, and that that's why it's important that you copy your source as well as the Docker file in the app. folder. Once that done, you install the composer and run it. And that's pretty much it you you know assign a different user to the app, and then you run the startup process. Now, again, you might have seen it that it's a startup.sh app.
W video, it was constructed that we should wait until the PHP FPM daemon is active, but that will cause a lot of delay. So I have commented it out in my version. So you can use it and it will be super super fast. Okay. Now before creating your Docker image, I would advise you to install your laravel app in the source folder because if you remember if you go to your our Docker file, what we are doing is we are copying our source image to source code from src folder to app and we're also copying the Docker files and configuration to that. So in order to do that, we just say composer create project
and then don't forget to put this dot backslash SRC or as it will throw a permission error. Okay. Once you do that, your project will be installed under the source directory. Perfect. Okay, so So the first thing you want to do is you pedicure so Docker image tag name you want to tag it with
Docker on line 10 And then the location and it will start building your image. Once the image is build, you are ready to run this image. Now, remember one thing that we are configuring the forward of the provided port number and then copying it into your nginx configuration file. So you cannot run this from your Docker interface. For example, if you have a goI installed and you want to go to the image and click on Run, it will not work because it needs that port ID. So what I would suggest is you just providing your environment variable code to a tee and then your mapping of four to get the for the container and for your browser. And then you provide the name of the image. And this is how you start running it. So there you go. Your instance of your Laravel nine app is up and running. Now when you go to your browser and see go to localhost. You will see the local image I have changed the index file. That's why you're looking at it and it looks like this, but it should look like a normal page when you load it on your local machine. Okay, so this is it. Once this is done, you need to create your GitHub repository. And once you're done with creating the repository, you push the code to the GitHub repository here. So that's very simple. You can go through this article and see how you can push it. So once this is published, you go to your Google Cloud dashboard and then go to cloud
to when you're here, you click on Create Image. And then instead of using the deploy one division from existing container, you select the continuous installation, and then you name the service so I would name it learn all 910 select the region, then this is new, you have to always select one as an auto scaling option for as minimum and maximum you can select as much as you want. And then obviously you want the public access to your app. So but then you have to set up your cloud builder. So so what you need to do is you have to connect your GitHub here, I'm already connected. So I'm gonna just do it like that. And then you don't have to provide any advanced option here. It's already taken care of and then click on Next. And then in the Docker file, you just select this Docker and automatically adopt the Docker file, click on Save.
Now when you are doing that, when you're creating this, and it's done, remember, click on this arrow, and then go to variable and secrets and add all the variables of your laravel app here. For example, your company
app and moment.
Your
URL, etc, etc. Plus your database credentials also. So remember the database credentials you got from the SQL that is what you have to copy here and provided. Basically, the environment file is not been copied from your GitHub to your Google Cloud run container because I think this is one of the security feature which Google Cloud run has and that's why you have to provide all these environment variables here. If you don't provide them and you create, click on Create button. It will throw a lot of error. It happened with me I mean, I can show you how many fail runs I had so you can see how many times the trigger and Cloud has failed when I was trying to do that, okay, so remember that so once that's done, and you click on your cloud
then your app is started running, it will build up. And then you can go to this link by clicking on this and then you will see your app working very, very fast. So let me show you how fast it is running. Okay.
Okay, so I'm gonna click on Command R to refresh it. And then you see the app is running pretty faster.
In terms of timing, the time to first byte is just 262 milliseconds. So that's pretty good. And this is what we want our app to run faster. Okay. Now let's test how this continuous integration will work. Okay. So currently on the app is looking like this. So what I want to do is I want to go to my source code, and I want to go to my sources, views and welcome plate and this is my welcome. Fine. So I've been using CloudFlare, tailwind two point 2.9 tene CSS, so this is here.
So I wanted to do and instead of patch one I wanted to I want to do h1. So they saved right. This is saved, so I will say
quiet and then I'll just say push, and it will push all the objects to the GitHub. Now, if you go to the GitHub and refresh, you will see 11 seconds ago that there is a date file updated. And then when you go back to your Google Cloud Platform and click on the instance, you will see that it's pending because it's building right now. Yeah. And in order to see what's the status, we can go to cloud build.
And you see that this has already started building great by second has been passed. And then task has been running here. And it should be done quite quickly because it has been done once before. So it should have quite a lot of goodies, which is required to run Laravel nine available in cash.
Some hoping that it will run quite fast. The last one took one minute 52 Second, some guessing Mitch take at least around that much time. And remember guys, Google Cloud Platform cloud big provide you collecting 120 or 100 minutes every month for free and then later on if you using anything beyond that time, then it will cost you but it is not like other cloud providers where you will see that you have to pay if you have to get up you will get a trial period. and then for if you use it on rock, don't use it. you have to just pay a monthly cost. it's just pay as you go. so that's brilliant. So as you can see it's built up and it's successful. So now I'm gonna go back to my children.

Top comments (0)