1
00:00:02,029 --> 00:00:05,099
So now that we had a look at two kinds of communication,

2
00:00:05,099 --> 00:00:08,220
there is one last kind of communication,

3
00:00:08,220 --> 00:00:10,900
and that's from container to container.

4
00:00:10,900 --> 00:00:14,040
For that, we first of all need a second container,

5
00:00:14,040 --> 00:00:15,360
because up to this point,

6
00:00:15,360 --> 00:00:19,580
we always had only 1 container working on its own.

7
00:00:19,580 --> 00:00:22,890
Now I want to have 1 container for the note app

8
00:00:22,890 --> 00:00:26,080
and one container for the MongoDB database,

9
00:00:26,080 --> 00:00:28,058
Because as I mentioned earlier, already,

10
00:00:28,058 --> 00:00:31,800
it is a good practice to have every container focus

11
00:00:31,800 --> 00:00:33,300
on just one thing.

12
00:00:33,300 --> 00:00:35,860
So one container for the node Web API,

13
00:00:35,860 --> 00:00:37,920
and one container for the database

14
00:00:38,870 --> 00:00:41,500
and therefore, I will first of all,

15
00:00:41,500 --> 00:00:45,860
stop my running node application container here

16
00:00:45,860 --> 00:00:48,930
with Docker stop favorites,

17
00:00:48,930 --> 00:00:50,930
and once this is stopped,

18
00:00:50,930 --> 00:00:55,140
I want to put my MongoDB database inside of a container.

19
00:00:55,140 --> 00:00:58,500
So I will no longer use the locally installed one,

20
00:00:58,500 --> 00:01:00,200
I could uninstall it

21
00:01:00,200 --> 00:01:03,060
and if you never installed it in the first place,

22
00:01:03,060 --> 00:01:04,890
which you probably didn't,

23
00:01:04,890 --> 00:01:07,180
because I told you that you won't need it,

24
00:01:07,180 --> 00:01:10,010
then you can now follow along because now

25
00:01:10,010 --> 00:01:11,370
is the point of time,

26
00:01:11,370 --> 00:01:14,550
where we're going to set up MongoDB together,

27
00:01:14,550 --> 00:01:16,422
but inside of a container

28
00:01:16,422 --> 00:01:18,030
and that means that we're going

29
00:01:18,030 --> 00:01:21,610
to use two containers instead of just one now.

30
00:01:21,610 --> 00:01:26,610
For that, let's start by creating a MongoDB container

31
00:01:27,390 --> 00:01:29,090
and you could think that we're going

32
00:01:29,090 --> 00:01:31,540
to write another Docker file for that,

33
00:01:31,540 --> 00:01:33,257
but we don't even need to do this,

34
00:01:33,257 --> 00:01:36,680
because if you search for Docker MongoDB,

35
00:01:36,680 --> 00:01:39,530
you will find an official Mongo image

36
00:01:39,530 --> 00:01:41,270
on Docker Hub.

37
00:01:41,270 --> 00:01:43,724
So just as before, with the official node

38
00:01:43,724 --> 00:01:47,793
and Python images, this is an official MongoDB image.

39
00:01:48,800 --> 00:01:51,260
Now you'll find detailed instructions on how

40
00:01:51,260 --> 00:01:54,330
to use it on this Docker Hub page.

41
00:01:54,330 --> 00:01:57,747
That's true for all kinds of images by the way.

42
00:01:57,747 --> 00:02:01,119
But we're going to set it up step by step together anyways,

43
00:02:01,119 --> 00:02:05,000
so you don't need to go through these instructions for now.

44
00:02:05,000 --> 00:02:07,240
Instead, what we're going to do here

45
00:02:07,240 --> 00:02:08,930
is we're going to run a container

46
00:02:08,930 --> 00:02:12,160
with Docker run based on this Mongo image.

47
00:02:12,160 --> 00:02:15,930
Now that is an image which already exists here

48
00:02:15,930 --> 00:02:16,930
on Docker Hub.

