How to Deploy a Node.js (Express) Server Docker Container to AWS via Elastic Container Registry

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi programmers in this video I'm gonna show you how you can deploy your node.js or Express server on AWS so this is the clear picture what we trying to achieve over here so basically we will be having our Express server a running locally first and we will try to deploy it into the docker container and run that Docker container inside our local machine first and let's see everything works fine or not so this is the first step second if everything goes well we will deploy this we will push that container to the registry of AWS ECR that is elastic container registry then we will launch one of the instances of AWS that is ec2 Linux instance and then configure the docker and pull that container which we already created from the local machine and put it here inside the registry and try to run that container and make those API available to entire public entire world so that is the flow and let's start doing it so first of all let's building the docker inside the local machine and then run it okay so what code I have over here is nothing fancy I'll be having the folder structure the server.js and the app.js nothing fancy just um separating to a separating code to keep organized over here I am I'm having this server they're running on Port 8000. nothing fancy so you have to do two things over here one thing is that you have to add dot kit ignore definitely and you have to add dot Docker file and from this file you will be asking you'll be telling the docker hey uh go inside the docker Hub fetch this image of node.js and then I would highly recommend you guys to initialize the environment so that you can change Dynamic values in the in future saying that okay I don't want to run production this time I want to run sandbox something like that and just copy those things I'm I'm sure that you must be aware of those simple commands of Docker uh and then I'm just copying the directory entire code to the docker container and just telling the docker hey after installing each and everything just run this segment and it it will up and run now what you have to do over here is like everything is configured and the second thing make sure that you should add the dot Docker ignore file and I would highly recommend to create one empty folder called key I will tell you what we will be adding over here and then add those key as inside the dot ignore file and Dot uh git ignore file as well now you have to go and let's build it Docker build iPhone T and and I'm gonna give like programmer that is double a m e r that is my uh username and let's see node Docker like that if I build it and it's fetching all the data from the docker hub now let's see whether we have Docker image ready uh yes I have now let's run it and see whether everything working fine or not Docker run iPhone e on 8000 that is 8 000 and then uh in detached mode with this container sorry this image which will be creating that container I'll copy this and on it okay so it's up and running let's see if I go over here okay I'm gonna type localhost 8000 yeah perfect it's running now what I'm gonna do quickly say that Docker stop test I'm gonna stop it uh sorry now this one I need to take this Docker image to stop it docker to occur in stop image name and it's not stopping so I'm gonna stop it forcefully again it's not stopping so the thing is that what I'm doing wrong is okay I need to check the processors oh that's the processor and then docker stop that one and it should stop if I go back and check the browser and I yeah so that's the browser if I hit it should not work okay now I'm gonna go server in server.js change this one to hi hello programmers like that so that if we so you will be having this one the new message inside the final Docker which will which we will be deploying on the AWS now this part is done now let's push that Docker container to ECR first okay so what we have to do now to push that one over here I'm assuming you already have AWS configure over here if you don't have just go and configure it and then open the main part that is the ECR over here inside the registry uh inside the Amazon console and then you just need to you know I'm just going to delete it quickly delete and create it I'm gonna add this name nothing just add this uh name over here you are creating a repository just nothing but to save that our Docker container what you have to do over here just open this select this button and just copy this steps one by one so just go over here paste it first you have to login so those those credentials are Dynamic over here based on your requirement everything the host will be dynamically added over here just need to copy paste see the login is succeeded here now you have to build that Docker image okay over here once you build it this building that Docker image and everything and once you build it tag it and paste it over here yeah everything works fine let me check Docker images is there or not yeah so its image is already there now I'm gonna run that I'm gonna push it sorry I would say I'm gonna push it to AWS so uh as soon as I push that one so I just need to go to this website and then sorry console and see everything works fine or not still it's taking time no problem is still is pushing and that was the last one I believe and yes everything was pushed and we got our ECR so the second part is done that is over here now we have to launch the ec2 instance no problem now just go to the ec2 instance ec2 and then over here what you have to do is launch the instance go to launch instance and type Linux server for node I would I would I can give any name and the lineage instance I would suggest to go with that and I'm gonna select a few free tier because I don't have that much uh too many apis I would say and then click select the first one it works fine that's fine now you have to make sure you should consider two points over here first create new key pair value so line X keys and just save it create this pair as soon as you create this pair open the vs code and just drag this pair to this even key folder that's it and second thing what you have to do over here is just go to the network setting edit it and over here you have to add one more protocol saying that custom TCP with 8000 Port you have to expose this port to the entire public and it should be accessible from entire world that's it you have to do those two things as soon as they are you done those things doing just launch it now what you have to do is that I'm assuming you already have git bash available if you don't have I would highly recommend go and install the Kit bash that is this one uh download and then just download this git bash okay now open the git bash go over here and view all instances now select this one as I have just only one so I can see it's still still initializing still in pending State that's fine yeah so if you have multiple just select uh the one which you just launched go to connect select the SSH client don't think too much just copy the last line over here you are telling to this specific command on the gitbash saying that hey take my keys and go to the username this username particular and that host name and try to login it no problem so what I'm gonna do I'm gonna go and type git bash launch it and then uh here this is the one okay what you have to do over here is go inside this command copy path okay here CDD I cannot like that d okay I'm D inside D and CD Ctrl V so I am here okay so YouTube coding workspace okay that's fine and let me try oh no I can't go like this oh no problem I'm gonna go individually quickly like this okay cooling workspace and then node and the project and the key like this I went inside I have these Keys over here sorry LS and I have that key perfect now just the one which we just launched the uh instance I'm taking those last command this one last command and just pasting that one inside that key that because that key is available inside this key hit it so it says like okay are you sure you want to continue connecting definitely I want to hit it enter and URL now you are inside the uh Linux instance now the fun part will come over here we what we have to do from here so we we are inside Linux instance so this is totally different independent and this is totally independent thing now what you have to do over here is go inside and talk to our AWS because this is realizing in Cloud right not inside the machine so you have to first configure the AWS inside the body over here inside this new virtual machine so first let's see update it quickly and then yes and then you have to configure each and everything and I would suggest how to do it is that just by just type AWS configure figure AWS just type AWS and let's see it will ask you a number of things no problem so I'm gonna type AWS configure and then hit it so it will be asking the access key and the password key so I'm gonna pause it and quickly figure it out and add it over here and let's start all right I've configured each and everything and now what I'm gonna do over here is just going inside the console one more time and then I'm gonna go and let's go inside and pull this one this image what you have to do just select this view push and that's because as you configured this username password inside the virtual machine of Linux you just need to go over here and then say ah okay so I got two not this one by the way okay this one I'm gonna go and hit it as soon as it says like no doctor coming upon I believe some needs that's the one no okay so it's it's saying that okay AWS ECR login is not found that's fine so what you have to do over here you have to first say that okay you have to install the docker first you have to do two things type sudo user mode hyphen a capital G and then docker ec2 user so this is the same user you are telling this uh Learning System hey use the admin mode as soon as you hit it Docker still it's not there so definitely you have to install the docker how to do it so you have to do like sudo em uh install docker and then it will it will pull all the files from the docker Hub uh sorry the docker and then and you go quickly I believe okay now you have to do like sudo em service Docker start if you do this you can see uh it's not available until now so what is no sir this is not found that's fine okay sorry not like that so we don't have to do yum command we just have to push this one that's fine now try pushing saying that Docker info the simple thing it says like access permission issue so you what you have to do you have to push two commands over here sudo reboot as soon as re you reboot it or I would say first to go with user mode have an a capital G and then docker e c 2 user it is Capital uh user oh yes sorry my bad so it's not mord it's like imod and hit it yes we got it now let's see whether I can see Docker info no still I can't see so what you have to do over here is say reboot so that it will reboot and configure and come up with the admin mode so guys over here you have to wait for some time because it's rebooting entire virtual machine behind the scene so this terminal is my local system and that terminal was the Linux terminal system inside the AWS you just have to wait for some time and if you go back to this one for example to try hitting it this one to connect it one more time I think we might get some issues saying that time mark sometimes it's a timeout sometime it will go quickly as we have the very lightweight Linux instance so it might take some time so what you have to do over here is just wait and yeah definitely it will it will give us an error saying that connection oh wow we went in that's beautiful so now what we have to do over here nothing fancy we have to go inside the okay we we configured Docker and let's let's see whether we have Docker info now over here Docker info and we don't have Docker info that's fine we have to install docker sudo em service we have to first start start the service of Docker and then service Docker start as soon as we did that it is it must say Docker info and yes I can see it's running the docker in our machine perfect that's really cool and then I'm gonna go and type docker uh images and check how many images we have we don't have any image over here so this is the image we have to take so what we have to do over here is just go back to this one I take this last value okay and then as you configured the AWS inside this one the console you just have to like data okay docker pull because you already uh have that image there just pull it it might say this error uh uh authentication fail okay no problem you have to configure it one more time I'm gonna go quickly and configure it oh sorry sorry not that one so you have to first do this command authenticating it and then successfully authenticated and then pull it once you pull it you can see all the values inside I'm gonna see Docker images whether I got that really yes we got that now what you have to do over here is like docker run iPhone p on 8000 as we expose you might remember on 8000 at the time of launching the instance Linux instance and then once you've done that what you have to do is over here is like you just go and type hypon D that is in detached mode and say take this value once you've done that hit enter it says okay I'm gonna check the processors are running or not it's running I'm gonna go to the ec2 instance instance and then close it so that that Docker was running there you have to copy this public ID IP address you can choose either of those go back over here and type 8 000. boom we got our profile up and running that's really good but the thing is that okay I want it to go to 9000. port it won't come up over here so but if you want to configure into 9000 what you have to do is just go to select your instance go to the security group and this is the instance for that server that server could be a test server or the instances if you go back sorry over here and then if you go and check the security group uh okay so you have to go from here the security group and then select this one as you can see we have eight thousand and nine thousand that was the previous group not this one uh so it's the one which we just created was I couldn't find over here so basically you just need to go inside this server and networking and chain Security Group what is the security group that's the one Security Group I have so I'm gonna go and check the security group uh my bad so the thing is that what you have to do over here is just go back to the instances select this one for a networking Change setting so launch wizard 6 is the one okay that's fine so you have to loan go to insert this and that's the one okay you have to go and edit it and exactly this one you have to add a role saying that TCP to 9000. and from anywhere as soon as I add that one you can see that this might will come as soon as I had add this file you can see the 9000 will work or might not work but let's see and I've added over here and still it's loading if I hit it it might take some time to reflect definitely it won't work because the thing is that that Port was running on different one so I'm gonna go and change this port to 9000. as well as soon as I hit it and then see 9000 I'm gonna go and check do this 9000 perfect we got nine thousand now as you can see now the thing is that if I close this tab even I close this tab come from the outside world exactly you can see this is up and running to the end this API this this particular line is a ready and available for entire world I hope you like my video if you do like please like And subscribe and do share to the people who definitely need it thanks for watching guys
Info
Channel: Prograamer
Views: 8,618
Rating: undefined out of 5
Keywords: aws, docker ecs setup, docker ecs tutorial, aws ecs tutorial, aws ec2 tutorial, amazon ecs docker, docker, aws tutorial, aws training, node docker ecs, node docker tutorial, aws ecs guide, docker aws tutorial, aws docker tutorial, node.js docker ecs, node.js docker tutorial, node docker linux ec2, deploy nodejs app to aws, amazon web services, AWS, linux, aws tutorial for beginners, aws ecs, Node.js, python docker ecs, aws ecs ec2 tutorial, aws ecs fargate tutorial
Id: SZOMHHGsATw
Channel Id: undefined
Length: 24min 56sec (1496 seconds)
Published: Sun Sep 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.