WEBVTT

00:00.570 --> 00:01.403
Instructor: In the last section

00:01.403 --> 00:03.390
we were able to successfully build our image

00:03.390 --> 00:05.700
and then start up our container based on it.

00:05.700 --> 00:08.040
We saw the message listening on Port 8080

00:08.040 --> 00:11.160
but when we actually tried to visit local host 8080

00:11.160 --> 00:13.890
inside the browser, we got a pretty nasty air message.

00:13.890 --> 00:16.593
So let's figure out what went wrong and how to fix it.

00:17.820 --> 00:19.890
All right, so here's the diagram

00:19.890 --> 00:21.330
of what's happening right now.

00:21.330 --> 00:25.050
Our browser is making a request to local host 8080

00:25.050 --> 00:26.310
which is essentially a reference

00:26.310 --> 00:29.880
to your current machine on Port 8080.

00:29.880 --> 00:33.600
By default, no traffic that is coming into your computer or

00:33.600 --> 00:37.710
into your local host network is routed into the container.

00:37.710 --> 00:41.040
So the container essentially has its own isolated set

00:41.040 --> 00:44.670
of ports that can receive traffic, but by default

00:44.670 --> 00:45.900
no incoming traffic

00:45.900 --> 00:49.560
to your computer is going to be directed into a container.

00:49.560 --> 00:52.470
In order to make sure that any request from

00:52.470 --> 00:55.110
either your computer or some outside computer

00:55.110 --> 00:56.940
will be redirected into the container

00:56.940 --> 01:00.123
we have to set up a explicit port mapping.

01:01.350 --> 01:04.110
A port mapping essentially says anytime

01:04.110 --> 01:05.610
that someone makes a request

01:05.610 --> 01:08.040
to a given port on your local network,

01:08.040 --> 01:11.100
take that request and automatically forward it

01:11.100 --> 01:13.830
to some port inside the container.

01:13.830 --> 01:15.690
So in other words, if anyone makes a request

01:15.690 --> 01:18.210
to local host 8080, take that request

01:18.210 --> 01:22.200
automatically forward it into the container on port 8080

01:22.200 --> 01:24.450
where the node application can then receive it

01:24.450 --> 01:27.960
and process the request and eventually respond to it.

01:27.960 --> 01:29.880
Now, one thing that I wanna make sure is really

01:29.880 --> 01:31.620
clear right now from the get-go

01:31.620 --> 01:35.640
this is only talking about incoming requests.

01:35.640 --> 01:39.030
Your Docker container can by default make requests

01:39.030 --> 01:41.850
on its own behalf to the outside world.

01:41.850 --> 01:42.870
We've already seen that

01:42.870 --> 01:46.053
in action anytime that you've been installing a dependency.

01:46.920 --> 01:50.820
When we ran npm install during the Docker built process just

01:50.820 --> 01:54.090
a moment ago, npm reaches to the outside world.

01:54.090 --> 01:56.250
It reaches out across the internet, and so

01:56.250 --> 02:00.180
there's no limitation by default on your container's ability

02:00.180 --> 02:01.530
to reach out.

02:01.530 --> 02:03.660
It's strictly a limitation on the ability

02:03.660 --> 02:07.800
for incoming traffic to get into the container.

02:07.800 --> 02:09.810
So in order to set up this mapping, in order to

02:09.810 --> 02:12.450
kind of forward this traffic to a specific port

02:12.450 --> 02:15.510
inside the container, we're gonna make a slight adjustment

02:15.510 --> 02:18.270
to the way in which we start the container up

02:18.270 --> 02:21.810
or specifically the Docker run command.

02:21.810 --> 02:24.060
So this is not a change that we're going to make

02:24.060 --> 02:25.110
to the Docker file.

02:25.110 --> 02:28.590
We do not set up port forwarding inside the Docker file.

02:28.590 --> 02:32.190
The port forwarding stuff is strictly a runtime constraint.

02:32.190 --> 02:34.710
In other words, it's something that we only change when

