Use Terraform to Build a Docker Image and Push the Image Docker Hub : Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another lecture in this lecture we will use terraform to deploy this website on a docker container this is the second part to the previous lecture we did in the previous lecture we deployed this website manually in the management console and in this lecture we will deploy this website on a docker container using terraform before you complete this lecture watch the previous lecture we did on how to deploy this website on a docker container in the management console to start this lecture the first thing we will do is create a repository in docker hub we will push the docker image to this repository when we build it so what i want you to do is go to your docker hub account in your docker hub account select repositories up here then click create repository give the repository a name i'll call the repository techmax and i'll add teraform at the end once you've given the repository a name you can enter the description i'll use the same name as the description and i'll add repository at the end so i'll call it techmax terraform repository once you've given the repository a description select public and click create we have successfully created the repository when we use terraform to build our docker image we also use terraform to push that image into this repository the next thing we need to do is create a folder for our terraform project on our computer so i'll minimize this on my desktop i'll create a folder for this terraform project right click on your desktop then come down to new and select folder then give the folder any name you want i'll call this folder techmax docker project the next thing we will do is open this folder in visual studio code so open visual studio code once you open visual studio code to open the folder we just created select file and select open folder then browse to the location where you saved the folder the folder is on my desktop i'll select desktop and i call the folder techmax docker project select the folder name and click select folder once you've opened your project folder in visual studio code the first thing we will do is create our shell script file the shell script file will contain the commands we will use to install docker on our ec2 instance build our docker image and push the image to docker hub to create your shell scripts file right click anywhere in here and select new file give the file a name i'll call the file build docker image.sh this is the name i'll give the shell script file you can also call the shell script file any name you want but make sure you have dot sh at the end once you've given your shell script file a name press enter the next thing i want you to do is open the reference shell script file that i created for this lecture in this video's description this is the reference file that i created for this lecture once you've opened it what i want you to do is select everything in it i'll press ctrl a to select everything then right click to copy once you've copied everything in this reference file close it then in your shell script file paste it in there once you have pasted the command scroll up if you look at this shell script file you will notice that the commands are the same exact commands we ran when we completed this lecture in the management console next the only thing we would do is modify this shell script file so terraform will be able to run it and to modify this shell script file the first thing we did is hide the shebang in the beginning in order for terraform to be able to run your shell script file you need to add a shebang in the beginning once you've added the shebang in the beginning anything you see in this file that has a hashtag this i just comment to myself when terraform runs this shell script file it is going to ignore them so here i just have notes to myself the first thing i need to do is create a repository in docker hub we already create that then we are going to launch an ec2 instance open port 80 and port 22 but we use terraform to create the easy to instance shortly once we've created the ec2 instance we will run these commands on the ec2 instance the first command here is going to install docker on the ec2 instance once we've installed docker on the ec2 instance we will create a docker file and add that docker file to the ec2 instance that is this comment here and we will create our docker file shortly as well once we've created the docker file we will run this command to build our docker image then we will log into docker hub account once we've logged into our docker hub account we will take the image a new name and once we've done that we will push that image to our docker hub repository the last command here is what we are going to use to start the container to test that the image is working so this is the shell script before we save this file there are some things i want you to modify on this file the first one is on line 16. here you need to enter the name you want to call your image when you build it remember terraform is going to run this script for you so you won't be able to interact with it to enter your image name so all we need to do is enter the image name now then when terraform runs it it will use that image name so where i have this less than and greater than we need to enter a tag name there i'll remove this and i'll call it techmax you can give the image any tag name you want once you enter the image name also make sure there is a period here and there's a space between tech marks and the period so there's a space between here and there's also a space between here that's the first thing we need to modify on this file the next thing we need to modify on this file is on line 19. when we build the docker image here we will push that docker image to docker hub in order to push the image to docker hub the first thing you have to do is sign in into your docker hub account so here when terraform runs this shell script for us we won't be able to interact with it to enter our password so this command will allow us to be able to add our password to this shell script when terraform runs it this is the command terraform will run to log into your docker hub account so what you need to do here is change this to your docker id so i want you to remove this and enter your docker id to get your docker id you can go to your docker hub account in your docker hub account this is your docker id here so that is what you need to enter here once you have add your docker id the next thing you need to do is create a text file on your computer and store the password to your docker account in that text file when terraform runs this command you will get the password for your docker hub account from the text file the reason why we need to enter it this way is because remember in the previous lecture when we run this command to log into our docker hub account we have to enter our password but when terraform runs this command there won't be a way for us to interact with it to enter our password so we need to pass a way for terraform to read our password when it runs this command the next thing i want you to do is create a text file on your computer you can give the text file any name you want but to be consistent with this lecture i call the text file mypassword.cxt we can also create the file we will store our docker hub password in in our project folder but the reason why i don't want to do it this way is because for example if you commit this code to your github repository then you will be exposing your password so instead we will create the password file on our computer and we will just reference it from there to create the password file on your computer i want you to come up here and double click here that will open a new file once you've opened the file where it says select a language click it then type txt in the search box once you type txt select plain text then the next thing you will do is enter the password for your docker hub account here for example let's say this is the password for my docker hub account enter the password for your docker hub account like this once you've entered it we are going to save the file anywhere on our computer i'll select file and i'll select save it is going to open this and i'll save it on my downloads directory so click downloads or anywhere on your computer and save the file in there i'll call the file name my password once you've entered the file name make sure the file type is plain text and click save to explain what we just did we created a plain text file we call the file mypassword.txt then in that file you will enter the password for your docker hub account and save it anywhere on your computer once you've saved it you can close it just to show you again this is the file it is saved in my downloads directory and the name of the file is my password so whatever name you call the file here let me right click here just to show you i'll copy this name whatever name you save the file as that is the same name you need to enter here so here i'm going to paste that name here this part is very important whatever name you call the file you saved your password in make sure you give it the same exact name here then add the extension dot txt this is all we need to do for this command the next command we need to modify is this command on line 22 so here we need to enter our tag name again this is the tag name we'll copy this and paste it here once you've added the tag name the next thing we need to modify is the repository name this is the repository we created previously that we will push our docker image to let's go back to docker hub in docker hub this is the repository i created previously that i want to push the image to just in case if you are not here to get to your repositories select repositories here then you will see a list of your repositories here select the repository you want to push the image to in my case it is this repository i'll select it this is the name of the repository i want to push the image to so i need to enter the name of this repository exactly how i see it here in my shell script file i'll minimize this here the reason why i don't want to copy paste it is when i copy paste it it always paste it in a wrong way so i'm just going to remove this the name of that repository is my docker id which is aos note 77 then forward slash and the name i gave that repository is techmax terraform the next thing we need to do is modify the command on line 25 and here we will also replace our repository name so select this and paste your repository name there make sure there is a space between the push and your repository name and we will also do the same thing on line 28 replace the repository name here this is all you need to do to create your shell script file once you've created your shell script file save it i'll select file and click save the next thing we need to do is create the docker file in our terraform projects folder so in here i want you to right click select new file give the file a name and call it docker file so make sure you type it with a capital d and call it docker file once you've entered the name press enter next i want you to open the reference docker file that i created for this lecture in this video's description this is the docker file where we use to build our docker image so what i want you to do is select everything in this file i'll press ctrl a to select everything right click to copy once you copy everything in this file i'll close it then in your docker file paste everything in there once you've pasted it save your file if you want to learn how to create a docker file please watch this video and i'll also leave a link to it in this video's description next we will create the terraform file that we will use to launch our ec2 instance and run the shell script to create the telephone file that we will use to launch our ec2 instance in your project folder right click in here and select new file give the file a name i'll call it ec2.tf once you've given the file a name press enter once you have created the reference file open the ec2 reference file that i created for this lecture in this video's description this is the reference file we used to create our ec2 instance and run the shell script what i want you to do is select everything in this reference file i'll press ctrl a to select everything right click to copy it then i'm going to close this and in my ec2.tf file are pasted there once you've pasted it scroll up the first syntax in our institute.tf file will allow us to authenticate with our aws environment before you can use terraform to create a resource in your aws environment first you have to authenticate with your aws environment to authenticate first you specify your provider block and the cloud provider we want to authenticate to is aws the first option we have here is region and this is the region we want to create the resource in for this project i am using us east1 the second option is profile and this is the named profile that i have configured on my computer that contains the access key id and sacred access key of a user in my aws account that has programmatic access if you don't know how to configure a named profile on your computer i'll leave a link to the lecture in this video's description once we have authenticate with our aws environment we will launch our ec2 instance in the default vpc and this syntax will create a default vpc in your aws account if you don't have one if you already have a default vpc in your aws account terraform will use that default vpc and if you don't have one this syntax will create a default vpc in your aws account once you've created the default vpc in your aws account the next syntax will allow you to get a list of all the availability zones in the region where you are launching the ec2 instance for example in this project i'm using the usb1 region so this syntax will allow me to get all the availability zones in usd1 and i can launch my ec2 instance in any one that i want once we have get a list of all the availability zones in the region where we want to launch the ec2 instance next we will create our subnet and this resource will create a default subnet in your aws account if you don't have one if you already have a default subnet in your aws account teraform will use that subnet but if you don't have a default subnet typhon will use this syntax to create a default subnet in your aws account and it is going to launch the ec2 instance in that subnet when you select the availability zone you want to launch your ec2 instance in all we did is we copied this resource here and pasted here so data is data aws availability zone is the data source here and the reference name is the reference name you see here all we did is we add a period between them and the attributes we are referencing is name once you enter the attributes you want to reference in bracket you enter a number so this number represents the availability zones in the region for example if you enter 0 here you are telling terraform when you get a list of all the availability zones in the region you want to use the first availability zone if you enter one in here you are telling terraform that when it gets the list of all the availability zones in the region you want to use the second availability zone this is called indexing for this example i will leave it at zero and this is going to select the first availability zone in the region the next resource is going to create a security group for the ec2 instance and for this resource we are going to open two ports port 80.22 this is the resource type to create a security group and this is the reference name i've given it the first argument is name and i've given it a name here but i'm going to change this name to docker server security group once you have entered your description the next argument is the vpc id and this is the id of the vpc we want to create the security group in remember we create the resource for our vpc up here so scroll up and what we will do is copy the resource type and the reference name once you copy it come back down paste it in here remove the double quotes between them add a period then come all the way to the end add another period and the attribute we want to reference is id type id in there next we will add the ingress rule and the first ingress rule we will add is for http access this is how we open port 80 on our security group for the description i have entered http access for the front part type 80 for the 2 port type 80 for the protocol type tcp in double quotes and for the side that blocks this is going to allow traffic coming from anywhere on the internet to allow traffic coming from anywhere on the internet beside that block is going to be 0.0.0.0.0 so to enter it first we are going to type a bracket and in that bracket enter your cider block in double quotes the next english rule we will add is for ssh access this is going to open port 22 on the security group for the description i have entered ssh access for the front port we will enter 22 for the 2 port we will also enter 22. for the protocol we will enter tcp in double quotes and for decider blocks the cider block we are going to enter is also going to allow us to ssh from anywhere in the world we need to enter it this way so we will be able to ssh into our ec2 instance using terraform i will explain later on so for decider blocks we want to allow access from anywhere on the internet copy this and paste it here scroll down the next rule we will add is for the egress rule for the front part we will enter zero for the two part we will enter zero for the protocol we will enter minus one and for decider blocks this is also going to allow traffic coming from anywhere on the internet so we are going to copy this and paste it here the next argument is tags and for the tag name i've called it docker server security group scroll down to launch the ec2 instance we will use amazon linux 2 ami and this resource block will allow us to select the latest version of the amazon linux 2 ami the next resource is going to launch the ec2 instance this is the resource type to launch an ec2 instance and this is the reference name i've given it the first argument is ami and this is the id of the ami we want to use to launch the ec2 instance remember we created the resource for the ami here so what we want to do is select everything from data all the way to amazon linux 2 copy it then come down here paste it there then remove the quotes add a period come here remove the quotes add a period then come all the way to the end add another period and type id the next argument is the instance type we want to use for the ec2 instance for this ec2 instance we will use t2 micro so in double quote type t2 micro make sure you type t2 dot micro the next argument is the subnet id and this is the id of the subnet we want to launch the ec2 instance in remember we created the resource for our subnet up here i'm going to scroll up this is where we created the resource for our subnet so what we will do is copy the resource type and the reference name once you copy it come back down then paste it here remove the double quotes add a period then come all the way to the end add another period and type id the next argument is the vpc security group ids and this is the id of the security group we want to attach to this ec2 instance remember we created our security group up here so we are going to copy the resource type and the reference name once you copy it come back down then paste it here before you paste it entire bracket and paste it in the bracket then remove the double quotes add a period come all the way to the end add another period and the attribute we want to reference is the id the next argument is the key name and this is the name of the keypair we want to use to ssh into the ec2a stats to show you the name of my keypair i'll go into the management console in the management console type ec2 in the search box and select ec2 under services on the ec2 dashboard scroll down here under network and security select key pairs and here i will use this keypad so what i'll do is select it press ctrl c to copy it for the keypair you will use for this lecture make sure the file format is pen format so if you don't have a pen format keeper you can create a new keypad and make sure it is paired format once you've copied the name of your keypad come back to your terraform file then in double quotes we'll paste the name of the keypair here the next argument is tags and we will give it a tag name docker server scroll down once we have launched the ec2 instance the next resource block will allow us to ssh into the ec2 instance then we will copy all of our file both the shell script file and the docker file into the ec2 instance then we will run our shell script file on the easy to install to install docker on the ec2 instance build our docker image and push the docker image to docker hub to specify this resource block the resource type is no resource and this is the reference name i've given it when you specify in our resource a null resource will create an empty resource block and in that empty resource block we will specify the things we want to do so here the first thing we want to do is we want to ssh into our ec2 instance this is the syntax we will use to ssh into the ec2 instance so here we have connection for the option type we want to ssh into the ec2 instance so in double quote type ssh the next option is user and this is the username of your ec2 instance remember the username of our ec2 instance is ec2 iphone user so we will enter it in double quote once you've entered the user name of your ec2 instance the next option is private key and this is where the private key pair is stored on your computer the private key of that key pair is located in my downloads directory this is my downloads folder this is the private key of that keypay and note it is in my downloads directory so what i need to do in terraform is i need to specify the path to that private key on my computer to specify the parts to the private key i will enter it like this in double quotes i'm going to enter detailed assign forward slash downloads forward slash the name of the keypad the name of the keypair is ec2key.pam so here i'm going to enter ec2 underscore key dot pen to explain what this means this tilde sign you see is the home directory on my computer i'm telling terraform to look in the home directory and on the home directory there's a folder called downloads and in that downloads folder there is my keypair in there that is what this means so wherever the private key of your keypair is stored on your computer you need to enter the parts to that private key here the next option is host and this is going to be the public ipv4 address of your ec2 instance remember we create our ec2 instance resource up here so copy the instance type and the reference name then come back down we will paste it here we will remove the double quotes add a period then come to the end add another period and the attribute we want to reference is the public ip so type public underscore ip the next syntax will copy the password file for your docker hub account from your computer into the ec2 instance to copy the password file we will use provisional and the type is file the first option here is source and this is where the file we want to copy is located remember i created my password file in my downloads directory so if i open my downloads directory my password file is here what i need to do in terraform is i need to specify the path to this file to specify the path to my password file it is the same thing as what i did up here so i'll copy this and i'm going to paste it here and all i need to do is modify the name of the file the name of the file is going to be mypassword.txt to show you what i mean i'm going to go back to my downloads directory this is the file i'm going to copy the name then in my terraform file i'll paste it here and add txt at the end the next option is destination and this is where we want to copy this file to on our ec2 instance for this project we will copy this file on our ec2 instance home directory so to enter the home directory on your ec2 instance you will enter it like this first enter forward slash home forward slash ec2 user then copy the file name here and paste it after the forward slash so what this means is we will copy the mypassword.txt file from my downloads directory into the home directory of my ec2 instance just like we did up here we will use the next syntax to copy the docker file from our computer into the ec2 instance so for this source this is where the file is located on your computer the docker file is located in the same directory as my project folder so in here for the source all i have to do is just enter the docker file name in double quote enter the name of the file which is docker file once you've entered the name of the file you want to copy on your computer into your ec2 instance then on that destination we will specify where on the ec2 instance we want to copy the file to we will also copy this file to the home directory of the ec2 instance so what we can do is copy this here and we will paste it here then all we have to do is modify this part i'm going to remove this part and i'm going to add the name of the file the name of the file that i want to copy to the home directory of my ec2 instance is dockerfile so i'll paste it here and again just like we did up here we will use this syntax to copy our shell script from our computer into the ec2 instance for this source we will enter the name of the file we want to copy on our computer the file we want to copy is also in our terraform projects folder so all we have to do is specify the name of the file the name of the file is build docker image so you need to enter the name of the file the way you type it here down here what i like to do is i like to right click on it and i'll select rename then i'll press ctrl c to copy it then come here in double quote i'll paste it there i like to do this so that i don't type it wrong once you've pasted enter dot sh at the end this is the name of the shell script file we want to copy from our computer and on that destination we will specify where on our ec2 instance we want to copy it to we also want to copy this file into the home directory of our ec2 instance so we can copy this here and paste it here then here we just modify the name the name of the file is builddockerimage.sh copy this and paste it here once we have copied the shell script file from our computer into the ec2 instance the next syntax here will allow us to run the shell script file on the ec2 instance to run the shell script file on the ec2 instance we are also using provisioner and the type is remote exact for the inline we are going to come down here and the first thing we are going to do is we are going to set the permission on our shell script file to make it executable i notice my note is wrong here so i'm just going to replace it now like i was saying the first inline command we will enter here will allow us to set the permission on our shell script file to make it executable to set the permission on your shell script file to make it executable the command is going to be sudo chmod plus x so in double quote type sudo chmod plus x then enter another space then you need to enter the file you want to change the permission on this is the file it is our build docker image.sh so we will tell it where the file is located the file is located in the home directory of the ec2 instance and the name of the file is build docker image dot sh copy it and paste it here this is the full command we use to make the shell script file executable remember the command is sudo chmod plus x then the path to the file once you've entered it enter a comma here then come down here once you have made the file executable the next thing we will do is execute that file to execute that file we are going to enter double quotes then we are going to type sh space and the path to the file we want to execute the path to the file we want to execute is also this so we will copy and we will paste it here so make sure there is a space between your sh and the path to the file so what we are telling teraform is we want to execute this build docker image.sh file and the file is located in the ec2's home directory next we are also going to add a comma here then scroll down the next argument we will add here is depend on so what this means is first we want to launch our ec2 instance which is this resource then once we've launched the ec2 instance the entire form can ssh into the ec2 instance and copy our file and execute the file so that's what this means so all we need to do is copy the resource type here and the reference name of the ec2 instance copy it then come down here and that depend on paste it in there remove the double quotes between them add a period come all the way to the end and remove the quote so what this means is terraform will create the ec2 instance for us before running this resource block the last syntax will print the url of the container and we can use this url to access our website i notice i have a typo here this should be container so i'll copy this and paste it here once we start our container remember to access our website on it all we have to do is use the public ipv4 dns and here for the value we are using join and we will join http colon forward slash with the public dns of the ec2 instance this is the terraform syntax you can use to build any docker image then push the image to docker hub and start your container to test that the image is working the next thing i want you to do is save your file i'm going to scroll up i'll select file and select save all once you've saved your file the next thing we will do is open the terminal and run terraform apply and when we run terraform apply terraform is going to launch an ec2 instance it is going to run our shell script to install docker on the ec2 instance build our image push the image to docker up and start the container just to show you if i go to my docker hub account in my docker hub account this is the repository i want to push the image to if i click refresh you can see there isn't any image in this repository once we run terraform apply we should see our docker image in this repository when terraform has finished building all the resources in our terraform file so let's go back to our terraform file i'm going to open the terminal right click anywhere in here and select open in integrated terminal in my terminal i'm going to drag this up so you can see it much better the first command we will type is terraform init to initialize with our aws environment once you type terraform init press enter it is now initializing with my aws environment and i'll give it some time to finish initializing i have successfully initialized with my aws environment the next command i'm going to type is terraform apply once you type terraform apply press enter when you type terraform apply and press enter the first entire form we do is show you the plan we can see the plan here and this is just the list of resources terraform will create in our aws account once you've reviewed the plan the next thing you can do is down here it is asking you do you want to perform these actions we will type yes and press enter and there you go terraform is now creating the resources in my aws account and i'll give you some time to finish creating it you can see at this point terraform has launched the ec2 instance and now it has ssh into the ec2 instance and it is executing the shell script we'll give it some time to finish executing the shell script trafform has launched the ec2 instance installed docker on the ec2 instance build my docker image pushed my docker image to docker hub and start the container to test the image the first thing we will do is use the url we printed here to test whether we can access the website that is running on the container so what we'll do is click follow link then click open and there you go you can see we can access our website and this website is running on a docker container now that we have verified that the image we built is working and we can access our website that is running on the container the next thing we need to do is go into our docker hub repository and check whether the image we pushed is there i'll go to my docker hub repository this is the repository i pushed the image to all i'm going to do is click refresh up here and there you go you can see in this repository the image is there another thing i want to show you is if we go into our aws account in the ec2 management console if you select the ec2 dashboard i'm going to refresh here and there you go you can see we have one ec2 instance running i'll select it and here you will see the ec2 instance we created with terraform so what happened is terraform created this ec2 instance we ssh into it and we run our shell script command to install docker on it build our docker image and push that image to docker hub we also use our shell script file to start our container to test that our website is working you can use this terraform file to build any docker image and push that image to your docker hub account now that we have verified that everything is working we will run terraform destroy to clean up our environment let's go back to our terraform file in our terraform file to clean up your environment type terraform destroy once you type teraform destroy press enter when you type terraform destroy and press enter the first thing it will do is show you the plan and here we are just going to type yes because we want to delete everything once you type yes press enter entire form will only delete all the resources it created in your aws account but the image we push to our docker hub account will always be there this is how you create the terraform file to install docker on an ec2 instance build your docker image push your docker image to docker hub and start your container if you have any questions on this lecture or there's any part you don't understand please leave your comments below thank you and i'll see you in the next lecture bye
Info
Channel: AOS Note
Views: 6,358
Rating: undefined out of 5
Keywords: docker terraform aws, terraform, docker, aws, terraform tutorial, devops, infrastructure as code, terraform aws, ec2, docker container, docker tutorial, docker tutorial for beginners, docker on aws, docker container on aws, terraform tutorial for beginners, how to install jenkins in aws ec2 instance, terraform cloud, terraform modules, docker container tutorial, aws terraform tutorial, aws ec2, devops tutorial, tutorial, ci/cd, docker image, aos note, docker hub, docker build
Id: eEU6gae494Y
Channel Id: undefined
Length: 42min 37sec (2557 seconds)
Published: Wed Aug 31 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.