1
00:00:02,210 --> 00:00:06,240
With Docker, you can create so-called container networks,

2
00:00:06,240 --> 00:00:08,740
also called just networks.

3
00:00:08,740 --> 00:00:11,050
So, what are networks then?

4
00:00:11,050 --> 00:00:14,200
Well, the idea is that you might have multiple containers

5
00:00:14,200 --> 00:00:16,530
and you want to allow communication

6
00:00:16,530 --> 00:00:17,980
between these containers.

7
00:00:17,980 --> 00:00:20,010
So, essentially the scenario we have

8
00:00:20,010 --> 00:00:24,400
with the node web API container and the MongoDB container.

9
00:00:24,400 --> 00:00:27,560
Now, with Docker, you can put all these containers

10
00:00:27,560 --> 00:00:29,540
into one and the same network

11
00:00:29,540 --> 00:00:34,203
by adding the --network option on the Docker run command.

12
00:00:35,130 --> 00:00:38,660
This then creates a network in which all containers

13
00:00:38,660 --> 00:00:40,830
are able to talk to Java,

14
00:00:40,830 --> 00:00:43,740
and Docker is then automatically doing this

15
00:00:43,740 --> 00:00:46,780
IP look up and resolving stuff,

16
00:00:46,780 --> 00:00:50,170
which we did manually a couple of minutes ago.

17
00:00:50,170 --> 00:00:52,540
And that's a really useful feature

18
00:00:52,540 --> 00:00:55,630
for having multiple, isolated containers

19
00:00:55,630 --> 00:00:57,770
with their own duties and tasks,

20
00:00:57,770 --> 00:01:00,600
which still are able to talk to Java,

21
00:01:00,600 --> 00:01:04,250
which is exactly what we want here in our example.

22
00:01:04,250 --> 00:01:07,780
So, let's see how we could utilize networks here.

23
00:01:07,780 --> 00:01:11,190
And for that, I'm going to stop the favorites container

24
00:01:11,190 --> 00:01:15,430
and I'm then also going to stop the MongoDB container,

25
00:01:15,430 --> 00:01:19,200
because I wanna restart both, but in a different way.

26
00:01:19,200 --> 00:01:22,753
So, let's stop favorites and let's stop MongoDB,

27
00:01:23,610 --> 00:01:27,810
and let's then explore how we can use networks here.

28
00:01:27,810 --> 00:01:30,720
Let's start with MongoDB.

29
00:01:30,720 --> 00:01:35,040
We started the MongoDB container with this command here,

30
00:01:35,040 --> 00:01:40,040
Docker run -d --name mongodb and then using the Mongo image.

31
00:01:41,040 --> 00:01:43,180
Now, we can add another option here

32
00:01:43,180 --> 00:01:46,700
and that's the --network option,

33
00:01:46,700 --> 00:01:49,640
and here we can then pick any name of our choice.

34
00:01:49,640 --> 00:01:53,030
Let's say, favorites net.

35
00:01:53,030 --> 00:01:56,420
So, favorite-net is my network name.

36
00:01:56,420 --> 00:01:59,180
Now, if I hit enter, this fails to run

37
00:01:59,180 --> 00:02:02,300
because I just stopped the MongoDB container,

38
00:02:02,300 --> 00:02:03,910
I did not remove it,

39
00:02:03,910 --> 00:02:08,910
so, therefore I will actually run Docker container prune

40
00:02:09,419 --> 00:02:12,150
to remove all stopped containers,

41
00:02:12,150 --> 00:02:15,350
so that this MongoDB container is also removed

42
00:02:15,350 --> 00:02:16,320
and thereafter,

43
00:02:16,320 --> 00:02:20,050
we would be able to run a container named MongoDB again,

44
00:02:20,050 --> 00:02:22,450
but still, if I try to run this

45
00:02:22,450 --> 00:02:26,430
with this --network option, if I hit enter,

46
00:02:26,430 --> 00:02:30,823
I get an error that the network favorites net is not found.

47
00:02:31,660 --> 00:02:34,740
Unlike with volumes, for networks,

