Deploying NestJS App To AWS EC2 with Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right everybody today we're gonna deploy a necess application to AWS ec2 instance using Docker uh well I built a Blog API project using messages so I'm going to deploy it but you can deploy whatever you want uh it doesn't matter we're not going to focus on the source code uh like we just only create a Docker file then a Docker compose yaml file and then we're going to create the ec2 instance and then deploy it to the server that's all we're going to do and I'm going to show you everything step by step so you can uh have a you can see the big picture of deployment right so let's get started uh first in the root of the directory I'm going to create a Docker file so like I'm going to create the docker file and Docker compose yaml file before going to production alarms uh like I find it more uh easy way I mean like that's better I think uh like it makes it more clear and uh organized before going to production anyway yeah you can do whatever you want but uh let's start so we have we will have two stage we're going to use multi-stage builds to optimize the final Docker image size uh because we're using typescript with dashes and so we're going to compile typescript to JavaScript and after that we don't need the typescript files right and also we don't need to production uh Dev dependencies in the node modules as well so we're just trying to remove those things to optimize our image size final image size so let's start and see how it goes uh our first state stage will be built let's call it like that it's just a comments and then we will have from our base image will be nodes 80 alphine and I'm gonna uh say it's going to build so it's going to be a build stage right and after that I'm going to create a work directory uh it's going to be user Source app and then I'm going to copy the package json's file and then I'm gonna run npm installed to install the dependencies and I'm going to copy all the source code to this uh container fastest and after I get all the codes I'm going to run the build script to compile subscript to JavaScript and as you probably know it will compile to this folder because that's how I configured in the config.json TS config.json file right the auth directory is this so it will basically create this folder and then compile javascripts into this folder uh so this is all about with the build stage so let's go with the pro Stage production stage so we can basically uh copy those things except for this builds Alias and we're going to copy uh from yeah from from build stage and we're going to put it from the user uh Source app dist to test right so what does that mean well uh we're basically copying things from this stage from this file system and like as you know we after we run the secret we have this folder after that right and so we basically get those JavaScript files from this folder and create this folder inside this production stage and copying all those things to this this folder right that's what's going on here and after that we need to uh copy the package.json again and I'm going to run npm install and this time we don't need div dependencies so I'm just going to say only production so it will only just install production dependencies after we done with the installation actually we done with the package.json file actually like in fact whatever because packages need just for indicating which module is going to be installed to mpm and after we're done with the installation we don't need them they are trash anymore so it's better to remove them for better optimized image so I'm just going to run uh RM package and after that I'm going to expose airports uh it's going to be three thousands you can choose whatever you want but I want my application to listen on Port 3000 and the Run script will be nodes uh dist main.js well in this part you can ask yourself that uh man like we have a secret called start prods that run the same codes why don't we use that well you can use it but you should uh delete this instruction if you want to use this script because uh in that case like without removing this instruction here if you try to use the secrets it will give you an error because basically like let me show you if you say npm uh run starts prods like imagine yourself in the in this container your uh like getting the JavaScript files to this this folder and then copying the packages and that installing all those things and creating node modules right and after that you're just creating the packages now in this container file system there is only two folder dist and node Motors nothing else right and then you just start trying to execute this code which is depends on the packages without packages and this means nothing like literally nothing and that's why you will get an error that says man uh this is not a valid command or something right so uh let's it's better to use uh notes uh nodes dist main.js because it doesn't require any packages or something uh so with that way we can just uh like start our container more optimized right so that's it one more thing we can actually set our not EnV not environment to production from development because uh by default it is set to development and like if we set it to production it will behave based on this variable like it will behave like its owner production so it will give us some advantage that I don't know yet yeah I don't know what it will be used for in the future but I see that a lot I think it's the best practice to do that let me show you we use ARC and it's basically stands for arguments and it will enables us to uh create a variable like this is obvious and I'm just going to call it uh notes EnV and it's going to be a production [Applause] production and after that I'm going to say EnV and it's for creating a declaring environment variables and our environment variables is Naughty and beat and the value will be naughty and me right here so we're basically set the node ene uh amount variable to the production right you can basically say production in here like that but I think it's best practice to use both like that that's what I saw in the internet and also asked the chat GPT and it shows me the same thing it's better to use them together but the main idea is we're setting an odme to production from developments so that's what it's for but that whole thing uh for the docker file we're done with it so now next step is creating Docker compose so let's create it real quick docker that could compose all right our version will be three uh and we have two sources backend and database backends you can call it whatever you wish um r build path will be root directly because our Docker file exists in the root directory and then our container name will be nest.js API that's what I'm going to call it you can just call it whatever you want as I said um and after that we need to set some environment variables such as ports right because uh basically my source code in the main.ts I used ports from environment variable right and I set it like here in the tme file actually this.tme file is just for development we're not going to even deploy to production but it's just for like the world as I said to see what's going on here uh using environment variables right but now we need to pass those things to environments just like this let me pass this like that so let's make that true outside of hateful as well so that's basically the environment variables we're passing to this container once we spin up right and after that we need to say the Define the port mappings that's going to be three thousand to three thousand and of course we need to say the port going to be three thousand as well like the all ports need should be same like it's need to be three thousands so it will listen to them for three thousand and also we need to map this to uh for 3000 as well and also we need to expose it in three thousand in the same port basically so after ports I'm gonna say it's going to be depends on database so let's go to database and our image will be image and ports will be this 27 0 8 17. yep and also we will have volumes for data persistence uh uh I want to create a DB backup folder in the root of this directory and I want to get the data DB from the mongodb file system and also I want to create a network and I'm going to call it nest JS Network and driver will be a bridge [Applause] so let's add this network to both Network it's going to be let's just Network let's copy it and paste it on here as well [Applause] all right looks perfect um yeah I think we're done with it right we're just passing environment variables where uh to find the port mappings we say it depends on database so it will first uh execute it by Docker compose and it's going to be in the same networks both of them it's going to be in the same Networks uh yeah that's pretty much it and one more thing uh we need to replace this look closed with the container name of this database and we need to do a container name I'm going to say BB uh like DB yeah on the DB and I need to replace it with that so that's the beauty of environment variables right if we wouldn't like we didn't uh use environment variables like I need to go to the uh I have that module where the uh where I register the Mongoose module and change the uh connection string based on this uh container ends it is much likely to make mistakes without usage environment variables because you just go to the one file and then another file another file man instead of just doing that just create environment variables configure the environment variables in your source code using some sort of packages such as Daddy and me or config module if you're using messages and then you can just uh pass these values to your variables from a one file Docker compose file in this case uh like that's it right so now we're done with the docker compose so it's time to push this project to GitHub so then we can create our ec2 instance and clone this project from GitHub and spin up our containers using Docker compose but I don't want to include Docker compose yaml file to my uh Source control system because it's includes some sensitive information especially the Json webs token secret right so I want to go to uh getting your file and I want to put a double compose Yama here to ignore this Docker compose yaml right and also we don't want this that enemy file as well as I said it's just for production just for development we already passed the values from our Docker composium right and when we uh clone this project to our to instance we're going to create this Docker compose yaml file and copy this code and paste them here which I'll show you in a mix so that's it let's open up our terminal and initialize the kit here I think we have it yet here right I'm gonna remove this for a clear start let's delete the gate so let's initialize again and I'm gonna add all of them to stage check that rates so let's commit that I'm going to say initial commit great now let's go to my GitHub account and create a new repository for that I'm going to say uh block app nest.js right yep let's just create Repository I'm gonna get this URL and let's first I'm going to change the branch name to me and I'm going to push it to this repository to the main branch great now let's refresh the page and see the changes it should be here yep great and we don't have Docker compose yaml as expected perfect now uh let's go our AWS account and create an ec2 instance to deploy it right um I'm going to create a new I'm going to create a loud instance all right ah I'm gonna choose the Ubuntu image instance I will be two T2 dot micro I'm going to select my Cape here uh that I created before and network settings I will leave it as is and of course we need to give a name I'm going to give it a name called uh block let's call it necess app right you can just call it whatever you want it is just a name but anyway let's launched instance thank you in this JS app so unfortunately let's go to Security Group and I just want to add the port 3000 TCP rule because otherwise we cannot access the port 3000 to uh to access to our application right so I'm gonna go to the security groups I'm going to edit inbound roles and currently we have only SSH rule which enables us to connect to this virtual machine uh via SSH and now we want to add the TCP rules for Port 3000 and that will enables us to go to this IP address Plus Port 3000 right and any program that works in Port 3000 will be able to see them so let's say the rules of course we need to uh Define the source you can choose your myip but uh it's not for real products I'm going to select anywhere it doesn't really matter but for real Productions you may want to choose your own your IP address or some specified IP address but anyway so that's it let's uh pick up our IP address and connect to this little virtual machine so my uh SSH key located in SSH folder so I'm gonna go there and I'm gonna say SSH i iPhone I my super kp.pm file and username is Ubuntu this one perfect yes I want to connect so we're in greats and now before cloning our project I won't install Docker to this machine first so I'm just gonna go to uh the Docker website to install Docker in a in a proper way so I'm gonna copy those instructions step by step and install Docker as as it should be right so foreign that's great I love deployments really it feels like a superhero [Music] let's copy that as well paste it in here and let's update Repository and then we're gonna install the docker Docker compose and all other uh some plugins so yep yo it's just that easy right it's just full of these instructions from the docker websites and yeah just you'll have a Docker installed on your machine all right and now we have that so if I say uh Ducker version yeah it's installed perfect now I'm going to create a folder called apps and I'm going to clone my project to this uh folder so let's go to the GitHub and get this repository code link I'm going to clone it in here so let's get in uh we have Docker file but we ignore Docker composium of files in our development environment so we don't have it here so I'm just going to create it here oh not like that's Nano Docker compose yaml I'm I'm I will basically copy those things and paste it in here exactly just save and exit and now we'll we're all set up now all we need to do is just type in sudo Docker compose up in detach mode right so it will it would it should spin up all our container one by one uh if we don't if it didn't make a stupid mistake right uh some syntax or some sort of uh really frustrating mistakes I hope you didn't let's see uh I will say sudo Docker uh compose compose up in the touch mode so let's see how it goes well I love this view really it feels like you're in a uh movie right [Music] all right so now we're installing the npm packages [Music] dude [Music] [Music] right now we're only installing the production dependencies we're in the production stage now right [Music] foreign [Music] come on man all right it's done perfect it just spin up all containers uh very like as expected and create our networks and everything looks good now let's see if it's really like that Docker yes and yeah perfect are containers just running perfectly and it's mapped to Port 3000 to 3000 TCP now let's open up our Postman and of course we need to get the IP address uh let's get that and I'm gonna go to postman I'm gonna uh for three thousand and like it should be like this uh I think we have a problem Let's uh see the logs Docker locks all right uh everything looks good it's running on Port 3000 and it's connected to database so what's the problem you get those things and paste it in here [Music] come on man all right I found this it was just because of my bad internet connection uh so it's just working I didn't change anything um so if you go to postman or any browser and type your IP address Plus Port and I currently if I send a get request to this port come on man just give me a response so let's try the same thing in here I'm getting that okay it gives the same thing uh so it just shows me that it's working because uh I put a card in front of this uh route so without uh putting a valid token in the authorization header it will return an unauthorized so that really doesn't matter but in the post request I'm gonna add it uh like just do that and we have off rocks we have a register it's about source code right I'm just trying to test it and I'm going to send some body uh email and password and it should return me a Json web token let's try this all right it's work as expected so that means that we pass the JWT Secrets ports in mongodb connection strings properly so it basically uh saves you to the database with a secrets and it's working on Port 3000 so all the variables environment variables sound perfectly uh like our containers are running so there's nothing wrong uh basically we deploy it correctly so let's just uh go to apps again uh with low caps so now we have mongodb backup for volumes for mongodb right and this basically keeps some uh data for data persistence if it's just we want to take a look at it there are a lot all right it's just it's just for as I said keeping for data and also uh like let's say uh sudo Docker PS not like that PS our mongodb container ID is that I just want to get into it right so I'm just going to say exec it for D and dash right of course uh practice with the sudo and now we're in let's go to the sh shell now if I say show DBS it'll show me the blog app which I connect to let's use that I'm just trying to show you that it's working properly right it's just for testing and if I say DB users dot finds it will show me the object that I just created now with this post request right perfect now we can just get out now uh yeah that's it and also if you want to take a look at uh to the Nexus container you can go there sh and now we have only distant node modules folder this is the final image remember right we we removed package.json we don't include uh Source folder or test folder or any other configuration files that we don't need uh to run our JavaScript files uh that's basically the most optimized way to deploy an SGS application to production environments right but that's it we successfully deploy our NCIS application to AWS ec2 instance using Docker yeah that's that's going to be the title of the video right how to deploy or deploying uh necess app to AWS ec2 instance with Docker yeah that's going to be the title but that's it I'll teach you guys next time bye
Info
Channel: Kaliteli Yazılımcı
Views: 7,962
Rating: undefined out of 5
Keywords:
Id: mqI5ZcV3prI
Channel Id: undefined
Length: 29min 22sec (1762 seconds)
Published: Thu Dec 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.