Complete Intro to Containers, v2

Building a Node.js App Container

Complete Intro to Containers, v2

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

The "Building a Node.js App 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 builds a container for a Node.js application. The server JavaScript file is added to the container using the COPY command in the Dockerfile. When the container is run, the port for the server can be exposed to the host computer using the publish flag.

Preview
Close

Transcript from the "Building a Node.js App Container" Lesson

[00:00:00]
>> Brian Holt: Let's build a slightly more complicated Node.js application. So I'm gonna go back over here to VS Code, I'm gonna create a new file and I'm gonna call index.js. And I don't wanna write all of it, so I'm just gonna copy and paste all of my code here.

[00:00:21]
>> Brian Holt: Again, not a Node course.
>> Brian Holt: This is essentially the most basic Node app you can ever build, there's no dependencies. There's not even anything to npm install here, right?
>> Brian Holt: So, now I have a index.js. How do I get this into my container, right? Cuz right now if I just build this again, there's nothing in there that's referencing it, right?

[00:00:52]
>> Brian Holt: So, I have to put a copy statement in here, I have to say copy index.js, I have to tell it where it's coming from. So, in this particular case, it's coming from this particular directory I'm in and I'm telling you where to put it. Now I could say dot, right?

[00:01:10]
Which would just say put it here, or I can just say index js. And then here I'm just gonna put index.js, okay? That's builder.
>> Brian Holt: So Dr Bill, we'll just call this version 3, cuz why not? And you can see it's doing a bit more up here now, right?

[00:01:35]
It copied some files, look at that.
>> Brian Holt: So, now I'm gonna say docker run my node app three
>> Brian Holt: And now it's running a server.
>> Brian Holt: Now, notice if I go back to my index.js here, I say, listen on port 3000, one would reasonably that I could go to local host 3000 and that it would work, but it doesn't.

[00:02:09]
Why? Well, I have a container that's running, that's listening on port 3000, but by default, containers don't expose any of their ports. It's generally a good thing, right? It's a security vulnerability otherwise. So, how do we get it to expose that port 3000 to the outside consumer? We have to publish it.

[00:02:35]
So I'm gonna stop my server here, I guess this is another annoyance of Docker. So, he knows why he's did a control C and it's not doing anything, it's because I didn't tell it how to shut down. So I actually have to go say docker kill, I don't even remember what I called it, docker ps, docker kill xen_org, yours is probably gonna be something else, right?

[00:03:05]
>> Brian Holt: Yeah, so,
>> Brian Holt: That killed it, and I'm now gonna say docker run. In order to get around that issue of not being able to Ctrl + C to get out of it, you can do -- init, and that will allow you to control C out of it, that's very helpful for this.

[00:03:27]
Normally what you should do if you're like doing this with like, Node.js or something like that. You should just handle that SIGTERM interrupt directly in your code, or use some package that does it for you.- -init is Docker-based. Basically saying I give up, I don't wanna handle this stuff handle it for me, but it is a blunt instrument, so, it's literally just gonna just cut the entire process off and chuck it out.

[00:03:51]
Normally, processes like to be like, wound down, let them finish whatever they're doing, let them sink out their logs and then shut down gracefully. Just good to know, but I use --init at all time cuz normally I don't care. All right --publish,
>> Brian Holt: Or if you are lazy like I am, you can just do -p.

[00:04:17]
And then you have 3000:3000. One those is the source port, and one of them is the destination port. So, you can't just do publish 3000 you have to do 3000:3000 but let's say you had it running on port 80, and you wanted to export it on port 8080, or something like that.

[00:04:37]
You could do some fun mapping that way. The first one represents the host machine, the second one represents what port is being used as the container, which is always the opposite of what I expected. So I have to remember that whatever I think is wrong. In any case, they're the same doesn't matter.

[00:04:56]
And I wouldn't do it for my node app. Let's give it a name as well so I don't have to remember it.
>> Brian Holt: And I need a :3 cuz I give it a name tag. Okay, server started it's running. And now if I go into here and I refresh the page, you can see it is actually exposing our node app outside.

[00:05:32]
>> Brian Holt: Pretty cool, so we just containerized our first node.js application.
>> Brian Holt: And as you can see here, not that hard, right? Just a couple of commands, and we were done. Think these are called instructions. Every line is called an instruction, I think.
>> Speaker 1: Is there any then in your Docker file, to just actually expose those ports, or is that deprecated?

[00:05:57]
>> Brian Holt: So it's a good question, I think I actually have a little section on them. But the answer to your question is there is an instruction called EXPOSE,
>> Brian Holt: And why don't we use it? Well, one, it's just a suggestion and it's not enforced. So, if I just run this as is exactly the same way it still won't work.

[00:06:25]
There is something that you can run this, I forgot the name of the flag that basically says, respect all the exposes. Then what it's gonna do is, it's going to expose port 3000 on port 32,500 or something like that. And it randomly assigns the port, so it's not very convenient.

[00:06:44]
In other words, no one uses it, everyone just has published using the command line arguments. So now that I've done --init, you can see here I got some requests receipts here. If I hit Ctrl+C, it actually will shut down the server which is nice as well.

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