48
00:02:34,740 --> 00:02:38,300
Docker will not automatically create them for you

49
00:02:38,300 --> 00:02:41,310
if you want to run a container using a network,

50
00:02:41,310 --> 00:02:44,110
instead you have to create them on your own.

51
00:02:44,110 --> 00:02:45,350
So, first of all,

52
00:02:45,350 --> 00:02:50,350
I'm going to remove this newly started MongoDB container,

53
00:02:50,630 --> 00:02:54,190
which then was stopped because we failed to

54
00:02:54,190 --> 00:02:56,010
find that network,

55
00:02:56,010 --> 00:02:59,030
but once that is done, we can create a network

56
00:02:59,030 --> 00:03:01,543
with the Docker network command.

57
00:03:02,458 --> 00:03:05,450
With --help you again see all options,

58
00:03:05,450 --> 00:03:08,030
and the most important option here at the moment

59
00:03:08,030 --> 00:03:09,633
is to create option.

60
00:03:10,540 --> 00:03:15,540
With Docker network create, you create a new Docker network,

61
00:03:17,310 --> 00:03:20,970
and then you don't need any sophisticated configuration,

62
00:03:20,970 --> 00:03:24,213
you just need to pick a name, for example, favorites-net.

63
00:03:25,280 --> 00:03:27,340
That's your custom network name,

64
00:03:27,340 --> 00:03:29,550
and you can simply hit enter thereafter,

65
00:03:29,550 --> 00:03:33,480
and this network gets created and set up by Docker.

66
00:03:33,480 --> 00:03:35,560
It's a Docker internal network,

67
00:03:35,560 --> 00:03:37,680
which you can then use on Docker containers

68
00:03:37,680 --> 00:03:39,910
to let them talk to Java.

69
00:03:39,910 --> 00:03:42,040
All the hard work and heavy lifting

70
00:03:42,040 --> 00:03:44,273
is taken care of by Docker here.

71
00:03:45,350 --> 00:03:47,310
Now, with Docker network LS,

72
00:03:47,310 --> 00:03:50,000
you can inspect all existing networks

73
00:03:50,000 --> 00:03:52,350
and you see your own network here,

74
00:03:52,350 --> 00:03:54,910
as well as a couple of built in default networks,

75
00:03:54,910 --> 00:03:56,770
which you can ignore here,

76
00:03:56,770 --> 00:04:00,120
and with that created, with that network existing,

77
00:04:00,120 --> 00:04:03,800
you can try this Docker run MongoDB command again

78
00:04:03,800 --> 00:04:06,670
with this --network flag here

79
00:04:06,670 --> 00:04:11,200
using that network, favorites net, which we just set up,

80
00:04:11,200 --> 00:04:14,150
and if you now hit enter, this container starts

81
00:04:14,150 --> 00:04:15,770
and it stays up and running,

82
00:04:15,770 --> 00:04:18,793
but it's now part of this favorites net network.

83
00:04:20,160 --> 00:04:22,200
And now any other container

84
00:04:22,200 --> 00:04:24,400
can also be part of that network,

85
00:04:24,400 --> 00:04:28,490
and if two containers or possibly more containers also

86
00:04:28,490 --> 00:04:32,300
are part of the same network, they can talk to Java.

87
00:04:32,300 --> 00:04:34,070
The only remaining question

88
00:04:34,070 --> 00:04:37,070
is how they can talk to Java.

89
00:04:37,070 --> 00:04:39,770
How should this note JS code look like

90
00:04:39,770 --> 00:04:42,363
to ensure that this request works?

91
00:04:43,250 --> 00:04:44,860
This IP address here

92
00:04:44,860 --> 00:04:49,350
might be the IP address of the other MongoDB container,

93
00:04:49,350 --> 00:04:53,120
but I mentioned that we don't want to hard code it in here.

94
00:04:53,120 --> 00:04:57,270
MongoDB should automatically resolve it instead,

95
00:04:57,270 --> 00:04:58,660
and the cool thing is