02:34.710 --> 02:37.893
we run a container or start a container up.

02:39.540 --> 02:41.040
So here's what we're gonna do,

02:42.129 --> 02:42.962
to set up docker run

02:42.962 --> 02:43.795
with port mapping.

02:43.795 --> 02:46.560
We're gonna add on a dash P flag.

02:46.560 --> 02:50.310
The syntax overall is docker, run, then dash P.

02:50.310 --> 02:53.550
Then we're gonna specify that any incoming traffic

02:53.550 --> 02:55.650
on our local network

02:55.650 --> 02:58.830
to this port right here should be automatically forwarded

02:58.830 --> 03:02.160
onto this port inside the container right here.

03:02.160 --> 03:03.510
And notice how there's a separation

03:03.510 --> 03:06.540
between these two ports or these two numbers of a colon.

03:06.540 --> 03:07.950
And then as usual, after that

03:07.950 --> 03:11.580
we're gonna specify either the image ID or the image name

03:11.580 --> 03:13.130
which is what we are now using.

03:14.100 --> 03:15.570
So let's flip over to our terminal

03:15.570 --> 03:18.240
and we're going to give this a quick shot.

03:18.240 --> 03:20.130
I'm gonna first stop the running container

03:20.130 --> 03:21.610
by pressing control C

03:22.770 --> 03:25.500
and then I'm going to start the container back up.

03:25.500 --> 03:28.110
But this time I'm gonna set up some port mapping.

03:28.110 --> 03:30.720
So I'll say anytime a request comes in

03:30.720 --> 03:35.720
to 8080 on my local machine, redirect it to port 8080

03:36.060 --> 03:37.593
inside the container.

03:38.700 --> 03:39.533
And I'll make sure

03:39.533 --> 03:41.580
that I still specify the image that I want to use

03:41.580 --> 03:44.340
for this container as my docker ID.

03:44.340 --> 03:48.330
You'll put whatever yours is right there, simple web

03:48.330 --> 03:51.333
and I'm gonna get that on to just one line like so.

03:53.190 --> 03:55.020
Okay, so let's start this.

03:55.020 --> 03:56.280
Now from the outside, it looks

03:56.280 --> 03:59.370
like everything is still identical to how it was before.

03:59.370 --> 04:02.100
But if you flip on over to your browser,

04:02.100 --> 04:05.550
open up a new tab again and go to local host 8080

04:05.550 --> 04:07.590
you'll very quickly see the text, hi there

04:07.590 --> 04:09.210
appear on the screen.

04:09.210 --> 04:11.250
This is the response that we would expect to see

04:11.250 --> 04:12.900
from the running node application.

04:12.900 --> 04:13.733
So that means

04:13.733 --> 04:16.320
that we are now correctly forwarding incoming requests

04:16.320 --> 04:17.910
into the container.

04:17.910 --> 04:20.100
The node application is processing the request

04:20.100 --> 04:22.890
and then sending a response back out.

04:22.890 --> 04:23.730
So that's pretty much it.

04:23.730 --> 04:26.853
That is port mapping in a nutshell.

04:27.810 --> 04:28.643
Now, very quickly

04:28.643 --> 04:29.970
one last little thing that I wanna show you

04:29.970 --> 04:33.540
because this will be done very frequently on real projects.

04:33.540 --> 04:36.780
The port from the source machine or the source network

04:36.780 --> 04:39.990
that we are mapping from to the port that we are mapping to

04:39.990 --> 04:43.530
inside the container do not have to be identical.

04:43.530 --> 04:45.120
So we could very easily say,

04:45.120 --> 04:46.020
Hey, anytime

04:46.020 --> 04:49.830
that a request comes into local host 5,000, redirect

04:49.830 --> 04:52.800
that request to 8080 inside the container.

04:52.800 --> 04:54.540
And like I said, this is actually something that

04:54.540 --> 04:56.760
we're going to do quite frequently in production

04:56.760 --> 04:57.813
applications.

04:58.860 --> 05:01.530
So now this is essentially saying, okay

