1
00:00:02,050 --> 00:00:03,500
For this first of all,

2
00:00:03,500 --> 00:00:07,600
with Docker network LS, we can list DOA level networks.

3
00:00:07,600 --> 00:00:11,010
And here I got this network from the last course section

4
00:00:11,010 --> 00:00:14,220
but I wanna create a new network for this application,

5
00:00:14,220 --> 00:00:16,110
for this demo here.

6
00:00:16,110 --> 00:00:19,800
So, we can do this with docker network create

7
00:00:19,800 --> 00:00:21,750
and then give it any name of our choice

8
00:00:21,750 --> 00:00:26,750
and I'll name it, goals, or goals-net for network.

9
00:00:27,150 --> 00:00:29,823
Hit Enter and this network is created.

10
00:00:31,070 --> 00:00:32,810
With the network created,

11
00:00:32,810 --> 00:00:36,000
we can now start the different containers again,

12
00:00:36,000 --> 00:00:39,530
but now put them all into that network.

13
00:00:39,530 --> 00:00:40,720
And I will start here

14
00:00:40,720 --> 00:00:44,410
by running this mongodb container again.

15
00:00:44,410 --> 00:00:47,450
Now, we no longer need to publish this port

16
00:00:47,450 --> 00:00:50,200
because if containers are in the same network

17
00:00:50,200 --> 00:00:53,850
they can communicate with each other anyways.

18
00:00:53,850 --> 00:00:56,770
My local host machine will not be able

19
00:00:56,770 --> 00:01:00,820
to talk to this container through its local host address

20
00:01:00,820 --> 00:01:02,540
if the port is not published.

21
00:01:02,540 --> 00:01:03,650
But that's no problem

22
00:01:03,650 --> 00:01:06,420
because I don't intend on doing that anyways.

23
00:01:06,420 --> 00:01:08,820
It's enough for me if the free containers

24
00:01:08,820 --> 00:01:10,053
can talk to each other.

25
00:01:10,980 --> 00:01:13,400
So, instead of publishing the port,

26
00:01:13,400 --> 00:01:16,680
I'll add --network and run this container

27
00:01:17,604 --> 00:01:20,810
in the goals net network here.

28
00:01:20,810 --> 00:01:22,070
Like this.

29
00:01:22,070 --> 00:01:26,200
So, now this is starting up and it's up and running here.

30
00:01:26,200 --> 00:01:27,940
So that's the mongodb database

31
00:01:27,940 --> 00:01:29,713
which is now part of this network.

32
00:01:30,550 --> 00:01:32,810
Let's continue with the backend.

33
00:01:32,810 --> 00:01:36,080
Here's our command for running this backend container,

34
00:01:36,080 --> 00:01:39,660
and again, we don't need to publish this port here,

35
00:01:39,660 --> 00:01:43,803
we can just add --network goals-net.

36
00:01:44,820 --> 00:01:46,340
And this will now run this container

37
00:01:46,340 --> 00:01:48,260
in this network as well.

38
00:01:48,260 --> 00:01:50,220
However, before we do that,

39
00:01:50,220 --> 00:01:52,690
let's think about that for a second.

40
00:01:52,690 --> 00:01:55,120
Is this all we need to do?

41
00:01:55,120 --> 00:01:58,510
Well, keep in mind that this backend node application

42
00:01:58,510 --> 00:02:01,380
connects to mongodb and it does that

43
00:02:01,380 --> 00:02:04,210
on host.docker internal.

44
00:02:04,210 --> 00:02:08,410
Now this will fail now because we just started mongodb

45
00:02:08,410 --> 00:02:10,289
without publishing the port,

46
00:02:10,289 --> 00:02:14,113
instead in its own network in the goals net network.

47
00:02:15,100 --> 00:02:19,440
Host docker internal refers to our local host machine though

48
00:02:19,440 --> 00:02:22,200
and there this port will no longer be available

49
00:02:22,200 --> 00:02:24,490
because we're not publishing it anymore,

50
00:02:24,490 --> 00:02:26,800
on the started mongodb container.

51
00:02:26,800 --> 00:02:29,460
Instead here, we should use mongodb,

52
00:02:29,460 --> 00:02:31,030
the name of that container,

53
00:02:31,030 --> 00:02:32,843
which is part of the same network.

54
00:02:33,830 --> 00:02:38,040
So, we need to adjust this code in our backend folder first

55
00:02:38,040 --> 00:02:40,983
and thereafter, we can run this container.

56
00:02:42,120 --> 00:02:44,960
Now that of course means that for the moment at least,

57
00:02:44,960 --> 00:02:47,300
we need to build this image again

58
00:02:47,300 --> 00:02:50,300
and give it this tag of goals-node.

59
00:02:51,890 --> 00:02:55,760
Build this image again, now with the updated source code

