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

The "Multi-Stage Builds" 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 multi-stage builds can reduce the size of a container. In this case, npm is used in the initial build stage, and the installed Node modules are copied into the container during the second stage. The npm dependency is removed from the container since it's not needed once the modules are installed.

Preview
Close

Transcript from the "Multi-Stage Builds" Lesson

[00:00:00]
>> Brian Holt: So one thing here that we're adding to our final destination container is MPM.
>> Brian Holt: Does your app need MPM to run?
>> Brian Holt: Most of the time not, right? You might have an MPM run statement, but I mean, at the end of the day, that's honestly just a command, right?

[00:00:22]
So you don't need MPM, generally speaking, to run. So let's go ahead and cut that stuff out. So I'm gonna just start collapsing these down a little bit.
>> Brian Holt: The spacing here is It's totally arbitrary in case you're not catching on to that. I'm gonna say, from node 20 as node builder.

[00:00:50]
You can call this whatever you want. This is just like a temporary variable name, basically. And I'm gonna say, workder/builds. This is where I'm actually gonna go build project. I'm gonna take this guy, which one? This one. That one right there. I'm gonna move that up there. And I'm gonna move this mpm ci up here.

[00:01:14]
And then move this one up here, and I also don't need these chown because it does not matter.
>> Brian Holt: This node builder, which, notice this is Debian, right? All this is going to be used for is to build. It's not gonna be used for anything else, we're gonna use it to spin up a container, do a bunch of stuff in it, grab all the stuff out of it and throw it away, okay?

[00:01:48]
So we'll even put a little note there, this is the build step, and this is the production step. Thanks, Google. Okay, so I now have this, and I have this container where I've built everything that I want. We're have to run the add no JS but notice we're not going to add MPM anymore cuz we don't need it,
>> Brian Holt: Okay, we're gonna add the run group, we still need that.

[00:02:19]
We still need the user node. We are going to,
>> Brian Holt: Work dir. That's fine. I have here that we can run mkdir. I don't think we necessarily need to do this. Home node code, but we'll go ahead and do that. And workdir, and then we just have to copy.

[00:02:44]
And you're gonna say fromstage=nodebuilder. And then we're gonna say --chown=node:node, and we're gonna say /build dot. I don't need two equals there, just ones. So what's new here is you see this from step, which basically refers, notice this is called node builder and as this is called node builder up Appear.

[00:03:18]
So you're saying from this previous stage of my build process, copy from the build directory into here, wherever here is, and here is going to be node code, right? So what did we accomplish here? Really, we only cut out MPM, I don't think we did really anything else, right?

[00:03:43]
But let's see if it does anything.
>> Speaker 1: I think your build directory needs to be either builder or build, each step has a different.
>> Brian Holt: Yeah, you're right. Thank you, let's call this build. Yeah, I had that wrong. Line three, just make sure that whatever you have on line three here matches what's on line 15 here.

[00:04:02]
Good, catch.
>> Speaker 2: That directory have to exist ahead of time?
>> Brian Holt: Is that why I did this?
>> Speaker 2: No.
>> Brian Holt: Yeah, is that failing?
>> Speaker 2: Yeah.
>> Brian Holt: So you might need to do a run. Make dot build.
>> Brian Holt: Cool, okay. So let's see how that plays out here.

[00:04:29]
So we're gonna call this 3.
>> Brian Holt: Lot more steps happening here, right? Still able to pull some cash stuff here and there, which is always just impressive to me. I also love that it's something I never think about. They just handle all that stuff behind the scenes. That's my favorite kinda technology.

[00:04:57]
Okay, make sure that we're still working here. I love that this is like we have the most simple node app in the world and we're just over-engineering the hell out of the Docker build process, but I just like doing it. Okay, so that all looks like it works.

[00:05:13]
Alpine 3, we cut a massive 9 megabytes off it for this. So everyone take a victory lap, get some champagne, I'm just kidding. I do use multistage build steps a lot because normally you're cutting out a lot more than just MPM. You're cutting out native dependencies. You're cutting out a bunch of stuff that you would never need.

[00:05:36]
You can, if you have stuff where you have to our Bastion server and then pull down a bunch of secrets. You could do that in a build step so that your production server doesn't have the ability to connect to other stuff like that. There's a bunch of security and vulnerability and stuff like that, you can do in that first stage that never has to make it into your production container, which is helpful.

[00:06:02]
>> Speaker 3: So we don't have to clean up or do anything with the first step. That's just just.
>> Brian Holt: Blown away.
>> Speaker 3: Okay.
>> Brian Holt: Yeah, once this Docker build container stuff finishes, it freezes what's in the production step. Whatever your last one is right? It crystallizes that into a container ships that off and everything else gets thrown away.

[00:06:24]
Pretty cool, right?
>> Speaker 3: Yeah.
>> Brian Holt: Yeah.
>> Speaker 3: And we could go faster if it was no 20 alpine, right?
>> Brian Holt: For this?
>> Speaker 3: Yeah.
>> Brian Holt: Maybe for that first install.
>> Speaker 3: Or is just the downloading part?
>> Brian Holt: Yeah, cuz everything's cashed after that. That's why I'm saying, you don't really need to care because you pay the cost once and then it's just cash forever.

[00:06:45]
I would not put this in as Alpine. There's no reason to

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