Hey everyone, welcome back to the
channel. In today's video, we will learn
how to build Docker images using Docker
file from the scratch. We will cover
what a Docker file is, common keywords
used in Docker files, layers in Docker
images, multi-stage builds, and finally
build two Docker images. One for Python
app, another one for Java app. I already
have some sample Python code and app
code that I will be using during the
building of these images. A Docker file
is simply a text file with instructions
that tell Joker how to build an image.
It automates everything from installing
dependencies, copying code, and to
setting up the environment. Some
important keywords on the Docker file
are from it sets the base image. run
executes the command during the build.
Copy or add copies the files into the
container. Workd diir it sets the
working directory. cmd default command
executed when a container start. Entry
point it sets the entry point for the
container. Expose it specifies the app
port. Volume mount storage for the
persistence. Each instruction in a
docker file creates a new layer. For
example, from creates the base OS layer.
Run apt install creates a dependency
layer. Copy app. py creates the
application code layer. The best part,
docker caches these layers and make the
builds faster. Multi-stage builds help
optimize image size. Instead of CPing
extra build tools, we split Docker file
into a build stage and a runtime stage.
For example, the first stage builds the
app with Maven or cred. Second stage
copies only the final compiled artifact
into the small image. All right, let's
move to the demo part where I will build
these Docker images. If I run ls-l,
I have two folders. One is for Python
app, another one is for Java app. Let's
go to the Python app folder first.
Inside this Python app folder, I have a
Python script and requirement.txt.
I will be using these files while
building the docker image for this
Python app. In order to build the docker
image, first we need to create a file.
That file name is docker file and d is
caps here. So let's create a file using
vi editor.
Enter into the insert merge and type the
first keyword here from next specify the
base image. In our case it would be
python.
Hit enter. Type work diir.
It will set the working directory. In
our case, it would be / app. Hit enter.
Next, copy the requirement.txt file
under this app folder. For that, use
copy. Specify the file that you want to
copy. In our case is requirements
dot txt.
Next, install the dependencies using
this requirement.xt file via pip install
command. For that, first define the word
run and then specify the command pip
install - r and then requirement.xt.
Hit enter. Now copy the app code using
copy command.
And in the last we'll define a cmd which
will start our application. So this will
be executed whenever a container is
started using this docker image. Under
cmd we need to define the exact command
that we want to execute when we start
this container using this docker image.
Type here Python
and then your app name.
Save and close the file.
In order to build this docker image, you
need to run the command docker
space build space-en t is for tag. Tag
means just labeling the docker image. In
our case, I will set it as python.
hyphen app
and dot dot means it will look for this
docker file inside the current working
directory. If you hit enter, it will
start building the docker images. So
this is completed. Now you can verify
the docker image.
Run the command docker images.
You will see that
a Python hyphen app docker image of size
132 MB is created. Let's create a
multi-stage Docker file for our Python
app. Multi-stage builds help create much
smaller images by separating the build
environment from the final runtime
image. Let's edit the Docker file.
Remove these lines.
For the interest of time, I have already
prepared the set of sections for this
multi-stage docker file.
All right, if you can see, I'm defining
the first stage as build stage. In the
build stage, I am pulling this image as
a builder. I'm setting the work
directory as app, installing the
dependencies using the required .txt
file and running the pip install. After
that I'm copying the app code to this
app folder
and then I'm starting my second stage
that is runtime stage. For that also I'm
using the same Python image setting the
work directory as again as/ app. And now
I'm copying only the installed packages
from the builder and updating the
environment or updating the path
environment. Once it is done then I'm
copying the app code and then exposing
the app on the port 5000 defining the
volume for our app/ app/ data and then
running or starting the app using the
python command. So this cmd whenever
this container got started using this
image it will run this command python
app. py which ultimately starts our
application. Now save and close the file
and build the image. For that rerun the
command docker build space- t and the
tag name. This time I will specify
space dot hit enter.
Docker file creation is also completed
using this multi-stage. Now verify the
docker
image size.
All right. In order to test this docker
image, we will start a container. Let me
clear the screen first.
Now verify the container. Run docker ps.
It has started a container using the
image that we have just built using
multi-stage. All right, it has started
this command python space app. py. It
means our application also got started.
Now validate this. Open the browser or
you can also test it from the command
line. You can simply run the curl
command here
enter. You will see the output something
like this. Hello from Python dockerized
app. It means our application is started
successfully in the container using this
docker image. All right, let's move to
our second demo where we will be
building a Docker image for Java app.
Let's go to our Java app folder.
You do l you will see our pom.xml and
our Java app code under the src folder.
Let's create a docker file here.
If you see I'm defining two stage. So it
means I'm using multi-stage docker file.
Here in stage one I'm saying it's a
build stage. So for this build stage I'm
using this maven based image as a
builder. I'm setting this work directory
as/shop. I'm copying the pom.xml file
under this app folder and then copying
the entire Java code from this src to
this src folder and then building the
package using mvn clean package. The
next stage where we are using a slim
base image and we call it as slim
runtime. Here I'm specifying that this
is the image I would be using it as a
base image. Work directory is again /
app. I'm copying the artifacts or the
jar file that was built in this build
phase directly from the builders to this
image and then exposing this application
on the port 8080. And in order to start
this application, I'm using entry point
here. And I'm executing the command java
space-en jar and then app dot jar. On
this is executed, it will start our
application. All right. Save and close
this file.
Now run the docker build command in
order to build this docker image.
The tag name is Java app and then doge.
Hit enter. Sorry, typo. Let's correct
it. It's build.
Okay, the output confirms that docker
image has been built successfully. Let's
verify it and the command
docker images. Our Java based Docker
image is created. All right, let's start
a container using this docker image and
let's validate whether our application
is accessible or not. We clear the
screen first
and the command docker run-d
[Music]
name is
Java - app. Hit enter. run docker ps
you will see one container got started
with the name this one it is using this
image that we have just built and it is
executing the command java space-en jar
space app dot jar so it will start our
application it's the same command that
we have defined in the entry point uh
keyword in the docker file let's try to
access this application run the command
curl
All right, we we are getting the
response from application. This confirms
that our application is accessible using
this container which is started using
this docker image. That's all from this
video tutorial. I hope you have found it
useful and informative. Do not forget to
like, share and subscribe for more
DevOps tutorials. Thank you. Bye. Have a
nice day. See you in the next one.