Check out a free preview of the full Complete Intro to Containers, v2 course

The "Layers" Lesson is part of the full, Complete Intro to Containers, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how layers allow for faster rebuilds of an image. Commands like adding Node or installing dependencies can be skipped when the only change is a source code modification. Layers will pull the other files and data from cache and only rebuild the necessary changes.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Layers" Lesson

[00:00:00]
>> Brian Holt: So we talked a little bit about layers. I'm gonna show you where layers actually becomes really important. Every time that you and I have been going back and forth and I make one change in the code, I have to go rebuild the project. That's normal, that's just how Docker works, and that's okay.

[00:00:15]
That's the issue with the way that we're doing this right now is, Docker is very smart about rebuilding. So for example here, let's get rid of the expose.
>> Brian Holt: It knows every time that I save a new file in there, I don't have to redo this. Nothing changed here, I don't have to redo this, nothing changed here.

[00:00:40]
I don't have to redo this, nothing changed here, and it's smart enough to see, some files here have changed, I gotta do stuff now. And then it'll run everything after this. So it can't assume that this one's not gonna change because something that was done here almost certainly will affect what's been done here.

[00:01:02]
Why is that that?
>> Brian Holt: Every time you change a file, you now have to run npm install every time. If any one of you are no developers, you know that can be extremely painful, right? You don't wanna do that. You don't wanna have to install literally 30,000 dependencies every single time that you change, the punctuation in your code.

[00:01:26]
So what do we do about that? So we don't have endless rebuild time. Well, you gotta get a little bit more explicit. Your Docker files gonna be a little less pretty, but your rebuild times will be so much better. So what I'm gonna do is I'm gonna say copy --chown and I'm gonna say, node:node or you can just say node, node is the name of the group as well.

[00:01:52]
So if you're familiar with Linux terms, there's the user in the group. There is a user called node, there is a group called node, this is just being explicit about that. And I'm going to copy package-lock. That's actually a clever way of doing it. I was gonna do package.json and then package-lock.json and ./.

[00:02:23]
>> Brian Holt: So this will copy, just the package.json, and the package-lock and nothing else.
>> Brian Holt: And it's gonna copy into whatever home directory we're in. Actually, that that was pretty clever that you could actually just put star here, right? And that'll actually pick up both of them, which kinda fun, right, same shit.

[00:02:49]
>> Brian Holt: Okay, and now I can say RUN npm ci, right? I can get rid of that. And now what's awesome about this is, it's gonna copy the package.json, it's gonna RUN npm ci. And then now, anytime I just change, my index.js or my routes.js, it sees that my package.json and my package-lock.json didn't change at all, and it's not going to copy them again.

[00:03:15]
It's just gonna pull these cache layers in and it'll only run this step again. Someone might criticize me here and say, you're copying your package files twice, and my response to you is, I do not care, who cares? You would have some big gross command to exclude them, why not just copy twice?

[00:03:37]
No one's gonna get it hurt by doing it that way.
>> Brian Holt: So again, we had to split this into two commands. But this is gonna go so much faster, right? I don't have to RUN npm ci anymore, every single time.
>> Speaker 2: And that would be the same story for running NPM build after you copy the note stuff in or the other code, right?

[00:04:02]
>> Brian Holt: What's hard about that is that is going to depend on all the files that you're copying in, right? So if you have, let's say, half of your project is docs and half of it is app code, you probably don't have to rebuild the docs every single time that you change app code.

[00:04:18]
So that would be okay to RUN npm build before then. So you don't have to do it every time you change app code. But if you are running an Astro project, you actually probably do have to rebuild your project every single time, right? So in that case, this isn't gonna help you.

[00:04:32]
Does that make sense? That's a good question. All right, so I need to build.
>> Brian Holt: So again, this is coming before, and you can see it took 0.8 seconds, and if I do run, cool, and then now let's just go change something in our file.
>> Brian Holt: Now I'm gonna run this again.

[00:05:00]
>> Brian Holt: And notice that this was basically almost entirely pulled from cache, right? Cached here, cached here, and only this actually ended up running, which is what you want, right? Cuz then you skip that entire step. So this becomes really important with things like apt get dependencies. We'll talk more about those.

[00:05:20]
Node dependencies, anything that you're pulling in that is very easily cacheable and is a slow process. Anything you can do to get that higher up in your Docker files is really gonna make your life a lot easier.
>> Brian Holt: Make sense? Questions?
>> Speaker 3: The obvious next step is I don't wanna rebuild my Docker image every time I change my app code.

[00:05:44]
>> Brian Holt: I will show you how to do that. That is coming, so a development style container. I have actually two ways of showing you how to do that.
>> Brian Holt: And eventually this is the stuff that you would do with this would end up just being production container. Which is to say, you should probably still pull some of these tricks because saving times in your build step does mean something, right?

[00:06:07]
It depends on how much time you're saving, but it can be meaningful. But yeah, I'll show you here in a second.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now