Instructor: In the last section
we were able to successfully build our image
and then start up our container based on it.
We saw the message listening on Port 8080
but when we actually tried to visit local host 8080
inside the browser, we got a pretty nasty air message.
So let's figure out what went wrong and how to fix it.
All right, so here's the diagram
of what's happening right now.
Our browser is making a request to local host 8080
which is essentially a reference
to your current machine on Port 8080.
By default, no traffic that is coming into your computer or
into your local host network is routed into the container.
So the container essentially has its own isolated set
of ports that can receive traffic, but by default
no incoming traffic
to your computer is going to be directed into a container.
In order to make sure that any request from
either your computer or some outside computer
will be redirected into the container
we have to set up a explicit port mapping.
A port mapping essentially says anytime
that someone makes a request
to a given port on your local network,
take that request and automatically forward it
to some port inside the container.
So in other words, if anyone makes a request
to local host 8080, take that request
automatically forward it into the container on port 8080
where the node application can then receive it
and process the request and eventually respond to it.
Now, one thing that I wanna make sure is really
clear right now from the get-go
this is only talking about incoming requests.
Your Docker container can by default make requests
on its own behalf to the outside world.
We've already seen that
in action anytime that you've been installing a dependency.
When we ran npm install during the Docker built process just
a moment ago, npm reaches to the outside world.
It reaches out across the internet, and so
there's no limitation by default on your container's ability
to reach out.
It's strictly a limitation on the ability
for incoming traffic to get into the container.
So in order to set up this mapping, in order to
kind of forward this traffic to a specific port
inside the container, we're gonna make a slight adjustment
to the way in which we start the container up
or specifically the Docker run command.
So this is not a change that we're going to make
to the Docker file.
We do not set up port forwarding inside the Docker file.
The port forwarding stuff is strictly a runtime constraint.
In other words, it's something that we only change when
we run a container or start a container up.
So here's what we're gonna do,
to set up docker run
with port mapping.
We're gonna add on a dash P flag.
The syntax overall is docker, run, then dash P.
Then we're gonna specify that any incoming traffic
on our local network
to this port right here should be automatically forwarded
onto this port inside the container right here.
And notice how there's a separation
between these two ports or these two numbers of a colon.
And then as usual, after that
we're gonna specify either the image ID or the image name
which is what we are now using.
So let's flip over to our terminal
and we're going to give this a quick shot.
I'm gonna first stop the running container
by pressing control C
and then I'm going to start the container back up.
But this time I'm gonna set up some port mapping.
So I'll say anytime a request comes in
to 8080 on my local machine, redirect it to port 8080
inside the container.
And I'll make sure
that I still specify the image that I want to use
for this container as my docker ID.
You'll put whatever yours is right there, simple web
and I'm gonna get that on to just one line like so.
Okay, so let's start this.
Now from the outside, it looks
like everything is still identical to how it was before.
But if you flip on over to your browser,
open up a new tab again and go to local host 8080
you'll very quickly see the text, hi there
appear on the screen.
This is the response that we would expect to see
from the running node application.
So that means
that we are now correctly forwarding incoming requests
into the container.
The node application is processing the request
and then sending a response back out.
So that's pretty much it.
That is port mapping in a nutshell.
Now, very quickly
one last little thing that I wanna show you
because this will be done very frequently on real projects.
The port from the source machine or the source network
that we are mapping from to the port that we are mapping to
inside the container do not have to be identical.
So we could very easily say,
Hey, anytime
that a request comes into local host 5,000, redirect
that request to 8080 inside the container.
And like I said, this is actually something that
we're going to do quite frequently in production
applications.
So now this is essentially saying, okay
browsers making request of 5,000, redirect request to 8080
inside the container.
Again, these two ports do not have to be identical.
So let's try doing that again with the docker run command.
I'm gonna stop the thing by pressing control C
and then we'll do docker run dash P, and I'll say
that I wanna map port 5,000 on my local network to 8080
inside the container.
And then I'll do Steven Grider simple web.
Okay, looks like everything started up successfully.
Now if I go back over to my browser and I try to
refresh local host colon 8080, I'll very quickly
see an error message.
But if I instead go to local host 5,000, well
there's the node server.
It is now taking traffic or incoming request
to local host 5,000 and redirecting them to 8080
inside the container.
Now, last thing you might be kind of curious
about. You know, this is kind of an obvious,
but I'm gonna say simultaneously a silly question.
Can we change the port inside the container?
Well, yes, of course we can change this to be
you know, 6,000, 7,000, whatever you want it to be.
Just if you change the port inside the container
you need to make sure
that your actual web server application.
So inside my index.js file, we specified
that our node application should listen on port 8080.
So if you're gonna change the target port
inside the container, well
of course you gotta make sure that you change
the port that your application is listening to as well.
You would have to change this to 6,000
or 7,000 or whatever it might be.
Okay, so that's it.
That is port mapping in a nutshell.
The one big thing to remember is that we have
to specify it at runtime, not inside of the docker file.
So we've got one more step towards success.
We are actually able to visit our running application
inside the browser.
So like I said, we are going to run
into a handful of little errors along the way.
We saw a case in which we were not copying
over the package.JSON file,
we saw using a bad base image
and we also saw a lack of port forwarding.
Again, these are all little things that I kind
of expect that you'll see as soon
as you start using Docker on your own personal project.
Now, even though our application is successfully running
there is one last little thing that I want to point out.
So we're gonna go
over one last little fix in the next section.
So quick break and I'll see you in just a minute.