Introduction to dockerfile. So a dockerfile is nothing but a text document in which
you write how do you want your container to be customized. So one example like I
just told you guys was when I did it manually. I ran a container, I went inside
it, I installed software, came out, committed the container and then pushed
it onto docker hub. Now all these changes can be automated using a script style
which we call the dockerfile now our dockerfile is very easy to write and
there are basically some nomenclatures or there's some some syntaxes that you
would have to learn but i'm sure they are very easy to learn and once you
learn them it will only be dockerfile that you would be using rather than
doing everything manually right so let's go ahead and see how we can create a
dockerfile. So guys these are some of the
important syntaxes that are relevant to creating a dockerfile. Now the first
line of a dockerfile is always from right so the image on which you want to
make changes for example we made changes on the ubuntu images where we installed
Apache alright so what we did we did docker run - iti - d and then
Ubuntu so in docker file the first base image that you want to work on
you'll have to specify it on the first line using the command from okay so this
this area of your presentation will basically tell you the docker file
content so this is exactly how the content of the dockerfile should look
like so the first command is from so from Ubuntu so the base image that I'm
going to work on is Ubuntu so this would be the first line second command would
be to add all the files so add is basically used to add files inside a
container now for example say I create a HTML file and I want that HTML file to
be added inside the ubuntu container so for
doing that the command add the first argument would be the place where you
where the files are present and the second argument would basically be the
location inside the container where you want all those files to be copied all
right so add space dot the meaning take files
or take all the files from the current directory and put it inside slash where
slash www and slash HTML all right so this is what the add command does. The
third is run so whenever you want to run any command in the container for
example I ran APD cut get update and apt-get install apache2 inside the
container right now if you want to run the same commands from the docker file
you can do that using the command run so over here there are two commands that I
want to run the first is apt-get update and the second is APD get install
apache2 right the - y signifies over here the
option that we get that whether you want to go ahead with installation yes or no
so if you specify - y implicitly in dockerfile it will not ask that option
it would continue the installation without any prompts all right
so run command is basically used to run any command inside the container on the
terminal that you would have run okay then you have a command called
CMD so CMD keyword is basically used to run or any commands that you want to run
and this start of the container right and these commands run only when there
is no argument specified while you're running the container so while you're
running a container you can also specify any command that you want to run so in
case you specify a command while so for running your container the con the
commander's docker and - ie - D and then the image name now after the image name
you can also specify a command that you want to learn inside the container okay
so if you don't specify anything like we did then
in the doctor file this basically this command will be running when we are
starting the container otherwise whatever command we specify in
docker run command that would be running okay. Then the so this was about the CMD
command line. So in this case what we are doing is what we want to do is like
remember when we started the container we always had to go inside the container
and start the Apache service manually that is once the image started or once
the container started go inside the container and type in the command
service apache to start right so that had to be done manually but when you
pass it using the command CMD which is nothing but all the commands run on the
startup time whenever the container will start this command will execute
automatically if you specify it under CMD so CMD Apache CTL - d
foreground would do nothing but the run Apache the moment the container starts
and this is exactly what we want right so this is possible using CMD so again
I'll repeat it CMD is used to run commands at the runtime of a container
and they run only when there is no argument specified in the docker run
command if there is no argument specifies the CMD command will run
otherwise it will be skipped from the docker file all right. The next command
is entry point now entry point is exactly the same as CMD that is
it runs at the starting of the container but the only difference is CMD will not
run if you specify an argument in the docker run but entry point will run
irrespective of the fact whether you have specified an argument or not all
right so CMD and entry point can be used interchangeably but if there are cases
when you are will be running the container with an argument so it's
better to use entry points so that the command does not get skipped. CMD command
will get skipped if you specify an argument after the docker run command
okay so in our case we use the Apache ctl - d in the foreground with the
entry point command alright the next command is env so if there are
any environment variables that you want to set inside the container you can pass
it using the command env space the name of the variable and space the value of
the variable alright so in my case I specified a variable name which has the
value DevOps Intellipaat all right so these are some of the commands that you
can use to create a dockerfile. Now let us create this docker file on our system as
well so what I'm gonna do is I'm gonna go to my putty software and I'm gonna
specify Nano and then docker file so let me just create a directory first so let me
create a directory called dockerfile itself simply a docker file let us go
inside it so see the dockerfile and now whenever you are creating a dockerfile
the name of the file always has to be dockerfile itself right so I'll specify
Nano and then dockerfile. We went inside so the first thing that we want to do is
we want the ubuntu image to be called then we want to update this image so
apt-get update then we want to install Apache inside it apt-get - why so were
that so if it get - bye and stole Apache
- all right sounds good then we are going to add all the files from this
directory to the directory where www.h tml so we're gonna create this file do
not worry we'll just create this file right once we have done that the next
step would be to run Apache in the foreground that has run it so Apache is
CTL
- D ground
so this would run a party automatically
okay and you specify the entry point and say I also want to specify environment
variable say let me create an environment with EBU called name and I
want to specify the volume as in 10 apart
okay sounds good so this is my aqua file and I just save this now and let me
create HTML file as well so it's create and one dot HTML and that is
make it a little simple this would be a HelloWorld HTML file
okay the body
born easy I have an h1 which will say hello from an teleport close the header
close the body
and then close THD LF all right this is what I wanted to and shave this and now
we are done with the dacha file and we have the HTML page in place so the next
step would be to build this dacha file now let's see how we can build it so for
building this dacha file all you have to do is talk build
where do you want to build it I want to build it from the current directory and
the image that it will create I want to name that image as C new underscore
Dhaka file okay so it will be named like this so I'll hit enter and I have forgot
to mention sudo so let me just clear the screen all
right so let me first teach you guys how to run a docker dhaka command without
sudo so for doing that let's type in sudo use a mod - a and G and then talker
and then dollar user enter and now all I have to do is just relogin into your
session
this should work
so for docker PS now without you know you can see the command is running also
so what I want to do is I want to go inside the naka file okay and now I want
to build the stock of as a docker build dot - T new underscore aquifer
so you can see the dockerfile is now being executed and basically this
is
creating the container for me so basically come up with an image which
will have all the changes that we just told to the Container so now if I do a
docker images I can see that there is a new image
which has been created which is new underscore dockerfile so let us run this
new underscore docker file a docker run - I T - D also let us specify the port
number so let us open it at port 84 and then - D new underscore docker file
and then hit enter okay so the container is launched now so if I do and talk PS I
can see that a container has been launched seven seconds ago awesome and
it being opened on port 80 four so let's check if everything is working well for
us so we just have to change this to 84 and you can see Apache is running
awesome Apache is running and now let's check our webpage if it's there in the
country no not so we named it as one dot HTML and yes so this is the HTML that we
created which has been added inside the container so let me show you inside the
container what exactly happened okay so docker exec - 9 T continue named mm bash
and let me just clear the screen okay and let me compare it with my Hakka file
so the first thing that we did was apt-get update apt-get - why install
apache2 this basically installed a patches so this was clear then we added
everything from the current directory to where www.h tml right so if we go inside
where and then www and then HTML and do an LS over there you can see that the
one dot HTML file has been added and at the same time docker file was also added
so inside the doc inside the directory we had these two files right one dot
HTML and aqua file so both of these were added inside the container now to not
have docker file inside your container what you can do is instead of dots
specified dot slash and one dot HTML that would solve this problem it would
only add one dot HTML in the container okay so it added one dot HTML and index
dot HTML is basically the default page of Apache that you see right so we added
one dot HTML so this was added by the Rockefeller the same time what we did
again was we defined apache to run in the
foreground so as you can see but did not invoke the apache service by going
inside the container we just went directly to the port number
that we mapped the container to and the apache was up and running so that is
awesome and the last thing that we did was we defined an environment with evil
so what I can do is I can echo the variable which was dollar name and you
can see this is the value of the variable that are specified in the
docker file I specify the variable names to be in telepods and if I specify echo
dollar name this is the value that I get automatically so this was set by the
docker file in the container image that I just created and now what I can do is
I can just exist this container and if I want you guys to use it all I have to do
is do a docker push to this container on my docker hub and you guys will also be
able to access this particular container alright so for that I'll just have to
change the name do a docker push and you should be able to use it but I'm sure
you would not be needing it because if you pass the same commands that are
written over here you would get the exact same container that I created
you