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