Docker Layers and Multistage Builds | Docker Optimization | Reduce Layer Size

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and girls raj here back with another docker video in this short video we are going to learn about docker layers and multi-stage builds so let's just start with docker file so what is a docker file docker file is a document that contains all the commands a user could call on the command line to assemble an image for your application so in this slide on the left i have a sample a python program along with the requirements and on the right we have the docker file to dockerize the container i'm not going to go deep on what is docker file and how to dockerize an application i have a separate video where i started with application running on your local desktop then we dockerize it in your local desktop test it out and then migrate to cloud and then deploy cloud concepts so i'm going to link that video up top check it out if you want so if we dive deep into this docker file you can see that each line is a command so when these commands are executed it creates a layer or intermediate images for example when the from python colon 3.8 is run it creates a layer for this command similarly each commands keep on adding more layers to the container image and your final docker container image is the collection of these layers and when you run your container image to run as a container on top of these container image layers a thin writable container layer is created so each of the layers adds to the size of the container image so let's take a look with our existing program okay i'm going to build the docker file that we just explained so if i run docker images you can see currently i have no images so i'm going to run docker build minus t name of the image as sample python image to dockerize this application and then it's gonna go do all the steps and finally it says exporting layers so let's see what is the size of this container image so if i run docker images you can see it generated this container image but the size is 893 megabyte and this is a pretty straightforward docker file right it's just installing this requirements file which has only one external library and then it's copying this python file and then starting the server.python program so now let's look at the layers to look at the layers you run docker history and the name of the container image so you can see the first step is getting the base container which is from python colon 3.8 so this takes a lot of space and then the word directory slash code doesn't take any space and then run this command takes 9.74 megabyte and finally the command takes no space so for this case most of the space is taken by the base image which is a fetching python 3.8 container image so going back to our container image layers in your production workload depending on the commands you are running on the docker file each layer will have some size and it is very critical that you optimize the container image size because when your containers need to scale or if a running container dies and it needs to be replaced with another running container one of the big factor of how fast the container orchestrator can create another running container from the image is the size of the container image so how do you optimize container image size so number one you can reduce the size of each layer and number two reduce the number of layers so in our case the base image is the largest layer so the base image that we are fetching is the python 3.8 with bunch of libraries installed that we don't need so it is highly recommended to fetch a lightweight image so one of the super popular approach is for example if you are using golang docker file instead of fetching let's say the golang version 1.7.3 just fetch the alpine latest container image it will have the bare minimum libraries installed that can run your golan code now going forward and this is the multi-stage part how do you reduce the number of layers so if you think about it at the end of the day all the docker file is doing is it is taking your code installing the dependencies and compiling it and then creating a executable and this is of course applicable for compile languages and once you have this executable it can run anywhere right because once you have this exe file or a jar file or the golang final package file you do not need to have the code and dependencies anymore that final artifact is able to run by itself so your final docker container image can just have this final executable artifact and you should be fine so multi-stage build achieves that so with multi-stage build you can have multiple stages in the same docker file so let's say stage one you have the large base image and then you install the code and dependencies and then you compile your code to an executable artifact and on the second stage you take the small base image and just copy the executable into that small base image and you create the final docker container for your application from this next stage and all these multiple stages can be done in the same docker file so let's take a look at an example so if we look at a multi-stage docker file so on stage one it is grabbing the golang version 1.7.3 and this as builder is the name of the stage so the name of the stage is builder and the final line of this stage it is compiling the code and creating the final artifact now in the same docker file you can see we are grabbing the lightweight image and anytime there is a from statement in the docker file that means a new stage starts and in this stage i am simply copying the compiled code from builder stage you can see i am referring the previous stage with dash dash from equals to builder and the docker container image will only be built from the last stage so in this case if you created your docker container from just stage 1 the size will be much larger but on the second stage since we are using the lightweight image and only copying the executable artifact the size will be much less so this is how you reduce the number of layers as well as the size of layers using multi-stage builds if you like this video smash that like button click subscribe all the likes and subscribe really help this channel grow we are almost at 10 000 subscribers can't wait till we cross that milestone all right with that guys and girls i end this video i'll see you guys and girls in the next video bye
Info
Channel: Cloud With Raj
Views: 2,461
Rating: undefined out of 5
Keywords:
Id: 1tHCVIO8Q04
Channel Id: undefined
Length: 8min 6sec (486 seconds)
Published: Sun Mar 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.