Dockerize and Deploy to Google Cloud Platform (GCP) - Rest API with NodeJS and MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn how to deploy our nodejs application to a live server we're going to use Google Cloud platform as the hosting provider but the process is pretty much similar with other services like digital lotion or any other Linux hosting provider all right so let's get started hey guys it's cor rosam here and this is the part 13 of the series building a rest apia with nodejs and MySQL so if you are new here make sure to subscribe and stay with the channel to follow the rest of the series some new stuff is very soon to be released a new type of content so stay with the channel to make this 2024 the best year for you as a developer all right so let's get started if you've been following this course from the beginning we have covered so much regarding rest apis with njs we learn how to connect to database how to implement crud relationships between tables and so much so watch it please make it your handbook it covers pretty much everything so now we are in the last few parts of this series and today we're going to learn a very important one which is deployment well it sounds scary well it is but now it has become so much easier so today we're going to learn how to do it all right so let's open up our project and let's start deploying our application all right so here is what we're going to do in this deployment process first we going to dockerize our application this make our lives so much easier since we don't have to install anything other than Docker on the server if you are not using Docker we going to have to install uh for example nodejs my SQL and SQL I CLI and also some other Process Management tool like pm2 or something and also we might end up having some you know version problems and so many troubles so dozing your application is the way to go next we're going to set up a server on Google Cloud platform this is very important if you have no previous knowledge in setting up a server the reason I chose gcp is that they offer $300 free credit to get you started so it's very generous of Google uh and uh and the process is also very straightforward and and finally we're going to push our code to the server using git and get our application up and running so these are the steps we're going to follow in this tutorial so before dockerizing our application let me quickly show you the project overview uh we are developing a rest API with nodejs and we are using Express and as the orm we are using SQL eyes so we are connected to a mySQL database um we are we are developing a blogin system so we have post and we have users who create the post and also we have comments uh that users can add on the post and also we have images uh post authors can upload images to the post so that's basically uh the functionality of of this project so we have a equalized configuration here and we have the controllers and we have few one helper file uh for image uploader and we have middleware to uh check the token if the token is valid and we create all the tables using migrations and here are our models and also um we have some CED files as well to generate the categories all right so the functional device this is a simple rest API uh you don't have to worry about the functionality we are focusing on deploying this application so it works on pretty much um any application any type of application all right so let's create a doer file to start dockerizing our application all right so if you are new to Docker I have another video to explain everything about doer from the scratch so you might want to check that out so that you will understand so everything I Define in this doer uh doer file and doer compose file so what's happening under the hood and uh what is the logic behind everything you will understand everything uh if you watch that video I'll link it in the description down below so otherwise you can follow along with me all right so let's start populating our doer file first we need to uh get the image uh in this case we need the image of node Alpine just like this and then we need to set the work directory let's put that as user Source in the app then we need to copy everything in our project folder to our work directory we we are skipping some files and folders um like this idea directory and also this um node module folder and U some other files uh so other than uh we're going to copy everything inside this folder to our work directory so first let's start copy our package.json file and and then we need to copy uh package lock file as well all right so then what we need to do is uh we're going to copy other files as well but uh before that let's uh add at this command npm install so as soon as we copy these two files we going to in uh run npm install command so the all the dependencies will be installed and then we need to install um uh cqal CLI so that is the one we are using to run all the uh migrations and everything just like this so these uh two commands will everything uh all the dependencies and caliz CLI all right so let's start copying other files and folders uh this uh we need to copy this config folder just like this then the controllers so if you are new to nodejs and this rest API concept you might be wondering uh what these files and folders are and what's the logic behind uh everything we uh we have created here in this uh project so I strongly suggest you to follow along with me uh from the beginning of this series it's it's pretty straightforward uh series I teach you everything uh behind uh all the concepts and it's very easy to follow along we are building this project from the scratch then the migrations and you just need to keep copying all the files and folders in the rot directory of the project and then the models folder by the way if you want more understanding on uh what we are doing here if you can watch my other tutorial regarding dockerizing a nodejs application by the way if your project is structure is something like you put all the source files inside a separate folder and that folder is the one inside this project uh root directory you just need to copy that folder since we don't have that kind of project structure we just need to copy everything uh every single file writing this copy command so that's something to consider when you are start developing the project then we want to copy our EnV file then some other SQ uh configuration file we use this file to tell equalize that uh since now we have an uh EnV file to Define all the database credentials and everything uh we tells equalize that uh don't look for your usual config.js file instead uh use this file well I think I should do a separate video on how to add an En file to the project uh rather than using uh ciz default configuration file by the way it's been a while since uh we started this series so if you started watching this series recently I strongly suggest you to use typescript rather than JavaScript to de this project with typescript it will be so much easier and you will be able to use new es6 uh syntax as well all right so we we wrote commands to copy all the files in our project all the necessary files and um next uh what we are going to do is we need to set the command to run this application so let's uh add this CMD command and here we you want to run npm start all right hit save so so the doer file is complete well next up we need to create a doer compost file well you know in this application to run this entire ecosystem we need to run at least two Docker containers one Docker container is for our application and the other Docker container is for our database so to run multiple Docker containers we need to have a Docker compos file so that so that all the containers can start on its own and continue running all right so let's create a new new file called Docker Dash compost and this is a ml file so uh I'm going to use this y ml uh extension all right so what we going to write here the first property we're going to add here is version so basically we use version three that's the uh latest version and then we need to Define services so in this case we need two Services which is the database service and also the application service so as the first service let me add a command DB service and uh let's name this service as DB service and let's name the container container name and I'm going to name that database container and to create this container we need an image the image is since we are using a mySQL database my SQL latest so we are telling dogger that to create this container pull an image of my SQL uh latest version and let's add this uh restart property we want to restart the database Service uh just in case if it fails uh always the next property is ports so this is where we Define uh all the ports that we want to run our service so we need to Define both external Port which is uh3 06 and also the internal port and next volumes and this is also for uh the persistent storage let's say our database service the docker container stops or restarted or deleted we don't want our data to be lost right so the persistent storage for um keeping our data permanently uh on our local disk so I'm going to use this uh DB data directory on my project directory and uh I have to use this this exact path for our uh Docker container directory that's where uh the data is kept uh from my SQL image all right then we should Define our environment uh variables which is is the database um database name database username and password so whenever we start this service we want to assign those values so that our application can connect to the database using those credentials so let's define uh this environment property um the first one is y SQL root user and we can get these environment values uh from our EnV file we have defined here so let's copy that and I'm going to use this dollar with curly braer syntax so that I can uh get that value straight from our EnV file just like this uh then the my sqq root password then the uh database name well I forgot to mention that uh in this em file as you notice the database host is this DB service which is um the service name of our uh database service Well normally we keep our database host if you are uh using my SQL Server we keep it like Local Host but in this case we are since we are running in Docker containers we should uh keep the DB host as the database service name so in the docker container file copy this uh DB service name and put it right here so that it can connect to uh the database service if you keep uh the DB host as Local Host the app will search for the local host of that particular container so that's why we keep the DB host as uh DB service all right so that's it for our database service next we want to Define another service for our application I'm going to name that app and I'm going to name the container and I'm going to name that app container so we have database container and app container and for this also we need to keep this restart property as always and then I'm going to use this build property so we want to tell doer that the built context is current directory then we want to define the ports for our application so for this we can use any port we want uh I'm going to use port 3,000 for our local machine and uh Port 3,000 for the doer container Port so for 3,000 will be used uh on our local machine we can access the application using this and the application will run inside the doer container using Port 3000 and then the depends on property so this means we want to depend our application on uh we want to Define this DB service here it's because our application container uh will be useless without this uh database service so we won't put tell doer that first start this DB service then after that start this application service because this application service depends on DB service all right so this is pretty much it for our application coner and uh these two services are enough to run our application with all the ecosystem of our application um but I'm going to define a thir third service here like I said these two services are enough to run our application but you know for the first time this application is running the database is empty right we want to create these uh tables using these migrations so we can simply connect to the uh container and run these uh migrations using SQ migrate command but I want to uh automate that so what I'm going to do is I'm going to define a third service here and as soon as this DB service is up and running uh I want to uh run our third service to uh run migrations so that the tables if they don't exist will be automatically created so it's very easy for uh deploying all right so let's define our third service which is I'm going to name that migration and the container name I'm going to name that myror something like that you can name it uh with whatever you want and the build cont is the current directory and this should also depends on depend on our database container oops I have a tyer here okay all right and then I want to add a shell command so that the shell command will run the migrations I'm going to add this command property just like this and well I'm going to add a shell command here but I have no knowledge of shell commands and everything uh so what I did was I generated this uh shell command using chat GPT um so let me face it down here all right so what this does is uh the first part of this shell command is a loop uh this n means it's a netcat command and it keeps trying until it can make a successful connection with our database service so once the connection is successful it runs this uh SQL migrate command the usual SQ migrate command so that's what happened uh in this shell command so I generated this um using uh chat GPT pretty cool right all right so hit save I think we are pretty much done with our do compost file let me double check uh these values are correct uh looks good so why don't we open up the terminal and let's run this let me run Docker compost up hit enter and it build in image yes still running it's still not the migration run yes migrations are also successfully uh completed as you can see let me open up uh the DOA desktop and yes we are running our application with all the services our database container is up and running and our app uh app container is also up and running this migrator started and it did it uh part which is running migration and it stopped all right so let's open up our web browser and let's see if our application is accessible using the web browser yes we can simply access our application using the web browser using this uh Local Host uh and Port 3000 all right cool so what's next so we successfully dockerized our application and so the next part is hosting our application on a real server so we are using uh gcp Google Cloud platform as the hosting provider so let's open up uh Google Cloud platform and uh let's create a project so if you have no previous experience on gcp don't worry uh I'll walk you through step by step so first of all when you first sign up for gcp you will see a screen like this then what you need to do is you need to create or select a project so since um I have few existing project but I'm am going to create a new project so first you need to add a name for the project it so let me add a name and hit create so it takes some time to create the project all right so project is created then uh you have to select the project and then you are taken to this uh this screen where you can uh see lot of configurations and options in in this uh project so next um what you need to do is in your project you need to go to this section called compute engine and in that Sub menu we have a section called VM instances so go to that and here you can create a new uh VM VM in the sense virtual machine so in for this project you have to enable this uh compute engine uh API so let's hit enable it's taking some time to create it all right so the API is enabled now you are taken to this page here you can create a new uh instance and here you can see U there are lot of options that you can select for your uh virtual machine so you can add a name I'm just going to keep this uh like this and you can select the uh region for your uh VM the data center uh region and also the S I'm going to keep uh these values as default and then you can select the uh the machine information like what kind of performance what kind of specs the machine uh should have for your case so you can see the CPUs uh the memory and the platform and everything you can uh see all the details here so in our case uh we just need to run a small rest API with Docker so we don't need uh much resources so we don't need a high performance computer we just need a uh day-to-day Computing uh machine so I'm just stick with this E2 instance type so it's automatically selected for me and there are some other configurations as well um so I'm just going to keep them as default and and for this um firewall section I'm going to take these check boxes uh allow HTTP and htps traffic and all right so let's hit create and it takes some time to uh fully configure and get the server up and running all right so the VM is created and it's uh already uh started and up and running all right so now what now we need to access this server using SSH well gcp provides a browser based SSH client that you can simply click this uh button and it's open up uh in a new window you can simply use the browser to uh SSH your um server so you need to sign in uh with your Google account and it's yeah you are taken to your server so if you are running any Linux commands like this so you are in the currently home of your user all right so well I'm not going to use this instead I'm just going to use a local SSS client uh so in this case I'm going to use puty you can simply uh download puty and from their official website and I already have uh downloaded and install it so I have that uh with me so I'm not going to redownload it so you can simply download it from their official website so now we need to generate a SSH key so that we can um add a key pair to our local machine and also to the server so it will generate two keys which is one is private key and one is public key so the public key is for your server and private key is for your local machine so these two keys are mathematically connected so the server knows that this is a legitimate authenticated machine all right so uh when you are installing puty a new another software called puty gen will be installed so you can simply search puty gen and it will open up so here you can generate a new SSH Key by simply clicking this generate button and now it's asking me to move your mouse pointer uh so that it can get some random inputs for generating the key all right so um so the key is generated now as you can see you can Simply Save the public key and private key and also the public ke is displayed here so what I'm going to do is in this uh key commment section I'm going to enter my username which I want to create uh so in this case I'm going to add my username Cod rosom and all right so now uh you can save the public key and make sure you uh save your key inside this uh default directory which is uh C partition and inside this users and then the username and inside the username uh SSH directory in my case uh my username is just user this should be your username in your local machine so this is the default location for storing SSH keys in Windows all right so inside here and let's add some name let's say gcp 3 to identify it is public key I'm going to add this uh Pub extension uh it's optional and hit save and uh also you can Simply Save the public uh private key and it's asking me whether you are saving this private key without a PR space you can simply add the pr prace here so you can make it more secure I'm just going to keep it blank ccp3 all right so the public key and private keys are saved now uh you should copy this public key and let's add this to our uh VM in gcp right so go inside this uh VM and click this edit button all right so now we are inside the uh edit section of our VM and in this SSH Keys section you can see there are few other uh SSH Keys uh added by Google by default you can simply add a new item and paste down your SSH key we just created hit save all right so we have uh successfully added our SSH key to our VM now we can simply open uh let's close this uh let's let's open puty so this is the default screen for puty when you are opening it for the first time now what you need to do is uh simply copy the external IP address of your VM you can simply copy it here and paste it down here in this host name section and uh add your username code rosom and at so the host name should be Cod rosum at then the IP address next what you need to do is inside this connection tree inside this SSH section expand that and you can find this o uh subsection and inside that there is a section called credentials and here there is a section called private key for authentication you can simply click this button and browse for your private key here now you can simply click open and it will start the session but I'm going to Simply uh save this session so that I don't want to enter these credentials again gcp Tre hit save all right now you can simply click this open button and it will open up the SSH client and now we are inside our all right simply enter some rux commands all right so now we are in the home directory of our uh user in our VM we just created all right so next let's push our code to our repo so that we can clone it to our uh newly created server so let me open up the project and commit these files and push it to uh our repo let me add a comment message like localized right all right so now let's open up our server again and let's install git on our server all right so first let's update our uh package index sudo appt update and this will update our package index and next let's type sud sudo sud sudo at install git hit yes and this will install git on our uh server to make sure git is installed type git D- version and it returns uh Git Version so uh git is successfully installed all right so what's next uh before cloning our project let's install Docker on our um on our server to install Docker let's uh simply update our package index Again by simply typing sudo app update all right so I'm following this tutorial from digital version uh so that it has all the commands uh I need to install all the packages related to Docker I will will add it in the description down below so that you can simply uh copy and paste and follow along with me well there are some commands that I have no idea of so uh you can simply uh follow the tutorial uh from this uh digital oan uh website all right so next I think uh we have another command to run and looks like we are getting there I think this will add the um do repository all right and hit yes and that will install DOA I guess all right there are some commands that I have no idea of since I'm not much uh Linux expert keep going keep [Music] going and yes all right so and this tells us uh doer is up and running so our installation is successful all right so what's next now we have Docker and git in place in our server now let's clone our project project to our uh preferred directory let me clear this all right so now we are in our home directory I'm going to create a new folder by typing mkd and and the folder name is um let's say blog now let's type LS and it shows me the newly created directory blog now let's go in inside that uh folder by typing CD block and now we are inside our uh new folder all right so now what now let's clone our repo here so this is my repository um I'm going to copy this uh URL and uh let's clone it to our server to this directory to do that I'm going to run uh uh git clone and paste down the URL I just copy it hit enter and it will clone our repo all right now what all right so the project is ready let me just type this LS command and make sure uh we have cloned our project and let's go inside our project directory which is blog API nodejs and MySQL and here we have all the files but our EnV file is not there we need to create it manually because we don't uh add en files to repost so let's uh create Pudo Nano then the en EnV file name and hit enter and this will open up the Nano editor here okay let's open up our project uh and open up en file and copy this values and paste on our uh new EnV file and to save this hit control o and hit enter again and it will uh save your EnV file to make sure your unu file is saved type cat and then the file name so uh our file has been updated now we can see all the content of our uh EnV file all right so the EnV file is created now we are ready to uh run our application on our server so to run our application doer compose up and it says uh permission denied uh why don't we just uh add sud sudo at the beginning of this sentence and now it's pulling and creating all the images currently the database service is uh getting created and the images are getting built and now the npm installation is uh going on the packages are getting installed and the C CL is getting installed all right so the containers are created and let's see if the migration are running all right so the migrations are also have uh completed running so we have a success response all right so our services up and running so why don't we open up our browser and let's see if our services are up and running so open up the browser and uh copy your external IP and paste it here and since we are using Port 3,000 so we uh this should open our application well it won't open because we need to manually open this port 3000 by default only the port 22 which is SSH port is open uh we have to manually uh open this port 3,000 uh in our uh VM so this won't just work until we open our Port all right to do that we need to create a firewall rule to create a firewall rule um I'm just going to go to uh this search bar and I'm going to search uh firewall policies so it will suggest you uh some item and click it and you will be redirect to firewall policies so in this page there is a button called create firewall Rule and here you can Define your firewall rule first let's add a name and it says uh add only lowercase letters and uh numbers and hypens so let me add block API traffic something like this and this should be unique and you can add a description as well and I'm going to turn on the logs and the network and priority I'm going to keep them as default and then the direction of traffic and it should be incoming so to allow incoming traffic uh you should take this Ingress uh option and other configurations like targets so we need to Target we need to specify which in instances we want to Target for this uh firewall rule so I'm just going to add all the instances uh in the network and for the IP range since I want to allow any IP so that anyone can access our application so for that I need to add this four Zer and 04 add it like this so it will allow all the IPS uh so that anyone can access our application and I'm going to check this wall button for protocols and ports and we are good to go let's hit create button and it will create the firewall rule for us all right so the firewall rule is created let's try refreshing this page and we are successfully accessing our application uh using our external IP and about 3,000 great all right so now we can test our application using Postman uh let me open up Postman and uh let's uh call some endpoints uh let's call this sign up endpoint we need to update this uh host which is currently the Local Host we need to change this to our uh external IP of our gcp server so let's copy this IP address and port and here in our environment let's update the host just like this so let's try calling this endpoint the sign up endpoint let's see uh if this is working yes we have a success response the user has been successfully created and let's uh try logging in okay and yes uh the authentication is successful and we can successfully login with that user so which means that everything is working fine so we have now successfully hosted our blog API in a real server in a live server so that anyone can access our application so now we know how to deploy our nodejs application to an actual server in the next video I'm going to teach you how to automate this deployment process so make sure to subscribe all right so I hope you enjoy the video hit the Subscribe button if you haven't already and I'll see you guys very soon in the next [Music] [Applause] video
Info
Channel: coder awesome
Views: 844
Rating: undefined out of 5
Keywords: docker, docker tutorial, docker nodejs, running nodejs docker, how to create a docker file, dockerizing a node.js web app, dockerize node app, nodejs app docker container, nodejs tutorial, nodejs docker tutorial, docker node app, how to dockerize nodejs app, how to build nodejs app with docker, how to run nodejs application in docker, deploy nodejs application, dockerize node js app, dockerize nodejs app and deploy, google cloud platform tutorial, gcp tutorial for beginners
Id: LSt1af5YZcQ
Channel Id: undefined
Length: 48min 43sec (2923 seconds)
Published: Thu Jan 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.