Check out a free preview of the full Complete Intro to Containers (feat. Docker) course

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

Brian explains what port mapping is using EXPOSE, a Dockerfile command that explicitly instructs users to check a specific port, and demonstrates that it is more convinient to add this information to the markdown of a specific app, instead of Dockerfile.


Transcript from the "EXPOSE" Lesson

>> So I have a little section here just to note on expose, right? So far we've been talking about using publish, right? So if you look here, shut that down. I've been using publish 3000:3000, right? You can technically write in here, I could say EXPOSE, 3000 and I can expose 3000.

It totally would work. So let's get rid of this. The issue here is first of all, I still have to run it with a flag, which is, What is it? Okay, so that's running, and actually, let's do it this way, so it just attaches. So, I have to build it first, let's build it.

Did I save that? Did it run EXPOSE? And the correct one. No, I'm in the wrong one, that's why. Docker file for more complicated node app. And then here I'm gonna say EXPOSE 3000, and I'm gonna build it again. And then I try and run my node app again.

But I'm gonna run it without publish. I'm gonna say detached, so it's running in the background. Now, if I say docker ps. First of all if I try and go over in here and go to localhost:3000, it's not gonna work, right? Cuz it's not actually technically exposed on that particular port.

So what you actually have to do is you have to tell docker, docker kill, this one that I just ran my node app. That's the image, right? So you have to kill this way. Docker kill that.
>> Mystifying journey.
>> What's that?
>> The name of it, mystifying journey.

>> Right, yeah, that will work too. So you have to do it with, let me see if I can docker run --help. There is an extra flag that you have to pass and it might just be EXPOSE. EXPOSE, it's dash -P, there we go. So instead what I have to do, is I have to say dash -P, which is gonna go and expose all of those ports that I put it on.

And now if I say docker ps, I actually don't know what port is going to expose it, just picks one for me. So you can see now that I have 3000, which is gonna go into the container. But coming out of the container is gonna be 32768. So now, I can come over here and put that in here And it works, right now, it's mapping into the container.

Does that seem less convenient? I would assume it probably is, right? And for the most part people don't really use expose for this reason. I've heard some people say that they'll still put expose in here just to let people know is like hey, just so you know, publish on 3000, right?

That's what we're expecting here. It's kinda like documentation, you can easily just put that in the read me. I don't know why you'd necessarily put it in the Docker file. I don't go to the Docker file for documentation. So that's EXPOSE, that was very confusing to me when I was starting out with Docker, so I thought I would hopefully demystify that a little bit for you.

>> So what does publish do? It just kind of packages it all together and like, hey, you can ship this.
>> So publish says like I have this port internally and have this port externally and I want you to plug those two things together, like tunnel out together.

So I can port 8080 inside the container published at the port 3000, it'll connect to those two containers or those two ports, right? So it's port mapping, it's literally what it is, it's just port mapping.

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