60
00:02:55,760 --> 00:02:58,600
where we use the mongodb container name

61
00:02:58,600 --> 00:03:00,320
and once that is done,

62
00:03:00,320 --> 00:03:03,910
only then it makes sense to now run docker run

63
00:03:03,910 --> 00:03:06,570
with that network being used.

64
00:03:06,570 --> 00:03:10,403
So, with --network goals-net.

65
00:03:11,860 --> 00:03:14,890
And now this will start this backend container attached

66
00:03:14,890 --> 00:03:17,600
to the same network as the mongodb container,

67
00:03:17,600 --> 00:03:20,840
and therefore using the name of that mongodb container

68
00:03:20,840 --> 00:03:24,463
will work and docker will be able to connect the two.

69
00:03:25,710 --> 00:03:29,110
So, for now hit Enter, this starts and with docker PS,

70
00:03:29,110 --> 00:03:32,230
we see both containers being up and running.

71
00:03:32,230 --> 00:03:35,020
And of course, make sure you're using the right name here

72
00:03:35,020 --> 00:03:36,350
in your node code.

73
00:03:36,350 --> 00:03:40,463
This really needs to be the name of your mongodb container.

74
00:03:41,540 --> 00:03:43,920
So, now we've got these two containers up and running.

75
00:03:43,920 --> 00:03:46,400
What about react now?

76
00:03:46,400 --> 00:03:48,870
Well, for react, it's quite the same.

77
00:03:48,870 --> 00:03:52,050
There we also need to adjust the code first of all.

78
00:03:52,050 --> 00:03:56,410
In the front end, source folder and there in App JS.

79
00:03:56,410 --> 00:04:00,180
And all the places where I reach out to local host,

80
00:04:00,180 --> 00:04:02,350
this now needs to be the name

81
00:04:02,350 --> 00:04:06,040
of my node application container.

82
00:04:06,040 --> 00:04:09,660
And that was goals-backend.

83
00:04:09,660 --> 00:04:11,850
This name here which I assigned

84
00:04:11,850 --> 00:04:14,463
for starting this goals backend container.

85
00:04:15,430 --> 00:04:18,899
That is what we should use here as an address,

86
00:04:18,899 --> 00:04:20,660
as a domain you could say,

87
00:04:20,660 --> 00:04:23,540
because that can be resolved by docker

88
00:04:23,540 --> 00:04:27,400
to the IP address of our node backend container,

89
00:04:27,400 --> 00:04:30,133
if we run all containers in the same network.

90
00:04:31,040 --> 00:04:32,640
So, that's what we should do here

91
00:04:32,640 --> 00:04:35,580
in App JS and the front end folder

92
00:04:35,580 --> 00:04:37,510
and therefore just as before,

93
00:04:37,510 --> 00:04:41,540
at the moment we still need to rebuild this image.

94
00:04:41,540 --> 00:04:44,800
So, with docker build-t goals react.,

95
00:04:44,800 --> 00:04:47,450
I'm going to rebuild this image.

96
00:04:47,450 --> 00:04:51,380
And again, this takes a short while here,

97
00:04:51,380 --> 00:04:54,470
especially the copy step takes pretty long

98
00:04:54,470 --> 00:04:56,420
because they still have node modules here

99
00:04:56,420 --> 00:05:00,540
and they are also getting here, they are also getting copied

100
00:05:00,540 --> 00:05:04,680
into the container again, and for the front end project,

101
00:05:04,680 --> 00:05:06,480
this is a huge folder,

102
00:05:06,480 --> 00:05:09,350
but we'll take care about optimizing this later.

103
00:05:09,350 --> 00:05:11,853
For the moment it takes a while, but it works.

104
00:05:12,690 --> 00:05:15,160
And thereafter we can run this container again.

105
00:05:15,160 --> 00:05:19,170
Now, however, by also putting it into the same network.

106
00:05:19,170 --> 00:05:22,090
Nonetheless, I still wanna publish my port here

107
00:05:22,090 --> 00:05:25,510
because I still also want to interact with this app

108
00:05:25,510 --> 00:05:27,460
from my local host machine,

109
00:05:27,460 --> 00:05:30,020
so that we can test it in the browser here.

110
00:05:30,020 --> 00:05:32,893
But I will also add --network and add this to goals net.

111
00:05:35,870 --> 00:05:38,940
So that this container is also part of the same network

112
00:05:38,940 --> 00:05:42,700
and is able to communicate with the other containers.

113
00:05:42,700 --> 00:05:45,810
So, let's hit Enter here and let's wait

114
00:05:45,810 --> 00:05:48,463
for this development server to start up,

115
00:05:49,450 --> 00:05:51,350
and then let's reload.

116
00:05:51,350 --> 00:05:53,530
And we get a problem here.

