How to Build a Container Image Using BuildKit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how to build a container image using buildkit [Music] when you first start out building container images more than likely you're just using docker build however did you know that there is another tool available that will help us build container images that tool is buildkit buildkit was integrated into docker in version 18.06 and specifically it focuses on build performance storage management and a few other options here's today's starting point i have a jenkins lts controller version 2.319.1 attached to this controller i have a linux based agent that has docker installed on it for this video i have a sample repository the link to that repository is down in the description and let's go ahead and take a look at that repository what i have is two jeekens files and two docker files and we'll look and see how these docker files are being used within these jenkins files now let's go and take a look at our docker file first and what we have here at line one is syntax docker slash docker file one three one now this is what is called a docker front-end and buildkit uses this in helping to build the images if you want to see more information about docker slash docker file you can just go out to docker hub and search for docker slash dockerfile and what you can see here is that the latest releases are 131 and then alias to 1 3 1 and then latest but nobody likes using latest so i'm going to specify 131 in my syntax right here and if we take a look at the rest of this docker file you can see that we're using a multi-stage build now they're all trivial examples i'm just creating a file within the temp directory in each of these stages but then i put in a sleep and this is where we're going to see the major differences between just using docker build and docker build where build kit is enabled now to start out let's look and see what our normal docker build is going to do if we take a look at our jenkins file docker which we'll just be using docker build what we're going to have is a system prune to clean everything out on our agent and then i'm going to put time in front of docker build so i know how long this one step takes and what we're saying is docker build we're giving it a tag and then we're specifying first then we're going to list out docker images and then again we're going to do a build but this time we're going to be using docker file second and again show docker images so what is this docker file second we'll go back up here and take a look at it it's exactly the same as dockerfile first except we've added in a fourth stage so that's the only thing so once it actually builds this final image we're just doing another copy from temp to temp so dockerfile first is the first three stages dockerfile second just adds in this stage four so let's go ahead and go on over to our controller and create a job to run the docker before we go over there let me go ahead and grab my url so let's go ahead and click on new item we're going to give it a name of docker select pipeline click ok change this from sem rcm is git repository url the branch is main and our script path is dash docker we'll click on save and let's go ahead and click on build now and watch what happens as this job runs so we see our checkout we do our clean which it was already empty we build our first stage and that takes just a few seconds but then it sleeps for six seconds then we move on into our second stage which then is into an eight-second sleep finally we are into our third stage and now we're into a 10 second sleep do you see a pattern here each time we run our stages we sleep for this amount of time and it's all running sequentially now what we can see once it finished is that the real time was about 30 seconds and if you think about it 6 plus 8 plus 10 is 24 plus the overhead of the job starting and the other things that were happening about 30 seconds what we would expect but take a look at the output of docker images remember the image that we created was this image right here and that's fine but also notice all of these other intermediate images that exist from using docker build we also have our base alpine image pulled in here as well now the second part of this job runs the exact same process except that it's using docker file dash second which added in that fourth stage so if we take a look at stage one it's using cash stage two using cash stage three also using cache but then once we get to stage four it does the run it sleeps for another 10 seconds creates the final image as zero zero two now the real time in this was about 11 seconds the 10 second sleep in that stage 4 plus the overhead of the rest of the job ran for about a second and a half but again taking a look at our docker image output we see our first image tagged at zero zero one our second image tagged at zero zero two but you'll also notice that yet again we have another intermediate layer that's left laying around now let's go back over to our repository and take a look at the second jenkins file when we take a look at jenkinsfile dash build kit everything is exactly the same except for a couple of very small items in order to enable build kit we need to set an environment variable and we're setting it at the global level docker underscore build kit equals one that's it that's all you need to set in order to enable build kit within docker we do our docker system prune we're running time i'm taking advantage of a dash dash progress equals plane because otherwise it shows it in a different format by doing it in plane that's plain text that works out much better in the console log but otherwise everything else in the docker build step is exactly the same we do docker images again we add in progress plane for the second run for second but otherwise exactly the same so three material changes to this jenkins file the two progress planes on the build steps and the most important one is the docker underscore build kit as a global environment variable let's go set up another job to run this jenkins file run back over to our controller new item we'll call this build kit pipeline click ok if we go ahead and take a look at scm we will go ahead and change scm to get put in our url change this to main and the script path to build kit jenkins file dash build kit to be complete let's click on save and click on build now now as we run this think back to the docker job that we just ran the very first pass-through with the dockerfile dash first took about 30 seconds watch how long this one takes one of the things with build kit is it's built to increase build performance so what it's doing is that it is going through and parsing that docker file and determining what can be run in parallel so what's happening here is it's going through and running the job breaking out each of those stages within the docker file itself not with jenkins but within the docker file itself and parallelizing the builds of those first three stages so when we take a look at in fact we can see it here here's stage 3 103. sort of looks strange but then here's stage one two of three and stage two two of three which sort of strange but you know here's all of our items and then here's stage one three of three so all of these things are going through in parallel within the docker daemon using build kit now check this out look at our total run time here when we ran it with just docker build without build kit it took about 30 seconds here it took about 15 seconds this really speeds up the build now here's the other thing that's really interesting if we take a look at the output of docker images think back to the previous one we saw alpine and a bunch of different intermediate images until we finally see our zero zero one with build kit we only have the final image all the other intermediates even the base image gone doesn't mean it wasn't there but once this job completes the only image that is here is the image that we built now let's continue on now we do our docker build using dockerfile second again same thing we're running in parallel in fact we added in that stage four now with stage four again about a 10 second build but that was the only major difference the other three stages were already cached so we were able to take advantage of those we're back down to 11 seconds which in reality is about the same as what we saw in the very first job the real impact in running build kit for us this time was in that very first pass with the docker job it took about 30 seconds with build kit it took about 15 seconds and finally again what we see are the images that we want the final images so in this case it's the myapp001 and myapp002 and none of the other intermediate images are there now there's one thing you might not even know you actually don't even need docker to run build kit as i said earlier buildkit has been integrated into docker since 18.06 but if you are running linux there is a standalone version of build kit that you can run eliminating the need to even install docker if you have any questions or comments you can reach out to us on twitter at cloudbees if this video was helpful to you give us a thumbs up and if you haven't subscribed to cloudbees tv yet why not take a moment click on that subscribe button and then ring that bell and you'll be notified anytime there's new content available on cloudbees tv thanks for watching and we will see you in the next video you
Info
Channel: CloudBeesTV
Views: 6,849
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, docker buildkit, docker_buildkit, docker build kit, buildkit docker, buildkit_inline_cache, docker buildkit cache, build kit docker
Id: 9CRDMs5D3kM
Channel Id: undefined
Length: 11min 19sec (679 seconds)
Published: Thu Jan 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.