49
00:02:16,930 --> 00:02:19,940
So I'm using this official image named Mongo.

50
00:02:19,940 --> 00:02:22,140
If I run Docker run Mongo,

51
00:02:22,140 --> 00:02:24,940
this would create a new container based on

52
00:02:24,940 --> 00:02:27,100
this MongoDB image

53
00:02:27,100 --> 00:02:31,883
and this image will spin up a MongoDB database.

54
00:02:32,840 --> 00:02:34,423
So if I hit Enter here,

55
00:02:35,260 --> 00:02:37,200
you should see some output like this

56
00:02:37,200 --> 00:02:41,597
and this is now the running MongoDB database container.

57
00:02:41,597 --> 00:02:44,800
Now, this is blocking the terminal here,

58
00:02:44,800 --> 00:02:46,872
because I did not run it in detached mode.

59
00:02:46,872 --> 00:02:49,140
Hence, I'll actually quit this

60
00:02:49,140 --> 00:02:50,504
with Ctrl + C,

61
00:02:50,504 --> 00:02:52,700
and then rerun it.

62
00:02:52,700 --> 00:02:55,840
But now with Dash + D to run it in detached mode,

63
00:02:55,840 --> 00:02:59,040
and also with Dash + Dash + name to give it a name

64
00:02:59,040 --> 00:03:01,811
and I'll name it MongoDB.

65
00:03:01,811 --> 00:03:04,100
So with that this is now running,

66
00:03:04,100 --> 00:03:07,130
we can see this running MongoDB container.

67
00:03:07,130 --> 00:03:08,490
So that's now container with

68
00:03:08,490 --> 00:03:10,543
a MongoDB database inside of it.

69
00:03:11,610 --> 00:03:16,050
Now, I want to alter my node js application code such

70
00:03:16,050 --> 00:03:18,678
that we can connect to this database running in

71
00:03:18,678 --> 00:03:20,530
this upper container

72
00:03:20,530 --> 00:03:22,530
and that's now the tricky part,

73
00:03:22,530 --> 00:03:25,880
host.docker internal will no longer work,

74
00:03:25,880 --> 00:03:29,510
because that refers to my local host machine IP address,

75
00:03:29,510 --> 00:03:33,130
not to the IP address of some other container

76
00:03:33,130 --> 00:03:35,850
but instead we can do something interesting,

77
00:03:35,850 --> 00:03:38,600
we can run a number of command

78
00:03:38,600 --> 00:03:42,050
and that's the Docker container inspect command to

79
00:03:42,050 --> 00:03:46,380
inspect this MongoDB container we just started.

80
00:03:46,380 --> 00:03:49,252
Here we get a lot of information about this container,

81
00:03:49,252 --> 00:03:50,910
but the interesting bit of

82
00:03:50,910 --> 00:03:54,140
information can be found if you scroll up a bit,

83
00:03:54,140 --> 00:03:55,634
this IP address here,

84
00:03:55,634 --> 00:03:59,090
which you find under network settings in

85
00:03:59,090 --> 00:04:02,333
this object here, there this IP address.

86
00:04:03,290 --> 00:04:06,680
This is the IP address of your container

87
00:04:06,680 --> 00:04:11,680
and this IP address can be used to contact this container.

88
00:04:11,780 --> 00:04:15,110
So if I copy this IP address, which could be different

89
00:04:15,110 --> 00:04:16,180
on your system,

90
00:04:16,180 --> 00:04:18,940
so make sure you copy your IP address which you see

91
00:04:18,940 --> 00:04:23,180
in your in your terminal when you inspect this container.

92
00:04:23,180 --> 00:04:25,300
If we use this IP address here,

93
00:04:25,300 --> 00:04:28,220
instead of host.docker internal,

94
00:04:28,220 --> 00:04:32,580
then this node application should be able to connect to

95
00:04:32,580 --> 00:04:36,343
this upper container and to the MongoDB database in here.

96
00:04:37,410 --> 00:04:39,421
This port is the default port so

97
00:04:39,421 --> 00:04:41,870
this shouldn't require any changes

