Dockerfile >Docker Image > Docker Container | Beginners Hands-On | Step by Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I am raghav and today we are going to do a very interesting Hands-On we are going to create a Docker file we will create an image from the docker file and then finally we are going to run the image and create a Docker container and we will access our application on a web browser so this is what we are going to do we will create a Docker file we will build the docker file that will create a Docker image and then run the docker image to create a Docker container and this is the generally what we do with Docker in our projects we create a Docker file and it is not mandatory always to create a Docker file sometimes you can directly take the images but generally if you are creating your custom application you can take some base images and then add your own instructions in a Docker file which will run and create a Docker image and then finally you will get a Docker container so in this session we are going to do practical Hands-On and we are going to go step by step we will first create a Docker file then we are going to add the instructions in the docker file to create a Docker image then we are going to run the docker file or we generally say build the docker file to create Docker image and then we are going to run the docker image to create a Docker container and then finally we will access our application on our web browser running in a Docker container this is going to be very easy and very interesting and you can follow along with me step by step so let's get started and here step number one is we are going to create a new directory now I am going to use the Linux machine that we created in our last session we created a AWS Linux machine this is the one I created in my last session so this is what I'm going to use and in the last session I have also discussed about an another video where I have shown how to create a free Linux ec2 machine on Amazon and then how to connect to it from a Windows or a Mac operating system so you can do that or if you are already having your system or you already are running Docker on any of your systems whether it is Linux Mac or Windows you can follow along with me so here I am going to go to my link Linux system and connect to it so this is the one let me first connect to this system so this is what I need to connect I am going to just connect it from my terminal and yes I'm connected so if I say uh who am I this is my user and my parent working directory so this is connected Let Me Clear My screen so now here I will first create a new directory and then I will go to my directory so I will say mkdir this is the command to make a new directory and I'm just giving the name my app you can give any name here and then I will say CD to my app directory and you can now see I am inside my new directory here so this was step number one now step number two is in this folder in this directory I am going to create a file called index.html and in that file I am just going to write hello world so that will be a HTML file containing hello world so I can do it multiple ways I can first create a file and then go inside the file and write hello world or I can directly say Eco hello world and then I can just pipe this or put this inside index.html file which will also create the file so I am saying eco and whatever I want let us say hello world and then I will say it should be written inside a file called index Dot HTML and yes this is done if I just check if the file is created I will say LS to list all the content of this directory my app and you can see this is here and if I want to check what is in inside inside index.html so I will say cat index.html now see I am just typing IND and pressing tab on my keyboard which will auto complete the name and if I hit enter you can see this is hello world now uh if you create the file separately I can create any file let's say I say touch and I say demo.html this will create the file if I say LS you can see the demo.html file is created and then if I want to write something inside this I will open it in an editor now on Linux I can say user VI editor I will say VI demo dot HTML and hit enter and you can see this is the file opened in an editor now I will press I on my keyboard which will take me to the insert mode and now I can type something here let's say hello world and when I'm done typing whatever I want to write in this file when I am done doing that I will press Escape key on my keyboard and then press colon and say w queue that is write and quit so first I press the Escape key then I pressed column key then WQ and then enter so that is it has written and written into the file and came out of the file now if I say get demo.html you can see it is written here so this was just a example to show if you want to create the file separately and then right into it you can do like this otherwise you can just use this single statement single command to create the file and also write hello world in this inside the file so this is what we have done step number three is now we are going to create a file called Docker file so I will say touch Docker file and if I now say LS you can see the docker file is created here Let Me Clear My screen and then in the docker file we will add some instructions so here I'm going to say from nginx now nginx is a open source web server that you can use in any of your projects if you want you can check more about nginx so if you just go and search for engine X you can go to its official website and you can see this is a open source web server and here what we are going to do is we are going to use the nginx image as the base image and then inside the nginx we are going to copy our HTML file that is index.html so let us first create a Docker file we have already done that so if I see our Docker file is here I will go inside the docker file so I will say VI and I'm pressing tab on my keyboard so now I'm in the insert mode I will press I to go in the insert mode and I will say here from nginx and then I will say copy index.html so this is the command copy now see my index.html is in the same directory where I have my Docker file so I can directly give the name otherwise you will have to give the location as well and I will say I have to copy it in the nginx folder that is user user share nginx HTML so I will say nginx in the HTML folder it will copy this file and that's it this is what I need so I will come out I will press escape on my keyboard then I will press colon and WQ that means write and quit and press enter if I want to check I will say cat Docker file and I have got this okay so here this is a Docker file and a Docker file is a text file with instructions to build a Docker image you can see this is the instructions to build our Docker image in this we are we are taking a base image from nginx which is already present and then we are doing some more actions here now this is a very very simple Docker file if you want to see some more examples you can check online for examples for Docker file and you will understand how exactly we build images so when we run our Docker file or when we build a Docker file a Docker image is created and then when we run the docker image Docker containers are created and that is what we are going to do here so here in this Docker file as we discussed it uses the official nginx image as base image and then it copies the index.html file to the appropriate location in the image so we are done with these four steps now let us go to the next step that is Step number five here we are going to start Docker and build Docker image from Docker file so the first thing we have to check is if Docker is running or not so if you say Docker info here it says cannot connect to the docker daemin that means Docker may not be running so I will say sudo service Docker start and let's wait now if I again check and say Docker info now you can see Docker is running okay so now I will say Docker build minus t my app Dot so my app is my folder and here we have the docker file inside this folder so when we say Docker build it will check the docker file whatever file you have in your current folder with the name Docker file it will take that file and build the image using the instructions in the docker file and it will tag the image with the name my app so I will say docker build hyphen t and I will say my app and then Dot let me see this is build not build Docker build minus t my app Dot okay I will hit enter and let's see it says First Step was from nginx so it is taking the engine X base image and the Second Step was copying the index.html to the nginx HTML folder and it is all done and you can see this we have got the success message so if I just now check Docker images you can see our my app is here so we have tagged this image with the name my app and these were the images we did in the earlier session we used in the earlier session so you can just ignore this this is what we need now okay it was created 25 seconds ago so we have got the image created now as we have seen this command builds a new Docker image with the tag my app using the docker file in the current directory okay now we will run the docker container from the image and for that we use the command Docker run and I can say Docker run and here we are saying minus P to map the ports so we are seeing 8080 colon 80 I'll tell you what this means and then we are seeing the image name which is my app so here what does this mean is Docker run is the command to create container from the images and then we are mapping the ports so here this command tells Docker to run the my app container from the image my app and map port 8080 on your local machine to Port 80 inside the container so that means once this app once this container is up and running we can access our app using the port 8080 on our local system so let's see I will run this now see I have not used The Hyphen D flag which runs the container in a detached mode so let me show you the difference if I run it now if I run it now you can see the container is created and it is running and we are inside the container so now I cannot do any other action here because my control has gone inside the container and if I don't want to do that I want to run the container in a detached mode I will have to use the hyphen dig option so I am pressing Ctrl C on my keyboard to stop this or exit from the container and I will again run the same command however I will also use the hyphen D option and now if I run it you can see I'm the container is running in a detached mode and if I say Docker PS I can see my container is running here okay so this is running now we can access our app using our browser and going to this location localhost 8080 and we should see Hello World message on our browser now if you are running Docker on your local system you can directly go to the browser and go to this address localhost colon 8080 and you should see Hello World message on the browser however in my case I am using the AWS Linux system okay so let me go back and show you I'm using the AWS Linux system this one and then I am accessing it from my Mac system from the terminal of Mac so here if I have to access the application from my browser on my local system that is Mac system as of now I will have to go and get the public IP V4 address of my Linux machine which is this one so I can copy this and then I will go to the browser and say http colon forward slash forward slash I will give the address ipv4 address and colon 8080 and if I hit enter you can see I am getting hello world here now in case you do not see this they may be you may need some changes in the security rules so just in case you do not see this running you can go back to your AWS system AWS machine here on the AWS console and select the machine go to the security tab here and here click on the security group and here check the inbound rules so you can go and say edit inbound rules and here just make sure that you have the rule which will allow the HTTP or TCP traffic from Port 80. so let me just try I will say add rule and I will say custom TCP or let me say here yeah they should be fine and here I will say 80. and this is what you can do so here um select custom TCP rule Port range you can give 8080 and then you can select this and then save rule so that you can get all the inbound traffic and then after doing this when you again check and run you should see hello world here when you go to your browser to this location so this is how we can create a Docker file and then when we run the docker file we can build a Docker image from the docker file when we run the docker image we create a Docker container and then finally we can access our applications so I hope this was very useful for you if you have any other questions you can let me know in the comment section below if you need any Improvement in the style of teaching or anything with this video you can also suggest in the comment section below I will see you soon thank you for watching and never stop learning
Info
Channel: Automation Step by Step
Views: 114,629
Rating: undefined out of 5
Keywords: Learn Dockerfile creation, Build Docker images for beginners, Create Docker containers step-by-step, Docker basics for beginners, Containerization with Docker, Docker CLI tutorial, Docker orchestration for beginners, Virtualization with Docker, Container management with Docker, Dockerfile best practice
Id: C-bX86AgyiA
Channel Id: undefined
Length: 17min 58sec (1078 seconds)
Published: Thu Mar 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.