96
00:04:58,660 --> 00:05:01,890
if two containers are part of the same network,

97
00:05:01,890 --> 00:05:04,870
you can just put the other containers name in here.

98
00:05:04,870 --> 00:05:07,150
So, in my case, MongoDB,

99
00:05:07,150 --> 00:05:11,400
because my other container is named MongoDB here.

100
00:05:11,400 --> 00:05:14,830
I picked this name when I started the MongoDB container,

101
00:05:14,830 --> 00:05:16,970
so, you can then use the name here.

102
00:05:16,970 --> 00:05:19,950
If you picked a different name, use that different name,

103
00:05:19,950 --> 00:05:23,000
if you didn't pick a name, use the auto-generated name

104
00:05:23,000 --> 00:05:26,600
and plug it in here as a domain.

105
00:05:26,600 --> 00:05:30,800
And just as with host.docker internal,

106
00:05:30,800 --> 00:05:34,530
which was translated to your local host IP address

107
00:05:34,530 --> 00:05:37,620
as seen from inside this Docker container,

108
00:05:37,620 --> 00:05:39,850
the other Docker container's name

109
00:05:39,850 --> 00:05:42,773
will also be translated by Docker

110
00:05:42,773 --> 00:05:46,290
to the IP address of that container,

111
00:05:46,290 --> 00:05:49,160
and this automatic transformation will work

112
00:05:49,160 --> 00:05:52,573
if both containers are part of the same network.

113
00:05:53,580 --> 00:05:56,360
So, save this updated code

114
00:05:56,360 --> 00:06:00,610
and then build the image again because the code was updated

115
00:06:00,610 --> 00:06:03,670
and thereafter run the note application again

116
00:06:03,670 --> 00:06:06,250
with Docker run --name

117
00:06:06,250 --> 00:06:09,400
favorites -d --rm,

118
00:06:09,400 --> 00:06:10,840
and then exposing the port

119
00:06:10,840 --> 00:06:14,160
and now very important, when our a flag,

120
00:06:14,160 --> 00:06:16,230
a flag to put this container

121
00:06:16,230 --> 00:06:20,430
into the same network as our MongoDB container is part of.

122
00:06:20,430 --> 00:06:23,720
So, at --network here as well

123
00:06:23,720 --> 00:06:26,480
and then it was the favorites net.

124
00:06:26,480 --> 00:06:30,410
So, this same network we used for the MongoDB container,

125
00:06:30,410 --> 00:06:33,920
both containers have to be part of the same network

126
00:06:33,920 --> 00:06:37,860
in order to talk to Java with this simplification

127
00:06:37,860 --> 00:06:40,980
of just using the other container's name.

128
00:06:40,980 --> 00:06:43,310
If you now hit enter this, it starts

129
00:06:43,310 --> 00:06:45,030
and it stays up and running,

130
00:06:45,030 --> 00:06:48,450
which is always a good sign, proves that it works.

131
00:06:48,450 --> 00:06:51,130
If you inspect the logs

132
00:06:51,130 --> 00:06:53,120
of the favorites

133
00:06:54,130 --> 00:06:55,140
container,

134
00:06:55,140 --> 00:06:56,810
you should see something like this,

135
00:06:56,810 --> 00:06:59,310
which is actually a warning,

136
00:06:59,310 --> 00:07:01,280
which actually proves that it works

137
00:07:01,280 --> 00:07:04,980
because it's just this warning and no connection error.

138
00:07:04,980 --> 00:07:08,260
So, indeed the node application was able to connect

139
00:07:08,260 --> 00:07:11,900
to the MongoDB database running in another container,

140
00:07:11,900 --> 00:07:14,903
and we still see both containers up and running here.

141
00:07:16,100 --> 00:07:20,120
Now with that, if you again tried it with Postman,

142
00:07:20,120 --> 00:07:22,070
I can again, get my favorites

143
00:07:22,070 --> 00:07:26,030
and we see an empty response, an empty aray of favorites,

144
00:07:26,030 --> 00:07:30,790
but a success response, no error and that is what matters,