117
00:05:53,530 --> 00:05:56,330
Error name not resolved.

118
00:05:56,330 --> 00:05:58,690
Now, what is the problem here?

119
00:05:58,690 --> 00:06:00,660
Why is this not working?

120
00:06:00,660 --> 00:06:02,960
Well, this is tricky at first,

121
00:06:02,960 --> 00:06:06,450
but it makes a lot of sense if you know how react works

122
00:06:06,450 --> 00:06:09,550
and if you keep in mind and understand

123
00:06:09,550 --> 00:06:11,970
that all the front end react code here,

124
00:06:11,970 --> 00:06:14,740
this JavaScript code, is actually running

125
00:06:14,740 --> 00:06:18,040
in the browser, not on some server.

126
00:06:18,040 --> 00:06:21,270
And that's a key difference compared to this node code.

127
00:06:21,270 --> 00:06:25,520
This is executed by the node runtime on the server,

128
00:06:25,520 --> 00:06:27,253
directly in the container.

129
00:06:28,150 --> 00:06:32,560
For react, that's different, there, we just run NPM start,

130
00:06:32,560 --> 00:06:36,560
which does only one thing, it starts a development server,

131
00:06:36,560 --> 00:06:40,157
which serves this basic react application.

132
00:06:40,157 --> 00:06:42,070
The react code however,

133
00:06:42,070 --> 00:06:44,710
is not executed inside of the container.

134
00:06:44,710 --> 00:06:46,923
This always runs in the browser.

135
00:06:47,800 --> 00:06:50,860
And that means that our nice code here

136
00:06:50,860 --> 00:06:52,950
where we reach out to goals backend,

137
00:06:52,950 --> 00:06:56,360
does not run in the container where docker would be able

138
00:06:56,360 --> 00:06:59,430
to translate this, it runs in the browser

139
00:06:59,430 --> 00:07:03,250
and the browser has no idea what goals backend should be.

140
00:07:03,250 --> 00:07:05,930
And that is not a bug or anything like that.

141
00:07:05,930 --> 00:07:10,930
This is just related to what react is and where it helps us

142
00:07:11,080 --> 00:07:13,790
and that with it you build applications

143
00:07:13,790 --> 00:07:17,073
that run in the browser, not on the server.

144
00:07:18,070 --> 00:07:20,840
And therefore using the container names here,

145
00:07:20,840 --> 00:07:24,320
is simply not an option because this code here

146
00:07:24,320 --> 00:07:26,770
is not executed in a docker container,

147
00:07:26,770 --> 00:07:28,800
it's running in a browser.

148
00:07:28,800 --> 00:07:31,920
The only thing which is running in a docker container here,

149
00:07:31,920 --> 00:07:35,490
is the development server, serving this application,

150
00:07:35,490 --> 00:07:37,690
but that's not enough.

151
00:07:37,690 --> 00:07:40,030
So, what should we do here then?

152
00:07:40,030 --> 00:07:42,900
Well, we should go back to local host

153
00:07:42,900 --> 00:07:47,440
in our react application code, because this is an address

154
00:07:47,440 --> 00:07:49,403
it will be able to communicate to,

155
00:07:51,350 --> 00:07:52,820
our local machine.

156
00:07:52,820 --> 00:07:56,020
This is a address the browser will understand.

157
00:07:56,020 --> 00:07:57,570
And it's all about the browser here,

158
00:07:57,570 --> 00:08:00,830
because again, this code is executed by the browser.

159
00:08:00,830 --> 00:08:05,260
So, local host is an identifier the browser understands.

160
00:08:05,260 --> 00:08:08,530
And with that changed back, we need to ensure

161
00:08:08,530 --> 00:08:12,683
that on local host, these end points can be reached.

162
00:08:13,520 --> 00:08:18,510
And that simply means, that we still need to publish port 80

163
00:08:18,510 --> 00:08:21,840
on the backend application, so that that application

164
00:08:21,840 --> 00:08:24,480
is also still available on local host,

165
00:08:24,480 --> 00:08:28,360
because our front end application needs that access.

166
00:08:28,360 --> 00:08:30,530
Because of the way react works,

167
00:08:30,530 --> 00:08:33,580
and because of the fact that react applications

168
00:08:33,580 --> 00:08:37,809
have browser site JavaScript code, and not JavaScript code

169
00:08:37,809 --> 00:08:40,950
that runs inside of the docker container.

170
00:08:40,950 --> 00:08:42,409
If that would be different,

171
00:08:42,409 --> 00:08:44,840
if we had a second node application

172
00:08:44,840 --> 00:08:46,850
that needs to talk to the first one,

173
00:08:46,850 --> 00:08:48,870
then we could use the container name.

174
00:08:48,870 --> 00:08:52,080
But since this is a JavaScript code running in the browser,

