1
00:00:02,050 --> 00:00:03,980
Sending HTTP requests

2
00:00:03,980 --> 00:00:06,250
to outside APIs works,

3
00:00:06,250 --> 00:00:07,790
but what about services

4
00:00:07,790 --> 00:00:11,110
or servers running on our local host machine?

5
00:00:11,110 --> 00:00:13,240
At the moment, this is not working,

6
00:00:13,240 --> 00:00:16,720
which is why I had to comment this code out.

7
00:00:16,720 --> 00:00:21,470
Now I will comment it back in and remove this line here.

8
00:00:21,470 --> 00:00:25,550
And now I wanna make sure that my Dockerize application

9
00:00:25,550 --> 00:00:30,510
is able to talk to my locally installed MongoDB database.

10
00:00:30,510 --> 00:00:33,270
Now, you will only be able to follow along

11
00:00:33,270 --> 00:00:35,200
if you installed MongoDB,

12
00:00:35,200 --> 00:00:37,440
as I showed you a couple of minutes ago.

13
00:00:37,440 --> 00:00:39,250
But you don't need to do that

14
00:00:39,250 --> 00:00:41,880
because this will only be an intermediate step

15
00:00:41,880 --> 00:00:45,970
until we later also put MongoDB into a container.

16
00:00:45,970 --> 00:00:48,520
But for the moment, it's not in a container.

17
00:00:48,520 --> 00:00:52,900
MongoDB is installed and running on my local machine.

18
00:00:52,900 --> 00:00:55,990
So not in a container, but on my local machine.

19
00:00:55,990 --> 00:00:59,290
This Node app on the other hand is in a container.

20
00:00:59,290 --> 00:01:01,930
And we just saw that this code here

21
00:01:01,930 --> 00:01:03,773
won't work just like that.

22
00:01:04,670 --> 00:01:06,400
So how can we make it work?

23
00:01:06,400 --> 00:01:10,020
For that all first stopped my running Node container

24
00:01:10,020 --> 00:01:12,730
so that we can restarted it later.

25
00:01:12,730 --> 00:01:15,340
But that alone, of course won't do the trick.

26
00:01:15,340 --> 00:01:17,220
Just restarting is not enough.

27
00:01:17,220 --> 00:01:19,930
We need to change something about this code

28
00:01:19,930 --> 00:01:22,793
or about the way we start this container.

29
00:01:23,870 --> 00:01:28,690
And for communication between your Dockerize application

30
00:01:28,690 --> 00:01:31,140
and your code in your Docker container

31
00:01:31,140 --> 00:01:33,320
and your local host machine

32
00:01:33,320 --> 00:01:35,400
for this kind of communication,

33
00:01:35,400 --> 00:01:39,530
you really only need to change your code in here.

34
00:01:39,530 --> 00:01:43,430
You don't need to start your container differently.

35
00:01:43,430 --> 00:01:46,570
Instead, there is a special instruction,

36
00:01:46,570 --> 00:01:50,750
a special hint you can give to Docker in your code.

37
00:01:50,750 --> 00:01:54,400
There is a special address you can use for your requests.

38
00:01:54,400 --> 00:01:58,980
You need to replace local hosts with a special domain,

39
00:01:58,980 --> 00:02:00,860
which is understood by Docker

40
00:02:00,860 --> 00:02:04,453
and that's host.docker.internal.

41
00:02:05,660 --> 00:02:08,840
This special domain is recognized by Docker.

42
00:02:08,840 --> 00:02:10,729
It's understood by Docker.

43
00:02:10,729 --> 00:02:15,730
And it's translated to the IP address off your host machine

44
00:02:15,940 --> 00:02:19,400
as seen from inside the Docker container.

45
00:02:19,400 --> 00:02:23,730
And you can use this anywhere where you need a domain,

46
00:02:23,730 --> 00:02:25,530
where you need a URL.

47
00:02:25,530 --> 00:02:28,660
Here, it's for a MongoDB type of request.

48
00:02:28,660 --> 00:02:31,410
But it could also be for an HTTP request,

49
00:02:31,410 --> 00:02:34,370
if you had some web server running on your machine

50
00:02:34,370 --> 00:02:36,810
and your code would need to talk to that.

51
00:02:36,810 --> 00:02:38,483
Here, it's MongoDB though.

52
00:02:39,400 --> 00:02:40,260
Now under the hood,

53
00:02:40,260 --> 00:02:43,620
this will be transformed to this IP address

54
00:02:43,620 --> 00:02:44,970
off your local host machine

55
00:02:44,970 --> 00:02:47,410
as seen from inside this container.

56
00:02:47,410 --> 00:02:48,380
But the great thing is

57
00:02:48,380 --> 00:02:50,350
that you don't have to worry about that.

58
00:02:50,350 --> 00:02:52,870
You just use this special domain

59
00:02:52,870 --> 00:02:55,740
and Docker will do the rest.

60
00:02:55,740 --> 00:02:57,140
So to make this work,

61
00:02:57,140 --> 00:03:00,340
we now need to rebuild this image

62
00:03:00,340 --> 00:03:03,090
since we changed the source code.

63
00:03:03,090 --> 00:03:05,730
And after this image was rebuilt,

64
00:03:05,730 --> 00:03:07,660
we can run our container again,

65
00:03:07,660 --> 00:03:10,563
based on this rebuild updated image.

66
00:03:11,470 --> 00:03:13,550
So I will hit enter here.

67
00:03:13,550 --> 00:03:16,540
And if we now inspect the running containers,

68
00:03:16,540 --> 00:03:19,493
we see that this container is up and running.

69
00:03:20,410 --> 00:03:22,950
And if I try this with Postman

70
00:03:22,950 --> 00:03:27,280
and I send a GET request to local host 3,000 favorites,

71
00:03:27,280 --> 00:03:28,770
I get this favorite,

72
00:03:28,770 --> 00:03:31,233
which I stored a couple of lectures ago.

73
00:03:32,090 --> 00:03:34,430
Because I'm now successfully talking

74
00:03:34,430 --> 00:03:38,640
to this MongoDB database which runs on my host machine,

75
00:03:38,640 --> 00:03:42,433
but I'm talking to it, from inside this Dockerize Node app.

76
00:03:43,510 --> 00:03:46,200
And the data which has stored a couple of lectures ago

77
00:03:46,200 --> 00:03:47,380
is still there,

78
00:03:47,380 --> 00:03:49,880
because this database is at the moment

79
00:03:49,880 --> 00:03:51,760
not running in a container,

80
00:03:51,760 --> 00:03:53,080
but on my host machine.

81
00:03:53,080 --> 00:03:56,060
And therefor all the data of course survives.

82
00:03:56,060 --> 00:03:59,500
And this therefor proofs that this communication

83
00:03:59,500 --> 00:04:02,810
from inside your Dockerize app to your host machine

84
00:04:02,810 --> 00:04:07,560
is also possible with this special domain.

85
00:04:07,560 --> 00:04:10,950
And that's the second type of communication,

86
00:04:10,950 --> 00:04:13,430
from Dockerize app to host machine.

87
00:04:13,430 --> 00:04:15,850
So from the container to host machine.

88
00:04:15,850 --> 00:04:20,850
This works but you need to use host.docker.internal

89
00:04:21,910 --> 00:04:25,560
as a URL, as a domain, so to say.

90
00:04:25,560 --> 00:04:29,180
Then Docker can do all required transformations

91
00:04:29,180 --> 00:04:32,233
and it's able to ensure that this communication

92
00:04:32,233 --> 00:04:35,113
between container and host machine works.