145
00:07:30,790 --> 00:07:33,850
and we can send a post request to slash favorites

146
00:07:33,850 --> 00:07:35,780
and the data does get stored

147
00:07:35,780 --> 00:07:38,660
until after we can also get it here.

148
00:07:38,660 --> 00:07:42,080
And this now works and proves that both containers

149
00:07:42,080 --> 00:07:44,410
again, are able to communicate,

150
00:07:44,410 --> 00:07:48,030
this time, however, with this simplification

151
00:07:48,030 --> 00:07:50,980
of using the built in network feature,

152
00:07:50,980 --> 00:07:52,600
and that's important.

153
00:07:52,600 --> 00:07:56,760
Two containers normally are not able to talk to Java,

154
00:07:56,760 --> 00:07:59,900
unless you create such a container network

155
00:07:59,900 --> 00:08:04,170
or you manually look up the container IP as you saw,

156
00:08:04,170 --> 00:08:06,390
but with such a container network,

157
00:08:06,390 --> 00:08:08,400
as you see here, it works

158
00:08:08,400 --> 00:08:11,300
and this is therefore how you typically ensure

159
00:08:11,300 --> 00:08:14,610
that multiple containers can talk to Java

160
00:08:14,610 --> 00:08:17,020
because this is a common requirement,

161
00:08:17,020 --> 00:08:20,570
it's not a niche scenario, indeed it is very common

162
00:08:20,570 --> 00:08:23,900
that two or more containers need to talk to Java

163
00:08:23,900 --> 00:08:25,500
and with networks

164
00:08:25,500 --> 00:08:28,600
set up the way you saw it here over the last lectures,

165
00:08:28,600 --> 00:08:29,950
this is a breeze,

166
00:08:29,950 --> 00:08:32,820
and by using the container names here in your code,

167
00:08:32,820 --> 00:08:36,380
it's then especially simple to do as you saw.

168
00:08:36,380 --> 00:08:39,330
And I also wanna point out something else.

169
00:08:39,330 --> 00:08:43,210
When we launch a container like the MongoDB container,

170
00:08:43,210 --> 00:08:45,930
which another container connects to,

171
00:08:45,930 --> 00:08:48,800
in our case, the favorites container

172
00:08:48,800 --> 00:08:51,490
will connect to the MongoDB container,

173
00:08:51,490 --> 00:08:55,690
then as you saw, you don't need to publish any port

174
00:08:55,690 --> 00:08:59,390
when running that to be connected to container.

175
00:08:59,390 --> 00:09:01,920
When I run the MongoDB container,

176
00:09:01,920 --> 00:09:05,200
I don't have the -p option here.

177
00:09:05,200 --> 00:09:09,830
The reason for that is that the -p option is only required

178
00:09:09,830 --> 00:09:14,410
if we plan on connecting to something in that container

179
00:09:14,410 --> 00:09:16,610
from our local host machine

180
00:09:16,610 --> 00:09:20,060
or from outside the container network.

181
00:09:20,060 --> 00:09:22,230
Now, in this case, we don't do that.

182
00:09:22,230 --> 00:09:26,020
The only thing which connects to this MongoDB container

183
00:09:26,020 --> 00:09:28,150
will be our favorites container,

184
00:09:28,150 --> 00:09:31,390
which will be part of the same Docker network,

185
00:09:31,390 --> 00:09:33,140
and in such cases,

186
00:09:33,140 --> 00:09:36,040
when you have a container to container connection,

187
00:09:36,040 --> 00:09:38,440
then you don't need to publish the port

188
00:09:38,440 --> 00:09:42,330
because internally in that container network,

189
00:09:42,330 --> 00:09:45,940
all the containers can freely communicate with each other

190
00:09:45,940 --> 00:09:49,200
and you don't need to expose any ports.

191
00:09:49,200 --> 00:09:52,260
That's really only needed if you wanna connect

192
00:09:52,260 --> 00:09:55,240
to a container from your local host machine

193
00:09:55,240 --> 00:09:58,683
or from outside that container in general.