175
00:08:52,080 --> 00:08:55,563
we need code which the browser understands, not docker.

176
00:08:56,720 --> 00:08:59,380
So, long story short, I changed this code

177
00:08:59,380 --> 00:09:02,841
and we can stop this container with Control + C.

178
00:09:02,841 --> 00:09:05,210
We can then rebuild this image,

179
00:09:05,210 --> 00:09:08,350
basically rebuilding the same image we had before,

180
00:09:08,350 --> 00:09:12,560
but I wanted to show you this important case here.

181
00:09:12,560 --> 00:09:16,160
And once this is rebuilt, we can restart this container

182
00:09:16,160 --> 00:09:19,560
and we don't need the --network option here

183
00:09:19,560 --> 00:09:21,560
because this is an option

184
00:09:21,560 --> 00:09:23,760
which doesn't make sense here anyways,

185
00:09:23,760 --> 00:09:26,630
because the part which runs in the container,

186
00:09:26,630 --> 00:09:29,810
the development server, doesn't care about the network,

187
00:09:29,810 --> 00:09:34,040
it doesn't interact with the node API or the database.

188
00:09:34,040 --> 00:09:37,320
And the part that would interact with the API

189
00:09:37,320 --> 00:09:40,540
is not executed in the docker environment.

190
00:09:40,540 --> 00:09:42,800
So, there's no need to add the network here,

191
00:09:42,800 --> 00:09:46,373
instead we run this front end container as we did before.

192
00:09:47,300 --> 00:09:50,130
But now we also need to go back to the terminal

193
00:09:50,130 --> 00:09:52,253
where we started the backend container.

194
00:09:53,160 --> 00:09:58,160
Here, we should stop this goals-backend container.

195
00:09:58,980 --> 00:10:02,360
Make sure you stop this goals backend node container,

196
00:10:02,360 --> 00:10:06,050
and then we should restart it, but we should ensure

197
00:10:06,050 --> 00:10:08,463
that we still publish port 80.

198
00:10:09,430 --> 00:10:12,320
So, we run our docker run command again

199
00:10:12,320 --> 00:10:14,120
for the goals backend.

200
00:10:14,120 --> 00:10:16,330
And we still need the network here,

201
00:10:16,330 --> 00:10:20,830
because the node API still also talks to mongodb.

202
00:10:20,830 --> 00:10:23,130
And we wanna use a network here,

203
00:10:23,130 --> 00:10:27,790
so that we can use the mongodb name here in our node code.

204
00:10:27,790 --> 00:10:31,360
And here it works, because this code executes directly

205
00:10:31,360 --> 00:10:32,710
in the docker container.

206
00:10:32,710 --> 00:10:35,650
So, here docker is able to help us.

207
00:10:35,650 --> 00:10:40,650
But I also still want to publish port 80 on port 80

208
00:10:42,440 --> 00:10:44,340
on the local host machine,

209
00:10:44,340 --> 00:10:46,990
so that our separate react application

210
00:10:46,990 --> 00:10:49,160
is able to talk to that.

211
00:10:49,160 --> 00:10:51,090
So, let's run this.

212
00:10:51,090 --> 00:10:54,420
Let's verify that all containers are up and running

213
00:10:54,420 --> 00:10:57,510
and then let's reload the react app.

214
00:10:57,510 --> 00:11:02,480
And now it works and I can learn docker, reload

215
00:11:02,480 --> 00:11:04,050
and everything is here.

216
00:11:04,050 --> 00:11:07,900
So, we got these free containers up and running

217
00:11:07,900 --> 00:11:10,560
and we got them working with each other.

218
00:11:10,560 --> 00:11:13,550
And especially for react it was a bit tricky,

219
00:11:13,550 --> 00:11:16,310
because the code doesn't run in the container,

220
00:11:16,310 --> 00:11:18,260
but it runs in the browser.

221
00:11:18,260 --> 00:11:20,910
And then docker is not able to help us

222
00:11:20,910 --> 00:11:24,400
with this automatic host IP resolving.

223
00:11:24,400 --> 00:11:26,570
I hope I could explain why that is

224
00:11:26,570 --> 00:11:28,840
and how we can mitigate it.

225
00:11:28,840 --> 00:11:32,760
But we're not done at this point, we got our containers

226
00:11:32,760 --> 00:11:35,230
and we got them talking to each other,

227
00:11:35,230 --> 00:11:38,390
but we got these other things which we wanna add.

228
00:11:38,390 --> 00:11:40,930
Data persistence, limited access

229
00:11:40,930 --> 00:11:43,990
and life source code updates, which are reflected

230
00:11:43,990 --> 00:11:45,380
in the running containers.

231
00:11:45,380 --> 00:11:48,233
So therefore, that is what we'll focus on next.

