Complete Intro to Containers, v2

Making an Alpine Node.js Container

Complete Intro to Containers, v2

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

The "Making an Alpine Node.js Container" 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 attempts to make the container smaller by creating a custom Alpine container for the Node.js application. The resulting container was 80MB smaller than the node-alpine container available in Docker Hub.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Making an Alpine Node.js Container" Lesson

[00:00:00]
>> Brian Holt: Let's make our own node Alpine container. So let's look at our benchmark, what did I have here? Someone remember this, 143 megabytes. But how low can you go? Can we limbo with our containers and do exactly what I told you not to do, because it is fun?

[00:00:19]
Okay.
>> Brian Holt: So let's just go ahead and keep building this project, totally fine here. We're gonna start from alpine, what did I say, 3.19,
>> Brian Holt: Which is, yep, that's the latest one.
>> Brian Holt: The first thing that we're gonna have to do is Alpine, like I said, includes nothing.

[00:00:47]
So we're gonna have to go ahead and add RUN apk's Alpine package manager, something of that nature. It's their apt get, essentially, add. We're gonna say --update node.js and npm, okay? So now we will have node and npm. You can do this from latest as well if you're feeling brave, I'm just gonna keep it on 3.19.

[00:01:18]
>> Brian Holt: So we've now added that.
>> Brian Holt: And that really should be enough for now, do I need to add a user?
>> Brian Holt: Yeah, no, I think we will here in just a second, but let's go ahead and save that for now.
>> Brian Holt: And we will call this my-alpine, just so we can see a difference that we go here.

[00:01:52]
Or maybe we'll call this alpine2.
>> Brian Holt: Okay, no, except we have to build first, not that.
>> Brian Holt: Build, and we're gonna call this alpine2.
>> Brian Holt: Did I not save this? I did not save that, otherwise I was like that's not doing what I want it to do.
>> Brian Holt: Yep, so here's my problem.

[00:02:31]
I was trying to run everything as this user node, but remember that it's the node.js container that includes that node user for us, right? If we don't have that existing, it's gonna be, I don't know who node is, I can't, and it won't create it for you on the fly.

[00:02:48]
So we could have to do this. I'm gonna say RUN addgroup, man, I feel like this is taking my thunder, cuz that's literally what I wanna type. But you can't tell me what to do Github, okay, -G node.
>> Brian Holt: This is going to add a node user and then it's gonna add a group called node as well.

[00:03:16]
So node to node we're just kind of matching exactly what the node container has built into it. I would even say, why do they name them the same thing that's actually kind of annoying to me, but who cares? It's what they do so we're sticking to it. You might ask me here, why wouldn't we just have to run commands here?

[00:03:35]
Cuz we want it to be in one layer, that's that's usually the thing that is. It's fine that you would have two layers there, but I like it that this is kind of doing one thing conceptually to me. So I like putting it all into one layer. All right, so we're gonna try that again, and now because that node user exists up here wherever that is, this one, now this is actually able to finish, all right?

[00:04:02]
So we built it, can we run it?
>> Brian Holt: Localhost 8080 and it's working, cool. And now we can see this is in use, more complicated, alpine2. We went from 142.98 MB to 82 MB. So I don't know what we cut out, I'll be super honest with you, but we cut out 60 MB of something.

[00:04:38]
And that made us a much smaller container. Now question we should really be asking ourselves if you were seriously considering shipping this to production is, what did the node or the docker people feel was so important to add 60 more MB to this container? In the grand scheme of things, 60 MB is nothing right?

[00:05:01]
900 MB, I'm gonna say is something, right? So I agree with the the motion to get to alpine. But I would be really cautious of somebody thought it was important that 60 more megs, why am I messing with this, right? Hopefully you kind of get my hesitancy there, so pretty cool.

[00:05:20]
I've noticed that over the years as this has been growing, I think when I previously taught this course, I think it was like 40 MB instead of 80. So even that base alpine container continues to grow a little bit over time. It's interesting that looking here at my notes, I had 89 MB last week.

[00:05:38]
And today you can see here it's 82 MB. You can see they are changing these things all of the time. Which is great, because I mean someone's caring about your security, right? Which is good that you don't have to do that, as much.

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