Transcript from the "Alpine Node.js Container" Lesson
>> You definitely can just go ahead and use node alpine, it's 80 megabytes which is nothing, for your production projects, always do that. Now we're gonna be dumb and we're gonna go make our own node alpine image because I wanna show you how to do it, how to set up all these different things.
[00:00:17] So, what we're actually gonna try and do is we're gonna try and get that 80 megabytes down even further so you can get down to 50 megabytes. Let's do it. Let's be ridiculous. So we're still in alpine-linux here, we're in 12-alpine right here. And what I want you to do instead of saying node 12-alpine, I want you to say alpine:3.10.
[00:00:44] So, if I come in here and say docker inspect alpine:3.10, this is way easier if I just colorize that. You can see here, this is in bytes maybe, so, yeah, this is five point six-ish megabytes. Pretty small, right? That's significantly smaller than the 80 that we had coming from our other application from node alpine, and it just includes some more node nicety stuff like some good idea stuff that you're probably gonna want on there.
[00:01:24] Just include some of the stuff like that, which for the most part you're gonna want. The node source people and the near form is the other company that works on these kinda things. Take pretty good care to make sure that these are as small as possible. So, trust them because they're probably better at docker than maybe you are, I'm not gonna say that, but they're definitely better than I am.
[00:01:44] And if you're for whatever reason listening to me, so let me proxy that goodwill to them. Or maybe you don't like me, I don't know, then you can hate them too. So, we're gonna do from alpine:3.10 here, and what we're gonna do is we're gonna say RUN apk which is the name of the package manager, not Android stuff, if you're coming from Android.
[00:02:10] APK is the Alpine Package Manager. Add, I'm gonna say --update me which just means run updates first before you, make sure you're getting the latest versions of these, and get nodejs and get npm. Now we could tie this to versions, for now I'm just gonna show you how to do it this way.
[00:02:34] Cool and for now I think that's actually all we need to do to get all this running, I don't think we need to do anything else. And let's try this again. We have to run a build. So it's actually able to do this pretty quickly it looks like.
[00:03:05] And again, this probably should be enough that if we took a look at it. Let's just do my-node-app. Right there. We're up to about 85. I guess that sounds about right. So let's actually just restart this entire Docker file. All right. So we just add the FROM alpine:3.10, RUN apk, node npm and we're gonna run this again.
[00:03:42] And now, if we inspect, take a look at what this is, so this ends up being, there you go, about 50 megabytes. Just to get Alpine and NodeJs and NPM, all on the same box. Now, Alpine by default, doesn't necessarily have the node user installed. That was something that the node distribution is doing for us, so we have to do that for ourselves, so we're gonna do RUN addgroup-S.
[00:04:12] Again, this is something I could never remember by myself. I always have to Google, how do I add user to LINUX. I don't know, it's just never something I can remember. So we're gonna just copy their best practices and we're gonna add a user called node and we're going to add user -S node, and in the group node.
[00:04:35] So now, this has added a user called node. And here we can say USER node. And here we can say build. Did I miss one? Adduser, sorry. Adduser right there. This was saying add not found. It's adduser. All right, now try it again. There we go. Now if I say, node app right there, and I say whoamI, should say node, cool.
>> Brian, where did you specify what version of node do you wanna get?
>> So, I didn't, here you would do it with apk. Just for now we're not gonna worry about it cuz this is something that you would never really do. But we can see what version it is.
[00:05:41] So we can say node-- version. Hopefully it's 12, no, it's 10. And I think there is some nomenclature for this, you would say like, at 10.16 or something like that. I am not actually sure. But it would be in there. Again this is from Alpine, so I don't know Alpine at all.
[00:06:12] So, Now is wherever you wanna go back and add all of our stuff back that we had before which is USER node, RUN mkdir /home/node/code. Then you wanna do WORKDIR /home/node/code, COPY chown node node pack. I mean, you can literally copy this from the last one that we used to have, package lock.json package.json.
[00:06:47] I'm gonna copy that to ./. Then we're going to RUN npm CI. As you can see here, whitespace is totally arbitrary. And I have no good reason for any of it that I'm doing right now. And I'm saying COPY --chown node:node, and. . to copy everything else into the same directory.
[00:07:09] I'm going to say CMD node index.js. So, let's go back and build this again. And let's run it. So the link. There we go, still running but with a Alpine container that we're continuing to build ourselves. So all said and done, to see if we actually saved yourself any space, I'm not actually sure we did.
[00:07:44] Docker inspect my-node-app. Pipe to jq to get some colors because it's a lot easier to read. There you go. So we got down to about 56 megabytes-ish. We can go lower. [LAUGH]