-: In the last section, we saw that making a change
to our index.js file and then rebuilding
the image caused the copy step to be completely invalidated.
We had to rerun copy and every step after it,
which means we had to sit through
and wait for all of our dependencies to be installed
through run NPM install.
So in this section, we're gonna try to figure out
a way to avoid having to completely
reinstall all dependencies just
because we made a little change to one
of the source code files inside of our project.
So here's what we'll do.
We're gonna take our current build process
inside of the Docker file
and we're gonna split the copper copy operation
out into two different steps.
The only thing that NPM install right here requires
in order to run successfully is the package.js on file.
That's all we care about.
We don't care about the index.js file
or any other file inside of here.
So during this initial copy step,
I'm gonna say the only thing that I want to
copy is the package.js on file.
So I just changed the instruction right here.
It now says, Look in the current working directory, or look
in the directory specified by the build context argument
of Docker run, or excuse me, Docker build
and find the package.js on file and then copy
that into the current working directory of the container.
We'll then rerun, or excuse me,
we will then run NPM install.
And then after we install all those dependencies,
we'll copy over everything else.
And so we'll do a copy dot slash dot slash like so.
So think about what is gonna happen now as you start to work
on your project, you can make as many changes as you want
to the index.js file and it will not invalidate the cache
for either of these steps right here.
The only time that NPM is NPM install is going to
be executed again, is if we make
a change to that step or any step above it.
And so in other words, the really only effect
this is gonna have is if we make a change
to the package.js on file.
That's really the only situation in which NPM install
will normally be executed again during the build process.
All right, so let's try building this again
and then starting the container up
and just making sure that everything still works.
All right, so I'm gonna flip back over.
I'll do a Docker build dash t, Steven Greiter,
again your Docker ID, not mine, Simple web and then we'll
specify the build context with that simple dot.
So I'm gonna rerun this.
Now we have made a change to the Docker files
so we are gonna see some steps rerun, and then
we eventually get our successfully tagged and built image.
Now before trying to run it, I want you to run
the same command a second time.
So same thing, no changes whatsoever.
So that time it went extremely quickly because we were
able to use the cache version of every single step.
So now let's imagine that we are doing some normal changes
to our application and a normal change would
probably be a change the index.js file.
So maybe we decide that we don't
wanna say bye there anymore.
Maybe instead we wanna send back, How are you doing?
So we've now made a change to the index.js file
and we definitely need to rebuild our image
cuz we do not have any support
for kind of hot reloading of the index.js file
or a project file into the container.
So we have to rebuild the image.
So I'll go back over.
I'm going to rerun the same command for a third time.
And we saw that it executed very quickly
because the only change that we made
to any step along the way was step number five.
We copied over the entire content
so that directory Docker detected
that we made a change to one of those files.
And so it reran that step and every step after it.
But we did not have to rerun the NPM install step
and reinstall those modules, which would've taken,
you know in our application not very long,
but in a real application,
it could take several minutes to run.
All right, so the real lesson to learn here is that yes
it does make a difference the order in which you put
down all these instructions into your Docker file
and wherever possible it is kind of nice to segment
out the copy operations to make sure
that you are only copying the bare minimum
for each successive step.
All right, so that's pretty much it for this application.
Now, I hope that it was not terribly annoying
for me to show you kind of like some of the wrong way
of doing things and then correct them along the way.
I hope that it was kind of nice to see
the air messages appear and see the changes that we made.
Of course, we could have put the right Docker file together
from the get go, but I don't know,
you probably may have not learned quite as much
as seeing the errors occur
and then fixing them in as they occurred.
Now I think this application has turned out pretty well
but it's definitely a very, very basic operation.
So let's take a quick break right now.
We're gonna come back the next section
and we'll start working
on a much more advanced Docker project.
So I'll see you in just a minute.