In the last section we were able to successfully build our image and then start up a container based
on it.
We saw the message listening on Port ADT but when we actually tried to visit local host ADHD inside
the browser we got a pretty nasty error message.
So let's figure out what went wrong and how to fix it.
All right so here's a diagram of what's happening right now.
Our browser is making a request to local host ADHD which is essentially a reference to your current
machine on Port ADHD 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 setup a explicit port mapping a port mapping essentially says any time 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 880 take that request automatically forward
it into the container on Port 880 where the node application can then receive it and process the request
and eventually respond to it.
Now one thing that I want to 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 you've been installing a dependency.
When we ran npm install during the docker build process just a moment ago NPM reaches to the outside
world and reaches out across the Internet.
And so there's no limitation by default on your containers ability to reach out.
It's strictly a limitation on the ability for incoming traffic to get in to 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 going to 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 setup port forwarding inside the doctor 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 setup Docker run with port mapping.
We're going to add on a dash P flack the syntax overall is Docker run then DSP then we're going to specify
that any incoming traffic on our local network to this port right here should be automatically forwarded
on to this port inside the container right here.
And notice how there's a separation between these two ports are these two numbers of a colon.
And then as usual after that we're going to specify either the image I.D. 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 going to first stop the running container by pressing control see and then I'm going to start the
container back up.
But this time I'm going to set up some port mapping so I'll say anytime a request comes in to 80 80
on my local machine redirect it to port 80 80 inside the container and then I'll make sure that I still
specify the image that I want to use for this container as my Docker I.D. you'll put whatever yours
is right there.
Simple web and I'm going to get that on what to just one line like so OK 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 ADHD 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 I want to 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 any time that a request comes into local host five thousand redirect
that request to 880 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 OK browsers make your request a five thousand redirect request to
80 80 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 going to stop the thing by passing control see and I will do doctor run dash P and I'll say that
I want to map port five thousand on my local network to 80 80 inside the container and then I'll do
Steven Greider simple web looks like everything started up successfully.
Now if I go back over to my browser and I tried to refresh local host colon 880 all very quickly see
an error message.
But if I instead go to local host five thousand well there's the node server it is now taking traffic
or incoming requests to local OES 5000 and redirecting them to 1880 inside the container.
Now last thing you might be kind of curious about you know this is kind of an obvious but I'm going
to 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 six thousand seven thousand 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 dot J as file we specified that our node application should listen on
Port 88.
So if you're going to change the target port inside the container.
Well of course you've got to make sure the change the port that your application is listening to as
well.
You would have to change this to 6000 or 7000 or whatever it might be.
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 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 were going to run into a handful of little errors along the way.
We saw a case in which we were not copying over the Pakistan Jason 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 going to go over it one last little fix in the next section.
So quick break and I'll see you in just a minute.