In this section there's one last quick thing that I want to mention about the docker build process.
This is an important thing to understand because it's what gives Docker so much performance whenever
creating a new image.
All right so I'm looking at my Docker file right here.
We have our three instructions and as you very well know by this point out of each one of these instructions
we essentially get a new image.
So from Alpine we get the Alpine image from the run instruction.
We get a temporary image that gets fed into the CMT instruction which gives us another temporary image.
So at every structured instruction along the way we have a file system snapshot and a start up command
for that image.
Now I want to show you something a little bit interesting I'm going to go back over to my doctor file
and on the run instruction I'm going to add a new one right underneath it.
I'm going to say run a P.K. add dash dash update GCSE.
So we're just installing a very random second dependency here.
There's nothing important that you need to know about GCSE.
We're just installing a second dependency.
So I want you to think about what effect this is going to have on our overall flow.
Essentially what it's going to do is add in a new instruction right here.
That's going to look a little something like this.
We're going to see a new one.
Let's see how quickly I can do this.
Edit we're going to get our new instruction right there.
It's going to be very similar to the image from the previous step and it's going to have a new little
program or folder or something inside of it of GCSE.
So essentially it is identical to the previous image but it has this extra little program that has been
added in.
Let's now flip over to our terminal.
I want to rebuild our image and you're going to notice something kind of interesting.
So back over at my terminal I'm still inside of my retina image directory and I'm going to build my
image a second time by running docker build dot all right.
We're going to very quickly see some installation steps right here as it adds in that additional package.
But I want to scroll up to the second instruction and on point out something interesting up there so
we still see it step one of four right here where we do it from Alpine.
You'll notice that we do not see any of that like fetching image stuff that we saw the first time because
we have already fetched the Alpine image.
We've already downloaded it from the Docker Hub and so we don't have to go and download it a second
time.
Now the interesting thing I want to show you and this is where Docker gets so much of its speed and
performance from when building an image.
You'll notice that during step number two right here we do not see any of that stuff around running
in blah blah blah.
We don't see any fetch.
We don't see any installation of dependencies.
Instead we see a single message that says using cash.
So what this means is that Docker has realized that from the previous step two step number two.
Nothing has changed from the last time that we ran Docker built.
In other words it knows without a doubt that it's going to get the same image from the previous step
because it's the exact same instruction that it was before.
And then four step number two Docker knows that it has already executed the command API Kate.
Add update Reddit it's already generated an image out of that step this image right here and this image
has been cached and stored on your local machine.
So rather than go into the process of creating another container out of the Alpine image and running
a P.K. ad update read us inside that container a second time it says you know what I've already done
this work I'm just going to use the image that I generated during the previous step again.
But then after that Docker correctly sees that there is a new command in play and this is not ad update
read as it's at update JCC.
It sees that there's a new command right here a new instruction in play and so from this point on it
decides well you know what.
Something has changed during the build process.
We probably can't use our cash anymore.
And from here on out we have to go through that process of generating a container and running a command
inside the container and taking the snapshot and going through all that stuff again.
Now the interesting thing about this is that it has now executed run KPK at GCSE and it has generated
an image out of that instruction right there.
So if we now run docker build dot a third time without making any changes to our Docker file Docker
is going to correctly see we have made no changes to the docker file.
None of these steps have changed whatsoever.
And so it's going to use the cached or saved versions of each of these images in building this new one.
Let's do that right now.
You're going to see it in action.
So are going to Docker build dot a third time and you'll notice that build process one extremely quickly.
So Docker correctly realized that it's already download Alpine.
It's realized that it already ran the run command on top of Alpine.
So it's using the cash version that image.
It knows it's already done the JCC so it's using the cash version of that and it knows that it's already
executed command read a server on top of the output of the GC stuff.
And so it's using the cache there again.
So the lesson to learn here is that anytime that we make a change to our Docker file we're going to
have to only rerun the series of steps from the changed line on down as a quick example of that.
Let's try taking this GCSE line right here and I'm going to cut it and put it right above read this.
So even though we're still taking the Alpine image and adding JCC to it and adding reds to it the series
of steps have changed.
And so Docker is going to say OK well this time we're taking Alpine and first adding in GCSE.
So it will use the cash version of Alpine but it's not going to have a cached version of the GC ad to
use because the last time that it ran that it added GCSE only after installing rights.
So essentially the order of operations is different.
So the cash cannot be used.
So going to save this file after moving GCSE around and we'll try it rebuilding our image for a fourth
time.
So do docker build dot I'll run that.
And now you're going to see.
Yes.
It has to go through that installation process all over again because the order of operations have changed.
However it only needs to rerun the steps from the changed line on down.
And so we have to rerun the entire ad GCSE we rerun the entire ad apparatus and we have to rerun the
command read as super All right.
So again if you're not changing your Docker file that's great because it means that we can use cash
versions for building the image but the real lesson to learn here is that if you ever expect to have
to change your Docker file you always want to put those changes like as far down as possible.
Now right now that might sound really confusing but we're going to very quickly go through a another
example in a couple of videos where we'll see that by changing the order of operations inside of our
Docker file can dramatically change how long it takes to rebuild our image.
All right.
So quick pause right here and I'll catch you in the next section.