1 00:00:00,720 --> 00:00:05,880 In the last section we saw that making a change to our index such as file and then rebuilding the image 2 00:00:06,090 --> 00:00:09,290 caused the copy step to be completely invalidated. 3 00:00:09,300 --> 00:00:14,850 We had to rerun copy in every step after it which means we had to sit through and wait for all of our 4 00:00:14,850 --> 00:00:18,090 dependencies to be installed through run npm install. 5 00:00:18,450 --> 00:00:23,190 So in this section we're going to try to figure out a way to avoid having to completely reinstall all 6 00:00:23,190 --> 00:00:28,740 dependencies just because we made a little change to one the source code files inside of our project. 7 00:00:28,740 --> 00:00:29,660 So here's what we'll do. 8 00:00:29,730 --> 00:00:34,740 We're gonna take our current build process inside of the docker file and we're going to split the corporate 9 00:00:34,740 --> 00:00:38,990 Copy operation out into two different steps. 10 00:00:39,000 --> 00:00:44,010 The only thing that npm install right here requires in order to run successfully is the package not 11 00:00:44,010 --> 00:00:45,050 JS on file. 12 00:00:45,060 --> 00:00:46,070 That's all we care about. 13 00:00:46,080 --> 00:00:50,220 We don't care about the index such as file or any other file inside of here. 14 00:00:50,970 --> 00:00:57,210 So during this initial copy step I'm going to say the only thing that I want to copy is the package 15 00:00:57,210 --> 00:01:01,680 not just on file so I just change the instruction right here. 16 00:01:01,780 --> 00:01:08,380 It now says look in the current working directory or look in the directory specified by the build context 17 00:01:08,380 --> 00:01:15,010 argument of Docker run or something Docker built and find the package stop Jace on file and then copy 18 00:01:15,010 --> 00:01:22,090 that into the current working directory of the container will then rerun or Samuel will then run npm 19 00:01:22,090 --> 00:01:29,020 install and then after we install all those dependencies we'll copy over everything else and so we'll 20 00:01:29,020 --> 00:01:34,380 do a copy dot slash dot slash like so so. 21 00:01:34,380 --> 00:01:37,630 Think about what is going to happen now as you start to work on your project. 22 00:01:37,750 --> 00:01:43,780 You can make as many changes as you want to the index not just file and it will not invalidate the cache 23 00:01:43,780 --> 00:01:45,760 for either of these steps right here. 24 00:01:45,790 --> 00:01:49,390 The only time that NPM is npm install is going to be executed. 25 00:01:49,390 --> 00:01:54,500 Again if we make a change to that step or any step above it. 26 00:01:54,640 --> 00:01:59,090 And so in other words the really only effect this is going to have is if we make a change to the package 27 00:01:59,100 --> 00:02:00,340 not just on file. 28 00:02:00,370 --> 00:02:06,250 That's really the only situation in which npm install will normally be executed again during the build 29 00:02:06,250 --> 00:02:07,870 process. 30 00:02:07,900 --> 00:02:08,200 All right. 31 00:02:08,350 --> 00:02:12,940 So let's try building this again and then starting the container up and just making sure that everything 32 00:02:13,030 --> 00:02:15,250 still works. 33 00:02:15,250 --> 00:02:15,500 All right. 34 00:02:15,520 --> 00:02:19,450 I'm going to flip back over I'll do a docker build dash T. 35 00:02:19,630 --> 00:02:23,230 Steven Greider again your Docker I.D. not mine. 36 00:02:23,500 --> 00:02:27,940 Simple web and I will specify that Bill context with that simple dot. 37 00:02:28,960 --> 00:02:30,210 So I'm gonna rerun this. 38 00:02:30,250 --> 00:02:35,650 Now we have made a change to the docker files so we are gonna see some steps rerun and then we eventually 39 00:02:35,650 --> 00:02:38,330 get our successfully tagged and built image. 40 00:02:38,530 --> 00:02:43,660 Now before trying to run it I want you to run the same command a second time so same thing. 41 00:02:43,660 --> 00:02:49,940 No changes whatsoever so that time it went extremely quickly because we were able to use the cached 42 00:02:49,940 --> 00:02:51,440 version of every single step. 43 00:02:52,010 --> 00:02:57,110 So now let's imagine that we are doing some normal changes to our application and a normal change would 44 00:02:57,110 --> 00:02:59,480 probably be a change the index not just file. 45 00:02:59,600 --> 00:03:03,510 So maybe we decide that we don't want to say bye there anymore. 46 00:03:03,510 --> 00:03:05,360 Maybe instead we want to send back. 47 00:03:05,450 --> 00:03:07,630 How are you doing. 48 00:03:07,640 --> 00:03:12,770 So we've now made a change the index not J.S. file and we definitely need to rebuild our image because 49 00:03:12,770 --> 00:03:16,600 we do not have any support for kind of hot reloading of the index Dot. 50 00:03:16,600 --> 00:03:22,140 J ust file a project file into the container so we have to rebuild the image. 51 00:03:22,520 --> 00:03:23,560 So I'll go back over. 52 00:03:23,720 --> 00:03:31,430 I'm going to rerun the same command for a third time and we saw that it executed very quickly because 53 00:03:31,430 --> 00:03:35,980 the only change that we made to any step along the way was step number five. 54 00:03:36,020 --> 00:03:41,120 We copied over the entire contents of that directory Docker detected that we made a change to one of 55 00:03:41,120 --> 00:03:45,080 those files and so it reran that step and every step after it. 56 00:03:45,320 --> 00:03:51,050 But we did not have to rerun the NPM install step and reinstall those modules which would have taken 57 00:03:51,530 --> 00:03:56,150 you know in our application not very long but in a real application it could take several minutes to 58 00:03:56,150 --> 00:03:57,070 run. 59 00:03:57,290 --> 00:03:57,620 All right. 60 00:03:57,620 --> 00:04:02,390 So the real lesson to learn here is that yes it does make a difference. 61 00:04:02,390 --> 00:04:08,210 The order in which you put the L.D. instructions into your Docker file and wherever possible it is kind 62 00:04:08,210 --> 00:04:14,270 of nice to segment out the copy operations to make sure that you are only copying the bare minimum for 63 00:04:14,300 --> 00:04:16,790 each successive step. 64 00:04:16,790 --> 00:04:17,180 All right. 65 00:04:17,210 --> 00:04:19,890 So that's pretty much it for this application now. 66 00:04:20,000 --> 00:04:26,270 I hope that it was not terribly annoying for me to show you kind of like some the wrong way of doing 67 00:04:26,270 --> 00:04:28,190 things and incorrect them along the way. 68 00:04:28,190 --> 00:04:33,080 I hope that it was kind of nice to see the error messages appear and see the changes that we made. 69 00:04:33,110 --> 00:04:36,910 Of course we could have put the right Docker file together from the get go but I don't know. 70 00:04:36,920 --> 00:04:42,110 You probably may have not learned quite as much as seeing the errors occur and then fixing them in as 71 00:04:42,110 --> 00:04:43,690 they occurred. 72 00:04:43,720 --> 00:04:49,450 No I think this application has turned out pretty well but it's definitely a very very basic operation 73 00:04:49,870 --> 00:04:51,370 so let's take a quick break right now. 74 00:04:51,370 --> 00:04:56,620 We're going to come back the next section we'll start working on a much more advanced Docker project 75 00:04:56,710 --> 00:04:57,960 so I'll see you in just a minute.