05:01.530 --> 05:05.670
browsers making request of 5,000, redirect request to 8080

05:05.670 --> 05:06.600
inside the container.

05:06.600 --> 05:10.290
Again, these two ports do not have to be identical.

05:10.290 --> 05:13.320
So let's try doing that again with the docker run command.

05:13.320 --> 05:15.900
I'm gonna stop the thing by pressing control C

05:15.900 --> 05:19.350
and then we'll do docker run dash P, and I'll say

05:19.350 --> 05:23.850
that I wanna map port 5,000 on my local network to 8080

05:23.850 --> 05:26.070
inside the container.

05:26.070 --> 05:28.533
And then I'll do Steven Grider simple web.

05:30.180 --> 05:33.030
Okay, looks like everything started up successfully.

05:33.030 --> 05:35.850
Now if I go back over to my browser and I try to

05:35.850 --> 05:39.210
refresh local host colon 8080, I'll very quickly

05:39.210 --> 05:40.890
see an error message.

05:40.890 --> 05:44.160
But if I instead go to local host 5,000, well

05:44.160 --> 05:45.090
there's the node server.

05:45.090 --> 05:47.820
It is now taking traffic or incoming request

05:47.820 --> 05:51.270
to local host 5,000 and redirecting them to 8080

05:51.270 --> 05:53.580
inside the container.

05:53.580 --> 05:55.440
Now, last thing you might be kind of curious

05:55.440 --> 05:57.180
about. You know, this is kind of an obvious,

05:57.180 --> 06:00.510
but I'm gonna say simultaneously a silly question.

06:00.510 --> 06:02.910
Can we change the port inside the container?

06:02.910 --> 06:05.075
Well, yes, of course we can change this to be

06:05.075 --> 06:09.600
you know, 6,000, 7,000, whatever you want it to be.

06:09.600 --> 06:12.210
Just if you change the port inside the container

06:12.210 --> 06:13.410
you need to make sure

06:13.410 --> 06:15.750
that your actual web server application.

06:15.750 --> 06:18.990
So inside my index.js file, we specified

06:18.990 --> 06:22.650
that our node application should listen on port 8080.

06:22.650 --> 06:24.060
So if you're gonna change the target port

06:24.060 --> 06:25.230
inside the container, well

06:25.230 --> 06:27.420
of course you gotta make sure that you change

06:27.420 --> 06:30.540
the port that your application is listening to as well.

06:30.540 --> 06:32.430
You would have to change this to 6,000

06:32.430 --> 06:34.623
or 7,000 or whatever it might be.

06:35.670 --> 06:36.750
Okay, so that's it.

06:36.750 --> 06:39.300
That is port mapping in a nutshell.

06:39.300 --> 06:41.040
The one big thing to remember is that we have

06:41.040 --> 06:44.883
to specify it at runtime, not inside of the docker file.

06:45.870 --> 06:49.260
So we've got one more step towards success.

06:49.260 --> 06:51.660
We are actually able to visit our running application

06:51.660 --> 06:52.890
inside the browser.

06:52.890 --> 06:54.480
So like I said, we are going to run

06:54.480 --> 06:57.630
into a handful of little errors along the way.

06:57.630 --> 07:01.080
We saw a case in which we were not copying

07:01.080 --> 07:03.180
over the package.JSON file,

07:03.180 --> 07:05.550
we saw using a bad base image

07:05.550 --> 07:08.040
and we also saw a lack of port forwarding.

07:08.040 --> 07:10.260
Again, these are all little things that I kind

07:10.260 --> 07:11.700
of expect that you'll see as soon

07:11.700 --> 07:14.760
as you start using Docker on your own personal project.

07:14.760 --> 07:17.760
Now, even though our application is successfully running

07:17.760 --> 07:20.880
there is one last little thing that I want to point out.

07:20.880 --> 07:21.713
So we're gonna go

07:21.713 --> 07:23.880
over one last little fix in the next section.

07:23.880 --> 07:26.330
So quick break and I'll see you in just a minute.