98
00:04:41,870 --> 00:04:44,490
and therefore we should now be able to talk to

99
00:04:44,490 --> 00:04:46,480
this our container.

100
00:04:46,480 --> 00:04:47,478
Hence we can build

101
00:04:47,478 --> 00:04:50,160
this image again the favorites node image

102
00:04:50,160 --> 00:04:52,348
because we updated the source code

103
00:04:52,348 --> 00:04:54,138
and we can then try running

104
00:04:54,138 --> 00:04:58,170
this favorites node application container based on

105
00:04:58,170 --> 00:04:59,383
this updated image.

106
00:05:00,370 --> 00:05:03,760
So if I hit Enter on this Docker run command,

107
00:05:03,760 --> 00:05:06,350
let's inspect the running containers

108
00:05:06,350 --> 00:05:09,547
and I see we got containers up and running

109
00:05:09,547 --> 00:05:14,520
the MongoDB container and my node application container.

110
00:05:14,520 --> 00:05:17,730
With that back here in postman,

111
00:05:17,730 --> 00:05:22,180
if I send a request to localhost 3000 favorites,

112
00:05:22,180 --> 00:05:25,310
talking to this dockerized node API,

113
00:05:25,310 --> 00:05:28,580
I get back a response with an empty favorites array.

114
00:05:28,580 --> 00:05:31,330
The fact that it's empty makes a lot of sense,

115
00:05:31,330 --> 00:05:35,153
because this no longer is my MongoDB database

116
00:05:35,153 --> 00:05:37,400
on my host machine

117
00:05:37,400 --> 00:05:40,656
but instead, this is a brand new MongoDB database

118
00:05:40,656 --> 00:05:42,940
in our MongoDB container

119
00:05:42,940 --> 00:05:45,660
and therefore it of course does not have access

120
00:05:45,660 --> 00:05:49,500
to the data stored in my local host MongoDB database.

121
00:05:49,500 --> 00:05:52,603
It's a totally separate, isolated database.

122
00:05:52,603 --> 00:05:57,134
After all, separation and isolation are core concepts

123
00:05:57,134 --> 00:06:00,930
and core reasons for using Docker, right.

124
00:06:00,930 --> 00:06:04,480
So we got no data back because there initially is no data

125
00:06:04,480 --> 00:06:06,513
but and that's super important.

126
00:06:06,513 --> 00:06:10,450
We can communicate here, we don't get an error

127
00:06:10,450 --> 00:06:13,371
and that shows that our approach works,

128
00:06:13,371 --> 00:06:17,798
and that the two containers are able to talk to each other.

129
00:06:17,798 --> 00:06:22,798
I can also try storing a favorite again and that works

130
00:06:22,900 --> 00:06:25,720
and if I thereafter send a GET request to favorites,

131
00:06:25,720 --> 00:06:27,520
that data is there.

132
00:06:27,520 --> 00:06:31,210
So this cross container communication works.

133
00:06:31,210 --> 00:06:34,330
But of course, it's not very convenient.

134
00:06:34,330 --> 00:06:37,190
We had to look up the IP address

135
00:06:37,190 --> 00:06:41,030
of the our container in order to then use it here.

136
00:06:41,030 --> 00:06:42,753
Definitely not a lot of work,

137
00:06:42,753 --> 00:06:44,840
but still quite cumbersome.

138
00:06:44,840 --> 00:06:46,556
It also means that we always have

139
00:06:46,556 --> 00:06:48,530
to build a new image

140
00:06:48,530 --> 00:06:52,150
whenever the MongoDB container IP address changed,

141
00:06:52,150 --> 00:06:56,393
because we hard code that IP address here ,in the node app

142
00:06:56,393 --> 00:06:58,970
and this of course, is not ideal.

143
00:06:58,970 --> 00:07:01,570
So thankfully, there is an easier way

144
00:07:01,570 --> 00:07:03,770
of having multiple Docker containers talk

145
00:07:03,770 --> 00:07:04,683
to each other.

