Taking this step let's look at how you can make your Dockery majors more efficient.
The process of creating Dockery made us much more efficient.
We use this Docker file to create the Docker image for I know Jay's project.
Let's go to the terminal.
Let's do a clear present working directory.
I would want to go to No.
Hello World notice let's do uh darker images.
The Hallowell no JS images here zero point zero point do not release and it was created 27 minutes ago.
Cool.
So we created it from this Pacific Docker file.
So let's try and create it again.
DR BILL hyphen let's try and build the same image again.
We have not made any coating genius as such.
So I'll go ahead and say Bill.
GOP zero point.
Do not release and send DOD the current directory as the bill context so you can see that the bill immediately executes so within a few seconds the bill completes execution.
Why.
Because it is using everything from the cache you can see using cash using cash using cash using cash using cash or here when nothing is changing in the previous layers.
Everything will be picked up from the cash.
Let's now make a small change.
Let us go to the next year and make it we three are making it.
Let's say we two point one.
So we do point one let's build this I made again enough you'd see that they would decrease slash app is using the cash however when we copy the apple it seems that there is a difference in the app folder and therefore from then on all the other steps will not be picked up from the cash.
So I a step to using it from the cash here.
There is a difference.
So for the rest of the stops irrespective of whether there is a change or not all of them will be executed.
Again so you can see that NPM installed ran for 20 30 seconds and then and the rest of the steps will not be from the cash so you can see that using cash is not present anywhere else.
Now if you are familiar with any application building process right to this application need a few dependencies.
All these dependencies are present in a file called package da Jason.
So for this particular thing the dependencies express the dependency over here is a step back.
This content does not change very often.
What changes is your code.
So.
Here this week two point one might change to something.
Actually you'll have real code in here so this code keeps changing with the package.
Jason where you are specifically dependencies.
This would hardly ever change.
Right.
So in that kind of a case how would it be if it built the dependencies up as a separate layer.
Then what would happen is there is a high chance that the dependency layer also gets cached and if we have the code layer on top of it then only the code layer keeps changing.
We don't need to change the dependency layer often.
So how do we create the dependencies as a separate layer.
What we can do is inside the docker file what I can do is a copy package that Jason.
The section is written down here so I'll copy package that Jason in a separate step.
So instead of all here copying dot to slash up the copying the entire directory what I lose I'll just copy the package though.
Jason first and I'll expose five thousand and only at the end as line six out copy the code which might change the package notches.
Jason does not really change a lot and that is one of the most important files for npm install.
Actually that is the most important file for npm install.
So we are copying the package to Jason running npm install ex-post vitals and copy dot slash app node index charges.
Now let's not really make any changes and let's quickly build a image.
Let's call this zero point zero point 3.0 release.
You'd see that a new image will be built because we have changed the lease a lot.
So let's go out and build that.
So you would see that the second layer is from cache but from third one on everything is going to change and npm install would run again.
This would take about 20 seconds because the first time we are running it.
Okay.
It took about 20 30 seconds to run the step for step five six and seven should be quickly done right.
We have now 0.01 3 release.
What I'll do is now I'll go to index charges and make a change.
So I'll make this be three.
So we are making a change in the source code.
Let's see how quickly we'll be able to build it on the same thing again.
You can see that it's quick.
Right.
Even though we made a code change it was like this it's very very fast.
Let's make the coaches three point one and let's push it we are not illegal should it be a building a major but you can see that the image was built very very quickly.
And just to be sure let's run it right to Docker run high fan D.
If and B I'll run it on five thousand one five thousand.
And this so we are running it on local host five thousand one so let's see what's on five thousand one could we have hello world javascript we do point one and that's the latest change that we made you can see that by changing the way the builder Docker fell we made our whole process of building the image very very efficient.
Now we can actually do something similar on the Python thing as well.
So what we can do is actually copy requirements that text individually.
So let's do a copy of requirements the text and let's actually move the copy of the actual files a little later copied or slash app and making it a little later and you would see that this would improve the time taken to build your python image as well let's quickly see that in action hello world Python this so I'll replace this with Python so lockable 15 20 min Halliwell Python 0 0 3 release so this should take a little while to run the pip install will take a few seconds it's not long so you can see that the people started running it would create the image very very soon yeah.
So it's tagged with 0.01 3.0 release.
I'll go and change the launcher P Way To V three so we 3 is ready now.
What I'll do is I'll run it again and you'll see that this time the bill would run successfully and you'd see that if you've done this you would see the latest code out there.
Instead we understood the fact that leaves are cached.
One of the important things that you'd need to remember if lives are cached it's not only faster to build the image but also it will be faster to push the image to Docker Hub and also to pull the image onto wherever you want to deployed.
So making sure that your layers are cached as much as possible is very very important.
I hope you are having a fun time and I'll see you in the next step.
We'll talk about a few more important Docker file related concepts.
Add Comment