How to Build Docker Image and Push it to Docker Hub : PART 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another lecture in this lecture you will learn how to deploy this website on a docker container before you start this lecture please sign up for a docker hub account if you don't have one because you will need it to complete this lecture i'll also leave a link to how you can sign up for a free docker hub account in this video's description also you can watch the entire project on how to build containerized application using docker amazon ecr and amazon ecs on my website this is the node we used to complete this project and i'll also include this node in this video's description once you have signed up for your docker hub account according to our notes the next thing we will do is create a repository in docker hub we will use the repository to store the docker image we viewed in this lecture so the first thing we need to do according to our node is go into our docker hub account and create a repository let's go to our docker hub account in your docker hub account to create a repository select repositories up here then click create repository give the repository a name the name of this project is techmax so i'm going to enter that as the repository name or you can give it any name you want once you've given the repository a name give it a description once you have entered the description for your repository under visibility select public and click create we have successfully create a repository in docker hub and when we build our docker image we will push that image into this repository once we have created the repository in docker hub the next thing we need to do according to our node is launch and ec2 instance and we will open port 80 and port 22. so let's go to the management console to launch an ec2 instance in the management console type ec2 in the search box select ec2 under services click launch instance and click launch instance again we'll give the ec2 instance a name i'll call it docker server once you've given the ec2 instance a name scroll down under application and os images select the quick start tab on this tab select amazon linux and it is going to be amazon linux 2. if you look here it is the free tier one make sure you have select amazon linux 2 which is the three tier one then scroll down under instance type select it to micro and it is also the three tier one then scroll down on that keypad select the drop down and select a key pair you have in your account if you don't have a keeper in your account you can click create new keypad here and i'll also leave a link in this video's description that shows you how to create a keypair in your account if you don't know how to create one under network settings click edit we will launch this ec2 instance in our default vpc so make sure your default vpc is selected here under subnet you can leave it at no preference or you can select this drop down and select any subnet you want it doesn't really matter then scroll down under firewall security groups we will create a new security group i will give the security group a name let's call it docker server security group once you've given your security group a name scroll down under inbound security groups rule we will add two rules the first row is going to be on ssh port 22 and the second rule is going to be on http you can see the ssh rule on port 22 is already automatically added and another thing you can do here is select the type of source that can ssh into your ec2 instance for security best practice it is always good to limit the type of source that can ssh into your ec2 instance to your ip address so if you select this drop down you can select my ip and it will limit the source stack and ssh into your ec2 instance to your ip address next we will add the second rule on port 80. click add security group rule then under type select this drop down and look for http once you see http selected http is on port 80 and under source type select this drop down and select anywhere anywhere means that any traffic coming from anywhere on the internet can access our website once you've added the rules scroll down and these are all the settings we need to launch our ec2 instance you can also review the settings here and once you've reviewed them click launch instance and there you go we have successfully launched our ec2 instance i'll click view all instances to see it and the instance is here let's go to our notes according to our note once we have launched our ec2 instance the next thing we will do is ssh into that ec2 instance and run this command to install docker on the ec2 instance so the next thing i want you to do is ssh into your ec2 instance so ssh into my ec2 instance i will use poti so the first thing i need to do is get the public ipv4 address of my docker server to get the public ipv4 address of my docker server i'll select it then under the details tab you will see public ipv4 address here click this box to copy the ip address once you've copied the address open party then under session we are going to type the username of our ec2 instance it is going to be ec2 iphone user then type add sign and paste the public ipv4 address you just copied there remember your host name will always be ec2 iphone user at the public ipv4 address of your ec2 instance once you've entered your host name the next thing we need to do is add our private keypad to enter your private keypay select the plus sign under ssh then select alt don't select the plus sign select alt then on alt click browse browse to wherever your private key pair is saved on your computer my private key pair is saved here this is it i'll select it and click open once you've add your private keypad click open then click yes there you go we have successfully ssh into our ec2 instance once you've ssh into your ec2 instance the next thing we will do is run these commands to install docker on our ec2 instance build our image and push that image to docker hub i'm going to dock this side by side with my body session the first commands we will run are these commands and these are the commands we need to install docker on our ec2 instance if you want to know how i got this command let me show you i'll scroll down here under my reference note i'm going to browse here this is the aws documentation where i found the command to install docker on an ec2 instance so if you scroll down here you can see installing docker on amazon linux 2. select this drop down and here these are the commands so the same commands you see in here are the same commands that i pasted in the notes i'm going to minimize this for now so i'm going to scroll back up here the first thing we will do is update our ec2 instance so i'll copy this command and paste it here press enter it is now updating my ec2 instance and i'll wait for the shell prompt to be available it has successfully updated my ec2 instance i'll type clear to clear my screen once you've cleared your screen i'm going to run the next command to install docker once you paste the command press enter it is now installing docker on the ec2 instance i'll give it some time to finish installing it it has successfully installed docker on my ec2 instance i'll clear my screen again i'm only climbing my screen so you can see the commands better then the next command we will run is the command to start docker i'll copy this command and paste it here press enter and it has start the docker service the next command we will run will ensure that the docker service is always in the start state even when we reboot our ec2 instance so copy this command this command is also optional but i always like to run it copy it and paste it in here press enter the next command we will run will allow us to add the ec2 user to the docker group the reason why you want to run this command is because when you start building your image with the docker commands like docker build docker login you don't have to add sudo at the end so i'm going to copy this command and paste it here and press enter according to the aws documentation i can show you i'll scroll down to here after you've run this command they want you to log out of your ssh session and log back in to pick up the changes so that's what we will do here i'm just going to close my ssh session here then i'm going to open party again to log back in once you log back into your ssh session we will run this command to test that we can run docker commands without sudo i'll copy this command and i'll paste it here and press enter and there you go we can run docker command without sudo i'm going to clear the screen once you have installed docker on your ec2 instance the next thing we need to do is create a docker file and a docker file contains all the commands we want to run to build our docker image i already created a video here that explains how to create this particular docker file i'll leave a link to that video in this video's description this is the docker file we use to build the docker image in this lecture i will add the link to this docker file in this video's description so the next thing i want you to do is copy everything in this docker file to copy everything in this docker file i'll select ctrl a then right click and select copy once you've copied everything in this docker file we will create a docker file on our ec2 instance and paste this command in it to create a docker file on your ec2 instance type sudo vi docker file like this please note that when i type my docker file the d is in capital letter so when you type your docker file make sure your d is in capital letter once you type sudo vi docker file press enter and this is going to open the vi editor to paste this command in our docker file on the ec2 instance first you have to be in insert mode so press i on your computer to go into insert mode if you look down here it is saying docker file when i press i this will change to insert before you can paste this command in the docker file you created on your ec2 instance you need to be in a set mode so watch when i press i on my computer you can see it has changed to insert this means that i can now edit this file so what i'm going to do is i'm going to paste all these commands in there there you go to paste the commands all i did was right click on my mouse once you have pasted the commands the next thing we will do is exit from the insert mode to exit out of it we are going to press the escape key and now we have exit out of the inside mode next to exit this file and save the changes we are going to type colon the second thing you are going to type is wq and exclamation mark so this is what you will type colon wq exclamation mark this means write quit so once you type it press enter there you go we have created the docker file on our ec2 instance and we have pasted the command in that docker file to show you what is in that docker file we are going to type cat and the name of the file the name of the file is docker file so i'm going to type d once you start to type the name of the file you can press the tab key and it is going to auto-complete it for you so what this command means is we want to see the content of this docker file that is what the card command does press enter and there you go you can see the content of that file here i'm going to clear the screen to understand the commands in this docker file please watch this lecture we did on how to install this website on an ec2 instance it is the same command that we used to install our website on the ec2 instance that we are using to create our docker file once we have created our docker file on the ec2 instance i'll close this i don't need it anymore the next thing we need to do is run this command to build our docker image the command we need to run is docker build minus t the t stands for tag and you give it a tag name so here i'm tagging my image techmax you can give your image any tag you want i'll copy this command and paste it here so remember the command is docker build minus t the t stands for tag and you can give it any tag name you want you can use the tag name that i'm using here or you can give it any tag name you want also make sure that you have a period at the end this means that the docker file we want to build is in the same directory we are running the command from once you've entered the command to build your docker image press enter and there you go we are now building the docker image when we are building the docker image what is happening is it is running all the commands we listed in the docker file and installing whatever we specify under those commands and i'll give you some time to finish building it there you go we have successfully viewed our docker image i'm going to clear the screen by typing clear to see the image we just built type docker image ls once you type docker image ls press enter and there you go you can see the image we just built here once you have built the docker image the next thing we will do is push the image to our repository in docker hub to store the image there before you can push your image to your docker hub repository first you need to log in and this is the command you will use to log in the command is docker login with the flag user name so let's type that once you type docker login username add another space then the next thing you need to enter is the user name of your docker hub account let me go to my docker hub account to show you in your docker hub account you will see your username up here this is what you need to enter after your command so my username is aos note 77 once you've entered the command press enter and the next thing it is going to ask you for is the password to your docker hub account so whatever password you use to log into your docker hub account that is what you will enter in here please note that when you enter the password it is not going to show it on the screen just type your password once you've typed your password press enter and there you go i have successfully logged into my docker hub account to know that you have successfully logged in you will see login succeeded here once you have successfully logged into your docker hub account the next thing we will do is give the image we created up here a new name so that is what this command means we are going to type the command it is docker tag once you type docker tag the name of the tag of the image we created is here i'm going to type it i called it techmax then add another space and here you will enter your docker id let me show you where this information is so what i always do is i'll go to my docker hub account in my docker hub account i'll select repositories this is the repository i just created that i want to push the image to so the name you see here is what i will enter in my command that is what you are seeing under this command the name of my repository is my docker id forward slash techmax which is aos note 77 forward slash techmax that is what i will enter in here once you've entered it press enter we have successfully used docker tag to give this image a new name now if we type docker image ls again you type docker image ls press enter i'm going to drag this out a little bit more and i'll type the command again so you can see it better there you go you can see the image we just tagged a new name is here now that we have tagged this image a new name we can push this image to our docker hub repository and this is the command you will use to push the image to your docker hub repository the command is docker push then the name of your repository remember the name of my repository that i just showed you in docker hub is my docker id forward slash techmax let me show you again this is the repository i want to push the image to if i click refresh now you can see there isn't any image in this repository and the name of this repository is aos note 77 forward slash tech max that is what i will enter here after you type docker push enter another space and type the name of your repository again this is the command it is docker push and the name of your repository in docker hub once you've typed the command press enter and there you go we are now pushing the image to our repository in docker hub i'll give it some time to finish pushing it we have successfully pushed the image to the docker hub repository now let's go to the docker hub repository to check that the image is there this is the docker hub repository i'll click refresh here and there you go you can see the image we just pushed is there we pushed it a minute ago and the tag is latest let's go back to our ssh session in the ssh session i'm going to clear the screen the last command i have here is the command that we will use to test that the image we just built is working and we can access our website to list the image again we will type docker image ls once you type docker image ls press enter and these are all the images we have built this is the new tag name we gave the image that we pushed to our repository in docker hub the next thing we would do is run this command to test that this image is working and we can access our website the command is docker run we'll type docker run then add a space and we will type minus dp the d stands for detach mode and the p stands for port the port we want to run our container on is port 80 so here we will enter 80 colon 80. once you enter 80 colon 80 enter another space then we will enter the name of the image we want to test the name of the image is aos note 77 forward slash tech marks so whatever the name of the image you push to your docker hub repository is that is what you will enter here once you type the command press enter the container is now running to see if we can access our website let's go to our ec2 instance in the management console in the management console select your docker server then copy the public ipv4 dns of your docker server to copy it click this box then let's open a new web browser and paste it in press enter there you go we can access our website and this website is running on a docker container so far in this lecture we built our docker image and pushed that image to our repository in docker hub we also test the image to make sure that the image is working and we can access the website the next thing we are going to do is stop the container that is running and clean up our environment let's go back to our ssh session to stop the container that is running first we have to get the id of the container and this is the command you can use to get the id of your container that is running type docker ps when you type docker ps and press enter it is going to give you the information of the container that you have running you can see the information of the container here and this is the id of the container what i want you to do is select the id of the container and copy it by pressing ctrl c once you've copied the id of the container this is the command you will use to stop the container you will type docker stop once you type docker stop enter another space and paste the id of the container next to it this is the command it is docker stop and the id of the container you want to stop press enter we have stopped the container if you type docker ps again and press enter you can see we don't have any containers running the next thing we are going to do is terminate our ec2 instance so what i'm going to do is close the ssh session then i'll go back to the management console in the management console select your docker server then select instance date and select terminate instance then click terminate we have successfully terminated the ec2 instance i'll click refresh and the ec2 instance is shutting down this is how you use an ec2 instance to build a docker image and push the image to your docker hub repository if you have any questions on this lecture or there's any parts 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: 5,934
Rating: undefined out of 5
Keywords: how to build docker image and push to docker hub, docker, docker hub, docker push, docker hub tutorial, containers, linux, docker tutorial, docker images, push, dockerhub, docker pull, docker registry, docker build, ubuntu, terminal, create image, registry, docker (software), docker exec, docker rmi, devops, upload docker image, jenkins pipeline, docker image, how to build docker image from dockerfile, docker stop, aos note, what is dockerfile, what is docker, how does docker work
Id: uEfUxFnlxgM
Channel Id: undefined
Length: 25min 42sec (1542 seconds)
Published: Tue Aug 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.