How to Deploy a Docker App to AWS ECS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome back to the channel in this video what we're going to do is learn how to dockerize an application and deploy it to aws elastic container service now for this crash course i do expect you to know the basics of docker you don't have to be a professional by any means but for example i do expect you to know how to download docker onto your local machine because that's not something that i am going to cover if you are new to docker and you want to learn more i do have a docker crash course as well as a kubernetes crash course on my channel all right let's get into it so what are we well dockerizing we're going to be dockerizing an express api and so an api is something where we hit and it gives us back data so we're going to be dockerizing that and then deploying it to our machine now express is built on top of node however you do not need to have node or anything installed on your local machine because of course we're working with docker and the containers are going to be isolated from our local machine so in order to get started let's not build the express application ourselves let's go to my github page let's go to my index.js and let's just get this very simple express app really really simple nothing uh nothing insanely difficult about it so let's create an index.js file and then in here we're just going to paste this so you can see here i have an express app and then we have this endpoint which is the the slash endpoint this says this is my express app and then we have the me endpoint that says hey i am late of course you can change this to whatever it is that you want all right and then the next thing that we need to copy and paste is the package.json and this just shows you all of the different dependencies that this application needs so let's go back over here and let's go to the package.json and let's copy this that's all we need that's all we need anything else over here so for example the uh the i guess i ignored the known modules but anything else we do not need at the moment all right so let's go ahead and dockerize this application so of course to create a docker file what we need to do is well to create a docker file so let's go here and what we're going to do is well create a docker file and in here we're going to highlight the series of steps that we need to do to create our docker image for our express application so what's the first thing that we need to do so let me just go here let me see if i can zoom in once more no i can't so what's the first thing that i need to do well i need to specify a base image we don't want to be working with a completely empty image we want to be working with an image that has some of the things that we need to work with the node application so what we're going to do is we're going to say from node alpine so from node alpine right over here alpine so that's going to be our base image the next thing is specifying the working directory the working directory is going to be the directory where all of our files are going to be and run inside of our container typically what i suppose like is a workdir and then our working director is just going to be inside of an app directory and what's going to happen now is everything is going to be put inside of this app directory and everything is going to run inside of this app directory all right so the next thing that we want to do is well we want to start copying some of this code inside of the working directory so the first thing that we actually want to copy is the package.json so in order to do this we're gonna do copy and we're gonna do package dot json and then we're gonna do dot so what this means is it's going to copy the package.json notice that the docker file is in the same directory as the package.json so i'm going to copy the package.json and move it to the working directory which is going to be inside of the app directory all right so that's the first thing that we're going to do and then what we want to do is install all of the dependencies so now that we have the package.json what we can do is an npm install to install all of the dependencies that we need for example right here we have express and we need to install express to run our application so let's go over here and we're gonna run run npm install all right so that's that's good that gets us our node modules our package.json however now there might be some other files and folders inside of our application that is not inside of our image such as the index.js so at this point what we want to do is we want to just copy everything in so copy everything inside of this directory to the app directory now you might be wondering why we didn't do something like this just copy everything in right away and we omitted this step this would work perfectly fine but docker works on layers and what happens is if it runs this command once and you try to run it again this is going to be cached this is going to be cached this is going to be cached so the reason why we want to do this is anytime you make a change inside of our file directory and not our package.json we don't want to run this step the npm install step because once something is not cached so for example let's say we have this over here we make some sort of change in our index.js maybe we change this to me with an extra e even though this change does absolutely no nothing to our package.json well it is a change so this step is not going to be cached and then any following step are not going to be cached as well so we're going to run this npm install even though we made no changes to our package.json and npm installs are typically the most expensive steps and take the longest time and so what we want to do here is just check hey do we have anything changed in the package.json if that's the case then don't cache this step and then of course run the npm install however if we uh however if we do have something changed then just uh or we don't have something changed just cache this cache this and then you don't have to cache this i hope that makes sense all right so the last thing that we want to do is uh well we're running on uh localhost 5000 so let me just quickly get rid of that and save it so we're running on uh localhost 5000. so in our docker file let's explicitly say that we're going to be exposing localhost 5000 and well now what command are we going to be running as soon as the container runs well as soon as the container runs what we want to execute is node and then we want to execute index.js you could also do something like npm run start however it's actually best practice to use node for this and the reason why is you don't want to have npm running in the background as well in order to start your application all right so that's the docker file um that's pretty much it that's really really simple so now let's actually go ahead and try to run the docker file and just see if it works on our local machine all right so let's go ahead and build this image in order to do this is open up our terminal and we are going to run docker build and we're going to tag it with express app very very simple and then we're going to specify that we want to build um this docker file right over here in the same directory so just notice that dots right over there so this might be a little bit uh faster for me than for you guys because i already ran this and a lot of this stuff is already cached so now what we can very simply do is just say docker images and you can see that we have an image called express app so wonky let's go like this we have a we have an image called express app uh the tag latest we also have this image id let's go ahead and copy that in order to run our container so now what we're going to do is we're going to do docker run and then we have to specify the ports so remember this application inside of the container is going to be running on localhost 5000 so what we want to do is do something like this we have to specify the port and we're going to say localhost 880 on our local machine we want to connect it to localhost 5000 in the container because remember again in the containers running on localhost 5000 this number can be anything this number can be whatever it is that we want on our local machine actually you know what let's change it to six five six five and that's pretty much it so we're gonna do docker run and then we're going to specify the port and we're going to specify the image id that we want to create the container from so now if we were to execute this we get listening as you can see here listening and if we were to go to localhost 6565 let's go here uh go to localhost 6565 and just wait a little bit you can see that our application is up and running so i'm on the root uh the root path which is this one and i'm getting this and if i were to change this to me so slash me i get high i am lathe awesome so we got it running on our local machine now let's get it running on the web with aws now that we got our docker application running our local machine we know that it works so now what we want to do is of course to get this application into the cloud and deployed it in the web and we're going to do that through aws so go ahead and log into your aws account and once you're in there we're going to search for ecs standing for elastic container service so this is where we're going to use to deploy our docker application so you should see something that looks like this so the first thing that we need to do is we need to get that image that we created into aws and we're going to do that through amazon ecr so this is the elastic container registry where we register all of our images this is similar to something like docker hub where we store our images there so we're going to go over here to repositories and we're going to create a new repository so let's go over here create a new repository let's make this a publicly accessible repository after you're done with it just go ahead and delete it the reason why we're going to make it public is i don't want to set any iam rules so we're going to go here say public we're going to give our repository a name let's just call it express app and then i think that's pretty much it we can give it a short description i think that's fine everything here is okay let's go ahead and create our repository all right so that was very very quick but this is just the repository now what we need is to push our docker image so this docker image over here we need to build our image and push it into the express app repository so let's click on the express app repository and click over here to view push commands so over here it shows you all of the steps that you need to run in order to well push your local docker image into this repository so let's go ahead over here so the first thing that we need to do is authenticate ourselves so let's copy this command and we're going to paste it in here and we should see after a while that we are successfully logged in awesome the next thing that we need to do of course is to build our image so let's go ahead here and let's build our image all right let's just wait a little bit there we go nice and built the next thing we need to do is to tag the image so you can see here tag your image so you can push it into this repository so let's tag it and then the last thing is well now we need to actually push the image to the repository so we're going to do a docker push and there we go now one thing that i'm going to note is you're going to need to have the aws cli installed to do this especially this step right over here so if you don't have the aws cli then just go ahead and well install the aws cli it's not a very difficult download i'll have the link in the description below it's going to be a little bit different depending on your operating system so you can see here now it's it's pushing our docker image to this repository so let's actually exit out of this and if we were to refresh let's do a quick refresh we still have no images because it's still pushing so let's just wait it shouldn't take too too long all right i just had a sip of some diet mountain dew all right so now if we were to refresh we should see our image in here and we do all right so now what we want to do is we want to copy the image uri so click on this button so we're going to need that later so this is only going to push our image to this repository it's not doing anything else it's very similar to pushing to docker hub we're not running an application in order to run our application we need some sort of server and networking and so what we can do is we can actually set that up with well let's go to let's go over here to amazon container service so not ecr what we're going to do is set up a cluster and this is going to set up an ec2 instance which is going to be our server we're going to run our application and it's also going to set up some networking so we're going to go here we're going to create server and so over here we're going to say ec2 linux plus some networking so we can actually network through our server we're going to say next and then over here we're just going to give it some you know some some some things that it wants so like the name of the cluster we can say my express app uh on demand or spot you can actually read over here so on demand is you pay for the capacity per hour and then spot is uh uh it's just kind of spots demand over here let's look at the types so the instance type so let's you know let's use tc let's use t3 let's use let's go here let's use t2 micro that's what we're gonna use uh for instance we're gonna use one over here uh and then if we go down this is where we're gonna configure our networking so we're going to use the default vpc the subnet let's pick the first one all right and then over here we're going to enable the auto assign public ip and then for security group we're gonna we're gonna create our default security group and then over here for container i think that's fine and that's pretty much it so now what we can do is we can create this cluster and what it's going to do is it's going to create our ec2 instance and it's going to connect it through the networks that we configured so just give it some time let's just wait it shouldn't take too too too long and while we're at it what we could do is very simply go to ec2 and let's click on ec2 here and if we were to go to instances we should see this is this is an extra one that i had but over here you can see that uh we have this ec2 instance right here that we are uh well we're creating awesome so let's go back now so over here if we go back to ecs so if we go back to ecs you can see now we have this cluster so we have this wonderful cluster so the next thing that we need to do is to run a task so we need to run a task so let's go over here to the task definitions and it says here that a task definition specifies the container information for your application so it's going to specify uh exactly you know how much cpu we need how much memory we need and specify the container information so we're going to go here we're going to create a task we're going to say ec2 we're going to say next steps and then well we want to give our task definition a name let's just call it my express app as well maybe task and then over here we're going to give it no roll let's find network we'll leave it as the default so in terms of memory let's just give it 100 we're going to delete this all later so let's give it 100 mib and then for cpu let's give it one v cpu and then we're gonna add our container definition so over here we're gonna say my express app container so let's say container and then over here we're gonna have to paste in that image uri so it knows exactly what image to utilize to build the container all right so over here i think that's fine so the last thing that we need to do is port mapping so let's actually go over here and let's remember what we had before so when we ran our application we did something like docker run and we did port was it six five six five and then over here 5000. so 5000 was the application in it was the port internal to our express application so we're going to specify that here so the container port and then the port that we want to expose was five so what we can do here is also add that in six five six five all right other than that i think that's fine we do not need anything else yeah i think i'm okay with that let's go ahead and add that and there we go we should see that there and then that's pretty much it we can go ahead and create this task definition and now what we can very simply do is go back to our cluster and once we're in our cluster we can go to our express app and then we can go to task and run that specific task so we're gonna run a new task we're gonna run this task right over here my express app task and then that's pretty much it that's really all we need uh so let's go ahead and run this task let's go ahead and run it am i missing something oops yeah i'm missing this so over here we need to specify the launch type ec2 we're going to go ahead and run the task and you can see here that now it's pending and you can like consistently refresh and wait until it is running so let me zoom in here a little bit for you guys as you can see so we're waiting and once it starts running we should be able to access it awesome so in order to access it let's go to our ec2 instance again and what we're going to do is we're going to go over here we're going to click on this ec2 instance why am i on the security group let's go to the instances so over here we have this running instance so let's click on this and you can see here that what we can do is we can copy the public dns so let's go ahead and copy that and what we're going to do is we're going to paste that in there and then we're going to do colon 6565 because remember that's the port that we're exposing now at this point this is actually not going to work and that's because in our security group we need to expose that port so let's go over to our instances right over here and what we want to do is we want to find the security group for this particular instance so let's go ahead and let's look at we need to find the default and since let's actually go back here and let's click on that let's go here to security group let's find security group here all right where is security group cq security group there we go security group there we go i found it so let's just zoom out a little bit all right so it's using the default security group so let's scroll all the way down and click on security group and now what we want to do is we want to update the default security group so what we want to do is we want to edit the inbound rules so let's click here edit inbound rules and over here actually i actually did this before so let's go ahead and delete this and so what we want to do now is we want to add a new rule and we're going to say 6565 that's the port so six five six five and we wanna say from anywhere so we're gonna say over here we're also gonna add another one we're gonna say six five six five and we're gonna do this one over here so so we can get it from anywhere and now what we can do is we can save these rules and if we were to go back to our application now and refresh you can see that it is deployed awesome and that's really it so now we can actually start making http requests to the server and if i were to go to me slash me you can see that it works all right hopefully you guys learned a lot in this crash course the last quick thing that i want to do is just show you how we can just delete everything so let's go to our cluster so e c s or no elastic container service we're gonna go to our cluster so let's go to our cluster and we're gonna go click on my express app and just simply delete this cluster and this might take some time however it's fine we can go ahead and delete that and i guess while we're at it we can open up a new aws console let's just copy this so i don't have to log in so let's go here to aws.home and so yeah while we're at it we can also start deleting some of the other things i think this is the only thing that's gonna actually charge us money but let's go to our let's go back to e contain elastic container service and we're gonna go to the repository let's also delete this repository so we can go ahead and delete that all right let's delete that so we can just we don't need it anymore just for learning purposes and then of course in the task definition what we can do is we can click on this and then we can perform a certain action on it oops sorry not that so we can do is we can click on this task over here and uh what we can do is just de-register this task so we don't have it anymore so now if you were to refresh we shouldn't have that and this this is going to take some time to remove but it should be uh should be fine once it's done so now we've deleted all the resources that we have been utilizing so hopefully you guys learned uh quite a lot i really enjoyed making this crash course and i'll see you guys in the next one
Info
Channel: Laith Harb
Views: 581
Rating: undefined out of 5
Keywords:
Id: YDNSItBN15w
Channel Id: undefined
Length: 24min 17sec (1457 seconds)
Published: Sat Dec 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.