1 00:00:00,540 --> 00:00:05,160 In this section there's one last quick thing that I want to mention about the docker build process. 2 00:00:05,190 --> 00:00:09,990 This is an important thing to understand because it's what gives Docker so much performance whenever 3 00:00:10,080 --> 00:00:12,500 creating a new image. 4 00:00:12,510 --> 00:00:14,840 All right so I'm looking at my Docker file right here. 5 00:00:14,910 --> 00:00:19,980 We have our three instructions and as you very well know by this point out of each one of these instructions 6 00:00:19,980 --> 00:00:22,200 we essentially get a new image. 7 00:00:22,200 --> 00:00:26,790 So from Alpine we get the Alpine image from the run instruction. 8 00:00:26,820 --> 00:00:32,400 We get a temporary image that gets fed into the CMT instruction which gives us another temporary image. 9 00:00:32,400 --> 00:00:38,490 So at every structured instruction along the way we have a file system snapshot and a start up command 10 00:00:38,520 --> 00:00:39,810 for that image. 11 00:00:39,810 --> 00:00:45,520 Now I want to show you something a little bit interesting I'm going to go back over to my doctor file 12 00:00:45,850 --> 00:00:49,900 and on the run instruction I'm going to add a new one right underneath it. 13 00:00:49,900 --> 00:00:55,220 I'm going to say run a P.K. add dash dash update GCSE. 14 00:00:55,330 --> 00:00:59,320 So we're just installing a very random second dependency here. 15 00:00:59,320 --> 00:01:01,600 There's nothing important that you need to know about GCSE. 16 00:01:01,600 --> 00:01:04,190 We're just installing a second dependency. 17 00:01:04,330 --> 00:01:08,470 So I want you to think about what effect this is going to have on our overall flow. 18 00:01:08,470 --> 00:01:11,980 Essentially what it's going to do is add in a new instruction right here. 19 00:01:11,980 --> 00:01:14,410 That's going to look a little something like this. 20 00:01:14,440 --> 00:01:16,990 We're going to see a new one. 21 00:01:17,040 --> 00:01:18,200 Let's see how quickly I can do this. 22 00:01:18,210 --> 00:01:22,620 Edit we're going to get our new instruction right there. 23 00:01:23,700 --> 00:01:29,920 It's going to be very similar to the image from the previous step and it's going to have a new little 24 00:01:29,920 --> 00:01:33,970 program or folder or something inside of it of GCSE. 25 00:01:34,330 --> 00:01:39,040 So essentially it is identical to the previous image but it has this extra little program that has been 26 00:01:39,130 --> 00:01:40,340 added in. 27 00:01:40,360 --> 00:01:41,870 Let's now flip over to our terminal. 28 00:01:41,890 --> 00:01:46,450 I want to rebuild our image and you're going to notice something kind of interesting. 29 00:01:46,510 --> 00:01:51,790 So back over at my terminal I'm still inside of my retina image directory and I'm going to build my 30 00:01:51,790 --> 00:01:58,310 image a second time by running docker build dot all right. 31 00:01:58,360 --> 00:02:03,230 We're going to very quickly see some installation steps right here as it adds in that additional package. 32 00:02:03,230 --> 00:02:09,200 But I want to scroll up to the second instruction and on point out something interesting up there so 33 00:02:09,200 --> 00:02:12,920 we still see it step one of four right here where we do it from Alpine. 34 00:02:12,920 --> 00:02:18,200 You'll notice that we do not see any of that like fetching image stuff that we saw the first time because 35 00:02:18,200 --> 00:02:20,420 we have already fetched the Alpine image. 36 00:02:20,420 --> 00:02:24,440 We've already downloaded it from the Docker Hub and so we don't have to go and download it a second 37 00:02:24,440 --> 00:02:25,580 time. 38 00:02:25,580 --> 00:02:29,900 Now the interesting thing I want to show you and this is where Docker gets so much of its speed and 39 00:02:29,900 --> 00:02:31,840 performance from when building an image. 40 00:02:31,880 --> 00:02:37,070 You'll notice that during step number two right here we do not see any of that stuff around running 41 00:02:37,130 --> 00:02:38,210 in blah blah blah. 42 00:02:38,420 --> 00:02:39,470 We don't see any fetch. 43 00:02:39,470 --> 00:02:42,260 We don't see any installation of dependencies. 44 00:02:42,260 --> 00:02:46,320 Instead we see a single message that says using cash. 45 00:02:46,340 --> 00:02:52,600 So what this means is that Docker has realized that from the previous step two step number two. 46 00:02:52,730 --> 00:02:57,890 Nothing has changed from the last time that we ran Docker built. 47 00:02:57,890 --> 00:03:02,990 In other words it knows without a doubt that it's going to get the same image from the previous step 48 00:03:03,080 --> 00:03:06,870 because it's the exact same instruction that it was before. 49 00:03:07,220 --> 00:03:13,020 And then four step number two Docker knows that it has already executed the command API Kate. 50 00:03:13,110 --> 00:03:19,340 Add update Reddit it's already generated an image out of that step this image right here and this image 51 00:03:19,460 --> 00:03:22,790 has been cached and stored on your local machine. 52 00:03:22,790 --> 00:03:28,190 So rather than go into the process of creating another container out of the Alpine image and running 53 00:03:28,250 --> 00:03:33,860 a P.K. ad update read us inside that container a second time it says you know what I've already done 54 00:03:33,860 --> 00:03:38,190 this work I'm just going to use the image that I generated during the previous step again. 55 00:03:39,320 --> 00:03:45,770 But then after that Docker correctly sees that there is a new command in play and this is not ad update 56 00:03:45,770 --> 00:03:47,670 read as it's at update JCC. 57 00:03:47,690 --> 00:03:52,400 It sees that there's a new command right here a new instruction in play and so from this point on it 58 00:03:52,400 --> 00:03:53,690 decides well you know what. 59 00:03:53,690 --> 00:03:55,710 Something has changed during the build process. 60 00:03:55,760 --> 00:03:57,660 We probably can't use our cash anymore. 61 00:03:57,740 --> 00:04:03,650 And from here on out we have to go through that process of generating a container and running a command 62 00:04:03,650 --> 00:04:08,690 inside the container and taking the snapshot and going through all that stuff again. 63 00:04:08,690 --> 00:04:15,740 Now the interesting thing about this is that it has now executed run KPK at GCSE and it has generated 64 00:04:15,770 --> 00:04:18,390 an image out of that instruction right there. 65 00:04:18,410 --> 00:04:25,340 So if we now run docker build dot a third time without making any changes to our Docker file Docker 66 00:04:25,340 --> 00:04:29,030 is going to correctly see we have made no changes to the docker file. 67 00:04:29,030 --> 00:04:31,040 None of these steps have changed whatsoever. 68 00:04:31,040 --> 00:04:37,060 And so it's going to use the cached or saved versions of each of these images in building this new one. 69 00:04:37,070 --> 00:04:37,940 Let's do that right now. 70 00:04:37,950 --> 00:04:39,450 You're going to see it in action. 71 00:04:39,490 --> 00:04:46,650 So are going to Docker build dot a third time and you'll notice that build process one extremely quickly. 72 00:04:46,720 --> 00:04:49,830 So Docker correctly realized that it's already download Alpine. 73 00:04:49,840 --> 00:04:53,480 It's realized that it already ran the run command on top of Alpine. 74 00:04:53,590 --> 00:04:55,730 So it's using the cash version that image. 75 00:04:55,810 --> 00:05:00,100 It knows it's already done the JCC so it's using the cash version of that and it knows that it's already 76 00:05:00,100 --> 00:05:03,930 executed command read a server on top of the output of the GC stuff. 77 00:05:03,940 --> 00:05:06,210 And so it's using the cache there again. 78 00:05:06,220 --> 00:05:11,440 So the lesson to learn here is that anytime that we make a change to our Docker file we're going to 79 00:05:11,440 --> 00:05:18,140 have to only rerun the series of steps from the changed line on down as a quick example of that. 80 00:05:18,190 --> 00:05:25,120 Let's try taking this GCSE line right here and I'm going to cut it and put it right above read this. 81 00:05:25,150 --> 00:05:31,450 So even though we're still taking the Alpine image and adding JCC to it and adding reds to it the series 82 00:05:31,450 --> 00:05:33,060 of steps have changed. 83 00:05:33,400 --> 00:05:40,080 And so Docker is going to say OK well this time we're taking Alpine and first adding in GCSE. 84 00:05:40,360 --> 00:05:46,120 So it will use the cash version of Alpine but it's not going to have a cached version of the GC ad to 85 00:05:46,120 --> 00:05:52,560 use because the last time that it ran that it added GCSE only after installing rights. 86 00:05:52,570 --> 00:05:55,020 So essentially the order of operations is different. 87 00:05:55,030 --> 00:05:57,250 So the cash cannot be used. 88 00:05:57,250 --> 00:06:03,160 So going to save this file after moving GCSE around and we'll try it rebuilding our image for a fourth 89 00:06:03,160 --> 00:06:03,550 time. 90 00:06:04,460 --> 00:06:07,870 So do docker build dot I'll run that. 91 00:06:07,870 --> 00:06:08,900 And now you're going to see. 92 00:06:08,960 --> 00:06:09,210 Yes. 93 00:06:09,250 --> 00:06:14,380 It has to go through that installation process all over again because the order of operations have changed. 94 00:06:14,620 --> 00:06:18,930 However it only needs to rerun the steps from the changed line on down. 95 00:06:19,420 --> 00:06:25,660 And so we have to rerun the entire ad GCSE we rerun the entire ad apparatus and we have to rerun the 96 00:06:25,660 --> 00:06:28,160 command read as super All right. 97 00:06:28,340 --> 00:06:33,440 So again if you're not changing your Docker file that's great because it means that we can use cash 98 00:06:33,440 --> 00:06:38,120 versions for building the image but the real lesson to learn here is that if you ever expect to have 99 00:06:38,120 --> 00:06:43,710 to change your Docker file you always want to put those changes like as far down as possible. 100 00:06:43,710 --> 00:06:47,930 Now right now that might sound really confusing but we're going to very quickly go through a another 101 00:06:47,930 --> 00:06:52,790 example in a couple of videos where we'll see that by changing the order of operations inside of our 102 00:06:52,790 --> 00:06:58,300 Docker file can dramatically change how long it takes to rebuild our image. 103 00:06:58,310 --> 00:06:58,610 All right. 104 00:06:58,640 --> 00:07:01,370 So quick pause right here and I'll catch you in the next section.