 
                        Building Docker Images using Jenkins running as a Docker container | Docker within Docker | DevOps
Building Docker Images using Jenkins running as a Docker container | Docker within Docker | DevOps
This tutorial demonstrates how to run Jenkins as Docker container and build Docker images of applications using Jenkins. Building Docker images using Jenkins. Running Jenkins as Docker container.  #SpringBoot #Jenkins #Docker #DockerJenkins #JenkinsOnDocker
If you liked this video please do subscribe and press the bell icon so that you are notified when I publish any new video. 
Please feel free to post any questions you might have related to Docker, Jenkins or Spring boot and I would be more than happy to help.
Flutter with Spring Boot playlist:    • Connecting Flutter with Spring Boot R…  
Content
0 -> when it comes to ci/cd tools jenkins is one of 
the most popular tools available in the market  
6.96 -> jenkins can be run on dockker and 
running jenkins on docker is a cakewalk  
11.76 -> all you have to do is run the docker run command 
and then you're good to go once the application  
19.04 -> is up you can then create jobs pipelines to build 
tests deploy your application and do all kinds of  
25.84 -> activities building docker image of an 
application from jenkins running a as a  
32.8 -> docker container is a little tricky hi my name 
is pratap and in today's video we will see  
39.36 -> how to build an image of an application 
with jenkins running in a docker container  
57.12 -> i'm running docker on windows and uh this 
is windows 10 i'm running docker desktop  
64.8 -> on windows let us spin up a jenkins instance as 
a docker container and for that the command is  
72.24 -> under normal docker run command here i 
have named the container as jenkins-lts  
79.76 -> and i'm running this on port uh at port 
8080 this is the volume that i have mounted  
87.6 -> this is the volume on my local system and i have 
mounted that to the container and let's hit enter
98.96 -> and the container is started let's give it a 
minute or two until um the instances instance is  
106.48 -> up and running then we'll go ahead 
and log in to jenkins i have already
116.16 -> created a jenkins account and i've already 
stored it on my local i will go ahead and  
123.84 -> log in with the credentials that 
i have already created okay now i
141.76 -> let's try to access jenkin um on my local at port 
8080 so i have typed in localhost 8080 i provided  
151.92 -> my user id and password and clicking on sign 
in okay i have already created a simple uh  
161.36 -> jenkins job let's see the details 
of the job if i click on configure  
168 -> it will take me to the configuration page 
where we will see what i have written
177.36 -> okay this is the simple job i've done 
given a small description and if i go down  
187.68 -> to the button i'm only running a single 
command this is the goal i'm trying to  
194.96 -> build a simple spring boot application uh 
spring spring boot 2.3 plus has this ability  
205.44 -> that you can you do not have to explicitly 
write a docker file you can simply type in  
211.76 -> this command spring boot build image and 
that's that is sufficient to build a docker  
218.88 -> image that's what i i have done let us save 
it and build the image if i click on build now
229.76 -> here's the build let's click on the build history 
and if i click on control output that will show  
239.68 -> us the output let me zoom in a bit so that you can 
see it easily okay i think this should suffice the  
255.76 -> building process has started and it has compiled 
tested and now then jenkins is building the image
266.48 -> and as you can see build failed with error failed 
to execute goal and this is trying to connect to  
276.08 -> docker daemon that is running on my host machine 
so this is the error you can see i'm getting  
284.64 -> in the next step we'll see how to 
mitigate this error and we'll see  
290.8 -> how to fix it when using docker desktop a vm 
running the docker demand is installed command  
298.64 -> such as docker run docker ps etc are passed from 
the cli to the docker daemon running in the vm  
310.08 -> so in this figure you can see that this is the 
docker demon and inside this docker there are  
315.6 -> multiple containers running and we have a cli 
from cli we can now pass command such as docker
328.56 -> can process those commands and respond 
accordingly if i go to the command prompt and run  
336.48 -> other version command let me clear out the 
screen first and then let me run the command
360.16 -> docker version command shows that we have on the 
docker engine that is comprised comprises of a  
368.96 -> client and server okay server is nothing but 
the daemon and client is nothing but the cli
378 -> there is a clear distinction 
between the client and the server  
381.84 -> okay um so if we go back to the slide the that 
is the same thing happening here we are sending  
389.76 -> commands from docker cli to docker daemon 
and docker daemon is responding accordingly  
396.48 -> so in our case we will have to devise a way 
in such that the container in which jenkins  
406.64 -> is running should be able to communicate with the 
docker daemon that is running on the host machine  
413.76 -> okay so we have a couple of tasks to do to 
achieve that communication right so first  
423.36 -> thing is we will have to install at the docker cli 
in the jenkins container second thing is we will  
431.92 -> have to communicate with the docker 
daemon running on the host machine  
436.8 -> the final part will be to establish a way and 
also grant appropriate permission to the container  
445.28 -> so that it can talk to the daemon so for that we 
will have to follow a couple of steps first thing  
453.36 -> we will have to create a docker file as as um 
depicted here um this is that we will build the  
463.04 -> another image another custom image from jenkins 
lts and we will use a root user and we will use  
474.4 -> this command to install docker cli with user 
jenkins the second step will be we will have to  
482.08 -> run the docker build command to build uh this 
docker file i mean that will build the image  
490.72 -> finally we will have to once once the image 
is ready we will have to run the image meaning  
498.4 -> we will have to spin up the container by running 
docker run command with following options  
506.4 -> we will have to provide um var run 
docker shock meaning we will have to  
514.72 -> bind the volume so let's talk about 
a docker sock file it's a unix file  
522.32 -> docker daemon actually listens to this file 
and responds to all the commands that are  
528.08 -> sent through cli this is the way through which 
our jenkins container will communicate with the  
536.24 -> docker daemon that is running on our um host 
okay i have already built um i mean i have  
547.68 -> already prepared this a docker file let's go ahead 
and run the build command and see how it goes
557.76 -> moving on to the
561.2 -> command prompt
578.72 -> this is where i have the docker file 
present and let me run the command
582.96 -> here okay the image is built let's see if 
the image is present in the in our docker  
605.36 -> okay so this is jenkins in docker uh 
was the image name we provided and the  
612.96 -> images already present here next 
thing is let's go ahead and run this  
620.32 -> image and see what happens so this is the 
command let me copy this command from here
638.16 -> i'm going to spin up the container 
and as you can see i am mounting  
645.12 -> docker sock using volume command and this 
is the volume where i have all the um  
652.64 -> docker related jenkins related information 
that is residing on my local machine  
659.12 -> okay if i go ahead and run it okay let us give 
it a different name looks like there is some  
666.96 -> other container running with this name one thing 
to notice here is 0 option in the command that  
677.52 -> is used to add the user with which the image 
was created to the root group of the container  
687.04 -> that is done in order to get access to the docker 
daemon that is running on the host system and  
698.32 -> that contains docker dot sock file and that is 
not uh usually that is not accessible to all  
707.12 -> the users only root users can access that file so 
we are adding group 0 so that the user with which  
718.08 -> the docker uh image of jenkins was created 
that can communicate with the daemon  
725.2 -> the container is running let's give it a 
minute or two to then we will go ahead and  
733.12 -> run our build job the container is running 
on localhost 8080 let me try go ahead and  
744.08 -> that url right the docker container 
is now up let me log in with my
752.48 -> credentials so i provided my 
credentials and i'm logging into  
762.96 -> the container moment of truth let's go 
ahead and run this job and see what happens
781.12 -> let's go to the control output of this job and say
792.16 -> jenkins is building the image
796.88 -> bingo there you go there you go the build 
is successful and docker image is created  
805.92 -> this is the image that is created just now  
809.44 -> to prove my point let's go to the command 
prompt and see if the image is really created
817.28 -> let me exit from here and if i clear the screen
826.56 -> type in the command docker image images
832.72 -> yes here is the image that was created just now
849.2 -> congratulations you are ready to rock and roll 
with this image you can push this image to
858.72 -> docker hub and do all kind of activities that 
you want to do thank you for watching this video  
866.24 -> and please hit the like button 
if you like the video and  
873.28 -> also if you have any comments please do not 
hesitate to comment on my video thank you so much
                    Source: https://www.youtube.com/watch?v=h15Mh9FDMLo