1 00:00:00,660 --> 00:00:05,100 In the last section we were able to successfully build our image and then start up a container based 2 00:00:05,100 --> 00:00:05,760 on it. 3 00:00:05,760 --> 00:00:11,490 We saw the message listening on Port ADT but when we actually tried to visit local host ADHD inside 4 00:00:11,490 --> 00:00:13,910 the browser we got a pretty nasty error message. 5 00:00:13,950 --> 00:00:17,620 So let's figure out what went wrong and how to fix it. 6 00:00:17,930 --> 00:00:21,270 All right so here's a diagram of what's happening right now. 7 00:00:21,390 --> 00:00:27,450 Our browser is making a request to local host ADHD which is essentially a reference to your current 8 00:00:27,450 --> 00:00:30,740 machine on Port ADHD by default. 9 00:00:30,810 --> 00:00:37,200 No traffic that is coming into your computer or into your local host network is routed into the container 10 00:00:37,770 --> 00:00:44,520 so the container essentially has its own isolated set of ports that can receive traffic but by default 11 00:00:44,730 --> 00:00:50,610 no incoming traffic to your computer is going to be directed into a container in order to make sure 12 00:00:50,850 --> 00:00:56,970 that any request from either your computer or some outside computer will be redirected into the container. 13 00:00:56,970 --> 00:01:04,890 We have to setup a explicit port mapping a port mapping essentially says any time that someone makes 14 00:01:04,890 --> 00:01:11,610 a request to a given port on your local network take that request and automatically forward it to some 15 00:01:11,610 --> 00:01:13,890 port inside the container. 16 00:01:13,890 --> 00:01:19,290 So in other words if anyone makes a request to local host 880 take that request automatically forward 17 00:01:19,290 --> 00:01:25,710 it into the container on Port 880 where the node application can then receive it and process the request 18 00:01:25,770 --> 00:01:28,020 and eventually respond to it. 19 00:01:28,020 --> 00:01:32,970 Now one thing that I want to make sure is really clear right now from the get go this is only talking 20 00:01:33,030 --> 00:01:40,470 about incoming requests your docker container can by default make requests on its own behalf to the 21 00:01:40,470 --> 00:01:41,840 outside world. 22 00:01:41,880 --> 00:01:43,680 We've already seen that in action. 23 00:01:43,710 --> 00:01:46,560 Anytime you've been installing a dependency. 24 00:01:46,980 --> 00:01:53,760 When we ran npm install during the docker build process just a moment ago NPM reaches to the outside 25 00:01:53,760 --> 00:01:56,040 world and reaches out across the Internet. 26 00:01:56,040 --> 00:02:01,570 And so there's no limitation by default on your containers ability to reach out. 27 00:02:01,590 --> 00:02:07,600 It's strictly a limitation on the ability for incoming traffic to get in to the container. 28 00:02:07,830 --> 00:02:12,810 So in order to set up this mapping in order to kind of forward this traffic to a specific port inside 29 00:02:12,810 --> 00:02:18,030 the container we're going to make a slight adjustment to the way in which we start the container up 30 00:02:18,330 --> 00:02:21,000 or specifically the docker run command. 31 00:02:21,870 --> 00:02:25,150 So this is not a change that we're going to make to the docker file. 32 00:02:25,170 --> 00:02:31,410 We do not setup port forwarding inside the doctor file the port forwarding stuff is strictly a runtime 33 00:02:31,440 --> 00:02:32,250 constraint. 34 00:02:32,250 --> 00:02:39,430 In other words it's something that we only change when we run a container or start a container up. 35 00:02:39,570 --> 00:02:43,710 So here's what we're gonna do to setup Docker run with port mapping. 36 00:02:43,710 --> 00:02:51,540 We're going to add on a dash P flack the syntax overall is Docker run then DSP then we're going to specify 37 00:02:51,660 --> 00:02:58,860 that any incoming traffic on our local network to this port right here should be automatically forwarded 38 00:02:58,860 --> 00:03:02,210 on to this port inside the container right here. 39 00:03:02,220 --> 00:03:06,290 And notice how there's a separation between these two ports are these two numbers of a colon. 40 00:03:06,600 --> 00:03:11,880 And then as usual after that we're going to specify either the image I.D. or the image name which is 41 00:03:11,880 --> 00:03:14,130 what we are now using. 42 00:03:14,160 --> 00:03:17,880 So let's flip over to our terminal and we're going to give this a quick shot. 43 00:03:18,300 --> 00:03:24,400 I'm going to first stop the running container by pressing control see and then I'm going to start the 44 00:03:24,400 --> 00:03:25,500 container back up. 45 00:03:25,510 --> 00:03:32,440 But this time I'm going to set up some port mapping so I'll say anytime a request comes in to 80 80 46 00:03:32,530 --> 00:03:39,870 on my local machine redirect it to port 80 80 inside the container and then I'll make sure that I still 47 00:03:39,870 --> 00:03:45,420 specify the image that I want to use for this container as my Docker I.D. you'll put whatever yours 48 00:03:45,420 --> 00:03:46,600 is right there. 49 00:03:46,650 --> 00:03:55,380 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 50 00:03:55,380 --> 00:03:59,260 the outside it looks like everything is still identical to how it was before. 51 00:03:59,370 --> 00:04:06,000 But if you flip on over to your browser open up a new tab again and go to local host ADHD you'll very 52 00:04:06,000 --> 00:04:07,010 quickly see the text. 53 00:04:07,020 --> 00:04:09,220 Hi there appear on the screen. 54 00:04:09,270 --> 00:04:12,960 This is the response that we would expect to see from the running node application. 55 00:04:12,960 --> 00:04:18,750 So that means that we are now correctly forwarding incoming requests into the container the node application 56 00:04:18,750 --> 00:04:22,880 is processing the request and then sending a response back out. 57 00:04:22,920 --> 00:04:23,730 So that's pretty much it. 58 00:04:23,730 --> 00:04:27,840 That is Port mapping in a nutshell. 59 00:04:27,840 --> 00:04:31,680 Now very quickly one last little thing I want to show you because this will be done very frequently 60 00:04:31,680 --> 00:04:33,600 on real projects. 61 00:04:33,600 --> 00:04:39,240 The port from the source machine or the source network that we are mapping from to the port that we 62 00:04:39,240 --> 00:04:43,540 are mapping to inside the container do not have to be identical. 63 00:04:43,590 --> 00:04:49,800 So we could very easily say Hey any time that a request comes into local host five thousand redirect 64 00:04:49,800 --> 00:04:52,890 that request to 880 inside the container. 65 00:04:52,890 --> 00:04:57,660 And like I said this is actually something that we're going to do quite frequently in production applications 66 00:04:58,960 --> 00:05:05,020 so now this is essentially saying OK browsers make your request a five thousand redirect request to 67 00:05:05,070 --> 00:05:06,670 80 80 inside the container. 68 00:05:06,670 --> 00:05:10,200 Again these two ports do not have to be identical. 69 00:05:10,330 --> 00:05:12,670 So let's try doing that again with the docker run command. 70 00:05:13,360 --> 00:05:19,540 I'm going to stop the thing by passing control see and I will do doctor run dash P and I'll say that 71 00:05:19,570 --> 00:05:26,590 I want to map port five thousand on my local network to 80 80 inside the container and then I'll do 72 00:05:26,590 --> 00:05:33,060 Steven Greider simple web looks like everything started up successfully. 73 00:05:33,080 --> 00:05:39,500 Now if I go back over to my browser and I tried to refresh local host colon 880 all very quickly see 74 00:05:39,500 --> 00:05:40,680 an error message. 75 00:05:40,910 --> 00:05:46,910 But if I instead go to local host five thousand well there's the node server it is now taking traffic 76 00:05:46,940 --> 00:05:53,490 or incoming requests to local OES 5000 and redirecting them to 1880 inside the container. 77 00:05:53,600 --> 00:05:58,190 Now last thing you might be kind of curious about you know this is kind of an obvious but I'm going 78 00:05:58,190 --> 00:06:02,960 to say simultaneously a silly question can we change the port inside the container. 79 00:06:02,960 --> 00:06:08,390 Well yes of course we can change this to be you know six thousand seven thousand whatever you want it 80 00:06:08,390 --> 00:06:15,080 to be just if you change the port inside the container you need to make sure that your actual web server 81 00:06:15,080 --> 00:06:21,440 application so inside my index dot J as file we specified that our node application should listen on 82 00:06:21,440 --> 00:06:22,480 Port 88. 83 00:06:22,700 --> 00:06:25,120 So if you're going to change the target port inside the container. 84 00:06:25,160 --> 00:06:29,840 Well of course you've got to make sure the change the port that your application is listening to as 85 00:06:29,840 --> 00:06:30,500 well. 86 00:06:30,670 --> 00:06:35,970 You would have to change this to 6000 or 7000 or whatever it might be. 87 00:06:36,020 --> 00:06:36,800 So that's it. 88 00:06:36,800 --> 00:06:39,350 That is Port mapping in a nutshell. 89 00:06:39,350 --> 00:06:45,920 The one big thing to remember is that we have to specify it at runtime not inside the docker file. 90 00:06:45,920 --> 00:06:49,290 So we've got one more step towards success. 91 00:06:49,310 --> 00:06:52,940 We are actually able to visit our running application inside the browser. 92 00:06:52,940 --> 00:06:57,650 So like I said we were going to run into a handful of little errors along the way. 93 00:06:57,680 --> 00:07:05,510 We saw a case in which we were not copying over the Pakistan Jason File we saw using a bad base image 94 00:07:05,540 --> 00:07:08,120 and we also saw a lack of port forwarding. 95 00:07:08,120 --> 00:07:12,740 Again these are all little things that I kind of expect that you'll see as soon as you start using Docker 96 00:07:12,740 --> 00:07:14,750 on your own personal project. 97 00:07:14,780 --> 00:07:20,390 Now even though our application is successfully running there is one last little thing that I want to 98 00:07:20,390 --> 00:07:23,910 point out so we're going to go over it one last little fix in the next section. 99 00:07:23,930 --> 00:07:25,790 So quick break and I'll see you in just a minute.