Deploy Docker project in ElasticBeanstalk

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone so in this video you will learn how to deploy a Docker project into elastic Beanstalk in my case I have an sjs project which is a framework for node.js with a more complex configuration so here on my package.json I'm using a library called bcrypt but Big Crypt requires Python 3 to be installed and configured on your machine for encrypting user password so I'm using big trip to encrypt user passwords and Big Crypt requires Python 3 to be properly installed for doing this you have two options you have you can create uh EB extensions folder on your elastic big stock project and then you put your configuration into EB extensions I'm going to attach some links in the video so we can learn more about it and I also have another video that I used EB extensions for configuring elastic beamstock or you can create a Docker project with your configuration in it and then do everything that needs to be done using docker but before we proceed if you like this kind of content just give a thumbs up and subscribe to the channel so you can learn more about Docker python JavaScript typescript and so on and don't forget about it the more you learn the better you become and more value you can provide okay so let's do this first of all let's open up AWS console and once you arrived here search for elastic B stock if it's here are already available for you then just click here otherwise search for elastic Beanstalk on the search bar perfect now let's create a new application I'm gonna call it npcsg Docker project now on the platform section let's choose docker and then I'm going to configure more options because because I want to make sure that our machine has some RAM for not having memory issues while I try to to build the machine I'm going to click to configure more options and then I'm gonna scroll down to the capacity section this is quite similar to what we did before on my previous video so I want to demonstrate something to you I'm going to create two environments the first one will be using x86 and another one will be using our arm64 and then you will understand something at the end of the video so watch out and then you will understand something which is really important for you to deploy your project successfully on elastic install so first of all this project will will use the next 36 architecture I'm gonna choose a T3 dot medium I think this one is a four gigabytes machine we can check this out on AWS pricing calculator then click to create estimate let's search for ec2 because I lost a B stock uses cloud formation and ec2 under the hood so let's click gc2 Joey situ and then let's search for T3 medium figure and then I'm gonna search for T3 dot medium and then you will see that this machine has four gigabytes of RAM so probably it's gonna work pretty well for our project and then I'm gonna choose D3 medium and then I'm gonna click on Save perfect now let's check the security section if you want to connect with your machine using SSH then you have to select a key pair just like we did previously and then let's create the app perfect while the project has been created I'm going to create another one use the rm64 so let's go back here and create a new environment in this case it's going to be a web server as well I'm gonna call it npcsj darker arm 64. if you put the name that you want here this is just for testing and then I'm going to choose Docker as a platform and then I'm going to click to configure more options perfect I'm going into the capacity section click to edit in this case I'm going to pick an rm64 machine and then let's search for a machine that has arm 64 architecture and four gigabytes of RAM I'm gonna choose a t4g medium in this case T4 g dot medium perfect and then let's click on Save and then on this the secure the section if you want to connect with your machine using SSH then again just go into the C2 key pair and integrated key pair and again if you don't have a key pair it needs to go into dc2 section and create a new key pair and then let's click just save in this case this is an rm64 machine confirm it perfect this is a this is an arm 64 machine and then let's click to create environment [Music] perfect in a few minutes both oh perfect the first machine is already configured great so while the second machine is being configured let's try to deploy our code into this Docker environment again let's go back to our code this is this is my Docker file that runs my code and then let's zip this whole project and deploy the zipped file into elastic being installed so here I have a file a simple batch script file which is capable to zip my project and to create a zipped file and then I'm going to send the zip file into elastic Beanstalk so let's do this so here I am on the terminal I'm gonna find this scripts folder into the terminal perfect here you can see the file on the terminal then let's run it their project perfect this is the file and then I'm gonna try to send this file into elastic Beanstalk now let's click to upload and deploy and look for the file so this is my file that I've just created and then I'm going to click to upload and then upload the file deploy it by clicking on deploy perfect now it's going to take a few minutes and then if you see in the if you see the green light it's perfect your project is going to be correctly deployed if not then you need to make some extra changes that we're going to do on the next section and then I'm going to do the same thing on the other project on the rm64 machine or this is the one okay I'm gonna deploy the same file here upload and deploy now this is going to take a few minutes for deploying the projects so let's see it's being deployed here okay great so after some time you can see that both projects failed to be deployed on the last v-stop and this is because my Docker file is quite big and and it takes some time to Docker image to be built so if you want to investigate more the the causes for the issues you can go into the log section and then you can have like a brief version of what happened clicking on the last 100 lines and then I click on last 100 lines and then we'll click to see it so if you can't see what happened here in my case I can only see that an error occurred into the build section then it can go back here and click to see the full logs but in my case I I already know what happened it's because my Docker file is quite big so then I need to Cache the machine configuration somewhere else and then and then read from that new Docker image to build my new Docker image so this might be a bit confusing basically what happened is because my Docker file that the creation of my Docker image is quite big so I need to move the machine configuration somewhere else and and that you cache this machine configuration and then read from this new image into this Docker image so basically we're going to Cache this somewhere else and then read from the cache into here this is gonna speed up the the creation for our Docker image into elastic Beanstalk and then probably I'm not I'm not gonna have time out issues anymore so there are multiple ways to do this if you want to keep everything inside into the AWS ecosystem I highly recommend to you to use ECR ECR or elastic container registry so what is elastic container registry this is a place for you to put your Docker images privately or publicly on AWS this is equivalent to Docker Hub but inside the AWS ecosystem so let's do this I'm gonna create a new repository on ECR click in here and then I'm gonna call it npcsj Docker project machine you can put the name that you want here and then I'm gonna select a private Docker image Repository and then let's create it okay on lowercase letters so I'm gonna call it [Music] okay great so here we have an empty repository created on this ER and then I'm going to I'm going to push my code to here my Docker image to this repository and then I'm gonna read from the repository created here into the elastic Beanstalk project so let's go back to our code this is the docker file I'm basically going to split this into two parts so from from the copy from this line to the end this is what is dynamic which means this is what exactly is related to my code and previously actually from the exposed line so from the line 45 and onwards it's our project and before this is the configuration for the machine so I'm gonna copy this whole part here actually I'm gonna cut and then I'm going to create inside this scripts folder I'm going to create another folder called docker images and then here I'm going to create an empty Docker file I'm gonna move the code to this place this is this is the docker file that which is going to create the docker image that configures the machine so here we are reading from Amazon Linux 2. in your in your case you can use whatever image that you want but I'm using Amazon X2 to keep the whole thing inside the AWS ecosystem and then here I am configuring the machine to install Python 3.10 and then I install some extra libraries that I install node and then I install yarn and then I install an sjs CLI oh I have a duplicate here yes so so this is it basically and now that now that I have this new Docker file here I am going to build this image and push it to ECR for doing this you can click here into ECR to view the push commands and then basically follow you follow these steps to push your Docker file into ECR I'm gonna copy the First Command and then I'm going to go into that folder that contains this new is this new Docker file City Docker images [Music] perfect here on the terminal I can see my new Docker file and then I'm going to run the First Command okay so in my case the command failed because I need to start the docker image on my machine depending on your on your operating system it's already running but in my case since I'm using Mac I need to choose start the docker engine if we're using Ubuntu or another Linux version maybe it's already running on your machine but in my case I need to start the engine okay perfect now that the docker image is running here I am going to run the command again [Music] perfect now the First Command succeeded let's run the next one I'm gonna build the docker image perfect now I'm gonna tag the Neo darker image and then I'm going to push this new Docker image to ECR [Music] okay great so after some time the push commands are complete and then if we if we go back into AWS console and then refresh the page you will see that now I haven't the the latest version here which is the one that I post previously from my machine in into ECR okay great so the parts that we removed from our original Docker file we're gonna read from this new ECR created now instead of run the commands directly so I'm gonna click here on into copy URI so I'm gonna read from the Neo eroi so that's pretty much it basically so Docker now we will read from ECR instead of running the commands directly so this ECR has the cached version of all the commands that I executed here so the deployment will be faster and probably we're not gonna have timeout issues anymore okay great and then I just changed this line here and then I'm going to delete the create the file that I created previously and then I'm gonna run the script to zip the project again I'm gonna run zip project right this is the new file let's go back into elastic B stock and then let's go into both projects I'm going to deploy this file into the rm64 environment I'm going to click here to upload and deploy choose the file this is the file uploaded and then deploy and then I'm gonna do the same into the x86 environment go back here [Music] click to upload and deploy choose the file the same file and upload and deploy okay great okay perfect so after a few minutes you can see that the arm64 version was deployed successfully and then if we click here you can see the message from my app will the world from the sjs app which is the message that I defined here on my application a controller hello world from nest.js app and then the when we go back to see the x86 version you can see that the deployment failed this is what I wanted to explain from the beginning of the video because when I try when I had to create a new Docker file with the cached version from the machine but because the because that I had to build this Docker image on my computer is gonna follow the same directives from my processor and the processor for my computer is the M1 Pro from Apple and the ammo Pro M1 Pro uses the arm64 architecture so because of this this Docker file and the docker project will only work if I use the same architecture on elastic Beanstalk so since I had to create a Docker image here and deploy it into ECR from an arm 64 processor and machine so the project and the ECR image will only work on arm 64 machines and it's not and it's not going to work on x86 machines so this is why the project was that was successfully deployed on the arm64 machine but it failed on the x86 and so if your computer uses an x86 processor probably it's going to be the opposite the x86 will work perfectly but the arm64 will fail and that's pretty much it besides this if you if your project needs to read from ECR just like my case here my Docker file is pushing the docker image from ECR if you need to do this into your elastic b style project you will need to create the permissions for the IAM role to do this so if we go into configuration [Music] and then we go into the security section click to edit so this is the IAM instance profile that I'm using AWS elastic B stock ec2 roll if I look for this role into the IAM section and that I scroll down to the roles and look for that role I can check the name again it's either less Alexa view stock is it your role so this is the roll you you will need this permission Amazon ec2 container registry read only so if you don't put this permission and then we try to to use ECR into your project just just like Mikey is here the product deployment will fail again so we need to put this permission into your IAM instas profile so if your IAM is instance profile doesn't have this permission the deployment will fail okay great so that's it after doing this your deployment probably will succeed just like it happened here so you need to pay attention to this when you try to deploy a Docker project into a last admin stock if your Docker file is quite small then probably it's going to work in your first attempt then if you have a bigger Docker file probably you need to break it into smaller parts and then you need to Cache the machine configuration just like I did here into a separate Docker file and then you need to build this Docker image and then push the docker image into ECR and then you read from the new ECR into your original Docker file and then you try again the deployment but you need to make sure that the architecture for the machine running on elastic v-sock uses the same architecture that you that you used to build the docker image and then you also need to make sure that your IAM instance profile has the permissions to read from ECR and that's it after doing this you will have a successfully deployed Docker project into a lasting Beanstalk so if you have any doubt just ask me on the comment section and then I will answer you later on so if you like this kind of content just give a thumbs up and subscribe to the channel and don't forget about it the more you'll learn the better you become and more value you can provide I hope you liked it and I'll see you in the next time
Info
Channel: Mpcsj Tech Tips
Views: 4,302
Rating: undefined out of 5
Keywords: docker, elasticbeanstalk, nodejs, nestjs, python3, bcrypt, yarn, aws, cloud, server, linux, javascript, typescript, nest js, iam, ecr, elasticcontainerregistry, timeout, arm64, x86, docker tutorial, learn docker
Id: wqM_B5Iyhcw
Channel Id: undefined
Length: 21min 27sec (1287 seconds)
Published: Mon Dec 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.