Hosting a Docker Container on AWS EC2 Free Tier in under 12 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm going to be walking through how we can take a Docker container from our local computer and put it onto an ec2 instance that we're running in AWS and in this example I'll be using flask but honestly anything you can dockerize you can upload it to an ec2 box and run it there and so I'll get started and just to show you guys what this thing does locally I'm on my Mac right now this is an M1 computer so it has the arm CPU architecture um if I run this command Docker compos up D- build it's going to go through the uh process of building the image uh and then running it as a container and so now if I open up a web browser and I go to my local host Port 80 we can see that uh I'm getting response right now from my uh computer so I'm hosting a little flask app in Docker I'm running Docker on my Mac and so we're going to go back to here I'm going to press control see to cancel this process uh and then um now what we're going to do is we're going to walk through the steps of creating an actual ec2 instance to get the ec2 instance to be running my flask app for me so that I don't have to run this on my local computer anymore so first thing I'm going to do is I'm going to have a web page right here with my AWS console signed into it uh I'm going to click on ec2 or you can type in ec2 and I'm going to launch an instance and I'm going to give this thing a name I'll just call it ec2 you can call it whatever you'd like uh and in terms of the operating system image uh I'm going to choose al2 um and so we're going to be using that for this demo because I'm trying to keep everything here on the free tier next up what we're going to do is we're going to leave it at the x86 architecture this was a very important point for me because um I can't take the direct Docker image that is built on my Mac because it's an M1 arm chip and put it on this ec2 inance because it's an x86 chip so we're going to have a work around which is we're actually going to put the docker file onto the uh ec2 box and then build it there which also is going to save some data transfer speeds and stuff like that too um but stick with this this is the only way you keep it on the free tier and I'm cheap um we're going to again leave it on the t2 micro and um for a key pair you need to have one I've already created one it's very simple to just create one um you guys can look up how to do that but I'm going be having this and when you do that it'll create a PM file you'll need to make sure you've saved that pen file locally to your Mac this is how you're going to authenticate to your ec2 instance um down the road and then we're going to be creating a new Security Group we're going to be allowing SSH traffic from anywhere obviously this is not a production environment otherwise i' would be a lot more strict about that we're also going to be allowing HTTP traffic and https traffic for our web server just so we can make sure that it works by sending it a get request on Port 80 um and so uh aside from that we're going to keep it uh under 30 gigs for the uh storage and then we're going to click on launch instant so we're going to give this thing a moment to finish uh launching so we're going to click here and uh we're going to wait for this thing to get into the running State all right so this instance I can now click on the connect button right here and we're going to be using uh the ec2 instance connect so there's a default uh user on the uh al2 image or Ami um and it's called ec2 so we're able to just connect right to it uh through our web page like this which is pretty slick um so we're going to let this thing establish this connection and now we're going to be running a series of commands uh the very first one we're going to be doing is uh a yum update all right and now that that's completed we're going to run this command which is going to um tell the Amazon Linux extras uh service to install Docker so we're just going to run that give this thing a moment and it's going to ask us if we're okay to proceeded make sure you put y for that and not anything else otherwise it'll cancel out okay and now that that's done we're going to actually start the docker service on this ec2 box so we're just going to run that command so now the docker service is up and running um another thing I'm going to do is I'm going to run this Command right here um so basically what we're doing is we're modifying the user permissions for the ec2 user who are signed in as to basically grant them uh access so that we don't always have to run Pudu every time we want to uh do some kind of dogger command which will help save some time and some frustration later on um that's very quick and then uh the other thing I'm going to do here is I'm going to run this command PWD just so we can see where we're at and I'm going to make a dur on this thing and I'll call this you know downloads whatever you can call whatever You' like um we're going to change directory into our downloads folder and so um that's all we're going to do here for the moment I'm going to go back to uh my py charm terminal uh app and uh all this stuff is on GitHub so I'll give you guys a link where you can clone this exact repo uh and I'm going to clear my terminal there and so uh the very first thing that I'm going to need to do here is inside of my project if I run LS you can see that I don't have a PM file but we're going to be using SCP in order to upload the uh files here to the ec2 instance and so I do have that pen file saved already on my Mac and I've called it uh vs uh kp1 so I'm going to paste that in and I'm just okay and I don't need pyron to try to open it um so I've got my PM file here and obviously you'll have your own unique PM file because uh this will be specific to your AWS account that you're creating um but basically with this pen file I'm going to be running the next command here um which is we need to modify inside of this directory uh the permissions on this pen file to be read only so that we can actually use it for authenticating to the ec2 instance and then the next thing if you don't run this command you will get an airor message when you try to run SCP to upload it to our instance but I'm going to run um this so the command is SCP dasi and then we're going to put in the path which is that local PM file so this is how we're authenticating and now what we're going to do is we're going to tell SCP what are we uploading and where's this destination so we're going to be uploading the docker file app.py as well as requirements.txt and specifically this is going to go to that ec2 user account at and then now we need to look up the public ipv4 address of our uh ec2 instance and if you're using ec2 instance connect on AWS they tell you what the public IP is for this guy so I'm just going to copy that and we're going to paste that right there and then we're going to do a colon and now also we have to tell uh SCP where on this ec2 instance do we want to actually save this stuff so now that I'm in my downloads directory if I run PWD to print the working directory I can see the full path of this thing so I'm just going to copy that go back to here paste that in and then we're going to uh try to run this command it's going to say are you okay with this I'm just going to type in uh yes and you can see that now what's happened is uh We've successfully uploaded just three files so it's tiny uh to our ec2 instance if I go back to uh Chrome which has our ec2 connect on it and I run LS you can see we now have these files that came from our local computer um so now that we've done that the next step I'm going to do here is I'm going to actually build this Docker container so I'm going to run docker build- T to tag it we're going to call this whatever we want I'm going to call this ec2 D flask that's the image name and then we're going to tag it I'll call it v1.0 and um we're also going to pass the DF flag and say to build this off of the docker file uh and I forgot to do that but I need to have a space and then a DOT right there and so I'm getting this permission denied air if you do get this err um what you need to do is in your command you need to type in sudu to have those elevated Privileges and so now what's happening is based on the contents of the docker file uh Docker is building us an image from it so it's actually downloading everything that we need so that just finished that was pretty quick um and now what we're going to do is uh we can see the image that we just built by running the command Pudu Docker images you can see that we now have that C2 flask V1 image that we just created The Next Step here is we're going to actually run this image as a container so we can turn our little ec2 box into a flas capab just so we can play around with it and test it um and to do that I'm going to run this command so uh need that Pudu Docker run DT and then we're going to pass in the uh name I'm sorry super do run DD and detach mode d P to assign the port bindings so I'm going to be assigning Port 80 on the host that ec2 box that's going to be what uh where those HTTP requests go and that's going to go to Port 5,000 on the container which is the default port for flask um and so we've done that we're now going to give it the image name which is that ec2 flask colon and then v1.0 and um we're going to run this Command right here and so we're not getting any response because we're running this in a detach mode but now if you want to to actually see your Docker container running in real life if we copy this IP address and I open up a new tab and I paste this in and I go to Port 8 you can see that we're now getting a response from our flask app running in a Docker container on our ec2 instance so this was one method to very manually upload and run a Docker container on our ec2 instance I'm all in the free tier here um if we wanted to be a little bit more nuanced and sophisticated uh we can start using ECR to actually upload our build artifacts from our local computer or from like a build server to a staging place like ECR which would then put them onto our ec2 box um but you know just for understanding this concept I think this is a good start um the other thing I'll say here is now that our containers running if we run the command at Docker PS and I need to have the Pudu um you can see that we now have our container running and so if you wanted to actually stop this you run sudu Docker stop and then paste in that container ID and now if we go back to here and I try to hit my page again you can see it's not working because our dark container is no longer going to respond to our requests and so um that's part of the cleanup once you're done playing around with this thing and the last thing I'm going to do here I'm going to close out this tab we're going to go back to the ec2 instance's homepage and I'm going to make sure to go to my instance State and click terminate because I don't want to be build for this stuff when I'm not using it so um that is the cleanup and that is how you can get started using uh Docker containers on an ec2 instance all on the free tier so hope this is helpful stuff thanks all for watching for any questions and be well
Info
Channel: Vincent Stevenson
Views: 5,172
Rating: undefined out of 5
Keywords:
Id: qNIniDftAcU
Channel Id: undefined
Length: 11min 34sec (694 seconds)
Published: Tue Oct 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.