Docker Init | Streamlining Dockerization for Any Application in Minutes 🚀 | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java Tei are you facing challenges while writing the docker file or writing the docker compost. yl file honestly I have never enjoyed it while working on these files I always thinking whether I was following the best practices and if what I wrote could potentially introduce security vulnerabilities there are multiple factors to consider before defining these files right well you don't have to worry anymore thanks to the team they have introduced an AI based common line interface utility tool called Docker init which automatically generates the required file for developers in an optimized way so in this tutorial we'll demonstrate how to play with Ducker in it and what all best practices it follows to generate the docker image for us okay all right so without any further delay let's get started [Music] so I have created a simple application by defining a endpoint will return the hardcoded list of customer object and I have just defined the URL here okay this is the kind of H application to demonstrate Docker init feature now let me open the pom.xml we are using the latest springboard version 3.2.3 and the jdk version we are using 17 and we have just added the dependency called Spring boot wave and lumo dependency that's it right nothing else so this is the simple application now let's play with the docker init command to generate the file for us so before we implement this make sure you should up and running your Docker desktop on your machine and also don't forget to upgrade your Docker desktop version it should be 4.2 7.2 if you're using older Docker desktop version then this command will not support for you okay so just try to upgrade your Docker desktop then play with this Docker init utility command fine now let me open the terminal in terminal we just need need to hit the command Docker in it just enter it then if you read the instruction it will create these four file okay now if You observe here let's get started it suggested all the language since it is able to detect that this is the Java application the arrow is pointing to the Java so it is using the AI to scan your dependency to identify which kind kind of application you are using okay now I click on the Java then it will asking that what's the relative directory with a leading dot for your app it's SRC just enter what version of java do you want to use it is able to detect that we are using Java 17 so just ignore it next it is asking to define the container Port what port does your server listen on so you can Define 8080 then just click on enter great can you see here it created all the four file your Docker file are ready take a moment to review them and taor them to your application when you are ready start your application by running this particular build before we start application let's validate all the file created by this Docker init command just go to the project can you see here that's fantastic right it it automatically generates all the necessary file for us now you can see here dot Docker ignore compost. yml Docker file and documentation rme file okay all good now let's go to each and every file and try to understand what all things generated by this Docker init command so if you'll go to the dot Docker ignore these are the modules or directories or files we are telling to Docker please exclude them while building the docker image so that you can reduce the image size right so that is the reason it created the docker ignore file now if I'll open the documentation it provided the steps what you can perform the next how can you build your image and on which Port you can able to access your application how to build the docker image how to push it to the docker Hub all the default uh what I can say documentation is already given in this part particular file okay so next let's take a look into this particular Docker file If You observe this particular Docker file there are so many lines right usually when we write the docker file we just Define what is the port what is the jdk image and how to run the jar we are just giving that command and where the jar is located right we we are just defining the four steps to create the docker file that is what the default we are using for testing purpose but if You observe it follow the best practices and also it follow the multi-stage docker image implementation okay so you can read each and every comment here can you see here so if You observe here each stage having their own job to do so you can see the first stage this is what the stage one this stage is called as a dependent image stage okay it will create stage for resolving and downloading the dependency it will take the jdk 17 for us then it will set the working directory to this particular build okay then it will copy the mvin rapper with executable permission that is how it is given the permission and it will copy here then the last step and the important step it download the project dependency using mavin and also it is leveraging Ducker casing mechanism so whatever the dependency it will keep inside / root / M2 so that whenever you'll do the subsequent build it won't R download the dependency rather it will face from the M2 directory that is what the cing implementation of Ducker I mean it's similar to the mavin the way we have the mavin roote M2 directory whenever you will keep building your project if it is downloaded once it will pull from that M2 rather than download it again similar way Docker is also having their own catching mechanism fine this is what the stage one called as a dependent image stage or you can name it shortly d e PS okay this is what the stage name now the second stage is there if You observe here the stage is also properly highlighted in this particular file so this stage two called as a packaging stage so this is the packaging stage stage what it will do from depth as package it will use the base the first stage what we have defined that is depths it will use that from deps as a package then it will set the working directory to build then it will copy all the files present in the do SRC location okay then the next step it will package the application using mavin by skip the test can you see here then at the end it will create a jar called app do jar by considering your artifact and project version this is what the packaging step or stage is done in this particular section now the third stage is extracting stage okay so this is what the third stage it properly highlighted in the file nothing to worry you can read the comment mention here you can understand what each stage is doing so the third stage is extracting stage what it will do it will point to the base image the previously defined package okay and then it will set the working directory to build then it will use spring boots layer tools to extract the application into separate layer you can see here Target extracted the result will be stored inside this target extracted can you see here fine now let's move to the last stage that is called final stage can you see here the stage name is is final and if You observe here you can read the comment from here and this is what the stage definition and all the steps being execute as part of this particular stage so what it does if You observe here it took the base image as a jdk 17 then it also create a non-privileged user called app user can you see here for running the application and this is what the best practice to follow by creating a non-privileged user then next step what it will do it will copy the executable files from extracted stage If You observe in the extracted stage what we have done here we have extracted all the required file from previous T to Target folder like all the dependency spring boot loader snapshot dependency and application all the files it will extract it fine then it will expose the port to 8080 then it will use the spring boot jar launcher to run your images okay this is what all the stages defined in this particular Docker file that is the reason this is called multi-stage Docker file okay and you understand about all the stage dependency stage package stage extract stage and final stage fine now let's take a look into the compost. yml file see here this compass. yl if you do the build it will run your application in the 8080 Port what is defined here then if you have any other services since we don't have the DV connection it is commented if you can add a DV you must need to define the driver class name URL and runtime argument what what if you have some Secrets or if you want to add some uh let's say capka Services then you need to define the bootstrap server and all those stes in this particular compost. yml file if you don't know how to use the docker compost you can check out my video I already uploaded using spring Boot and mongodb using Docker compose okay but this is just a simple Docker compose file so we are good right it created all the four file and we reviewed all of them now cool let's move to the next step that to build your Docker image I mean if you'll run this Docker compos of build it will build your image and it will start your container so let me copy this just add enter around this particular command you can see here it starting executing all the steps can you see here first time it is downloading the dependency you can see here that is what I'm explaining about the casing implementation okay first time it download the dependency then it create a folder called / root. M2 and it store those dependency so that when second time I'll do the docker build it won't download the image again I'll show you in that in a moment just wait it to complete it will take few second yeah build is completed you can see here this is the steps we have defined in the final stage right it is executing that I mean all the stage what we have understand in the docker file If You observe carefully this console log you can understand what steps it is doing and at the end it started my application now let me verify that I'll just trigger this Endo what is the endpoint SL customers can you see see here I got all my three hardcoded customer object that is what I have defined here right basan Santos and shti all the three user I can able to see here I'm able to successfully access my Ducker image I mean the Ducker image running on the container now if you'll check here this is what the image it created Ducker in it demo appen with the server as a suffix okay and the size of image you can see here 29 1.78 MB if you'll create Ducker image with the default configuration if you not use the multi-stage Ducker file then definitely your image size will be more this is the light version of your Ducker image okay lightweight image fine now let's validate the Ducker casing feature so let me go to the idea then what I'll do let me stop these so so that now if I do the rebuild it should not download the dependency again rather it should face from the cash that is what we just want to validate let me clear the console I'll run Docker compose of build let's see great can you see here it started the server it didn't took more than 10 second I believe you can see the time here right 4.9 second so it took total time 4.9 second to build and run your Docker image and you can see here all the info is being cast can you see here okay and this is the image if I'll retrigger it I'm able to see the response great so you can use this Docker init command to generate all the required file to perform this container to run on your application to The Container but just like any other AI based tool this one is not perfect either do not blindly trust the configuration it generates Docker ignore compos IML Docker file and this documentation because this is just AI based tool who generate all these things for us right so I recommend you have to another look at the config before you push it forward so just go through each and every file check whether all the things are correct or not or if you want to customize something or validate there is no security brid all the things you need to consider before you push it Forward I just give an demo of using this Docker init utility to understand or to automate all the process to generate the files and run your Docker image successfully okay so do let me know in the comment section if you guys have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 5,109
Rating: undefined out of 5
Keywords: javatechie, docker, docker init, javatechie docker
Id: lUQ4y74LWgw
Channel Id: undefined
Length: 16min 4sec (964 seconds)
Published: Fri Feb 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.