1
00:00:02,130 --> 00:00:03,630
So let's now configure

2
00:00:03,630 --> 00:00:07,510
our individual services and therefore containers.

3
00:00:07,510 --> 00:00:08,903
And let's start with Mongo.

4
00:00:10,060 --> 00:00:12,320
For this I'll have a look at the command we used

5
00:00:12,320 --> 00:00:15,320
for launching our MongoDB container.

6
00:00:15,320 --> 00:00:19,570
In the end we used the Mongo image here,

7
00:00:19,570 --> 00:00:23,750
the official Mongo image which exists on Docker hub.

8
00:00:23,750 --> 00:00:25,550
We don't use our own image,

9
00:00:25,550 --> 00:00:28,190
we don't use our own Docker file here,

10
00:00:28,190 --> 00:00:29,780
so we use this image.

11
00:00:29,780 --> 00:00:32,549
We then also attach it to a network

12
00:00:32,549 --> 00:00:37,443
we run it in detach mode and we remove it if it's shut down.

13
00:00:38,290 --> 00:00:40,960
We have this volume, this named volume

14
00:00:40,960 --> 00:00:43,310
for persisting the database data,

15
00:00:43,310 --> 00:00:45,943
and we pass into environment variables.

16
00:00:46,890 --> 00:00:50,460
So how can we translate this to docker-compose.yaml now.

17
00:00:50,460 --> 00:00:52,050
Well for MongoDB,

18
00:00:52,050 --> 00:00:54,950
we again start by pointing at the image we wanna use.

19
00:00:54,950 --> 00:00:58,280
And we do this by using an image key here,

20
00:00:58,280 --> 00:01:01,170
below the container to which it belongs.

21
00:01:01,170 --> 00:01:05,410
So indented by two blanks below MongoDB.

22
00:01:05,410 --> 00:01:10,410
And here we then specify the image as a string 'mongo'.

23
00:01:12,050 --> 00:01:14,550
So between single or double quotes here,

24
00:01:14,550 --> 00:01:18,610
you specify 'mongo' as a image name

25
00:01:18,610 --> 00:01:21,200
because that is the image name we wanna use.

26
00:01:21,200 --> 00:01:24,350
It's the same name we use here for a Docker Run.

27
00:01:24,350 --> 00:01:26,440
And this will tell Docker Compose

28
00:01:26,440 --> 00:01:28,110
and therefore Docker in the end

29
00:01:28,110 --> 00:01:29,640
that the MongoDB container

30
00:01:29,640 --> 00:01:32,590
should be based on this Mongo image.

31
00:01:32,590 --> 00:01:35,080
And this can be just the image name,

32
00:01:35,080 --> 00:01:37,320
which will then be looked up locally

33
00:01:37,320 --> 00:01:39,750
and in the Docker hub repository,

34
00:01:39,750 --> 00:01:44,510
it could be a full URL to another repository, for example,

35
00:01:44,510 --> 00:01:45,824
where this image might be stored,

36
00:01:45,824 --> 00:01:50,060
it could be your own repository, like academind,

37
00:01:50,060 --> 00:01:53,110
hello-world, which we created early in this course.

38
00:01:53,110 --> 00:01:54,690
So this is all valid.

39
00:01:54,690 --> 00:01:57,060
It can of course all the be accustomed image

40
00:01:57,060 --> 00:01:59,030
which you built on your system,

41
00:01:59,030 --> 00:02:00,450
which you now reference.

42
00:02:00,450 --> 00:02:01,283
For example,

43
00:02:01,283 --> 00:02:05,950
goals-node is an image we built in the last course section.

44
00:02:05,950 --> 00:02:08,340
Of course, the wrong image for MongoDB

45
00:02:08,340 --> 00:02:11,430
it's just an example, what would be allowed.

46
00:02:11,430 --> 00:02:14,860
So here it's 'mongo', that's the image we wanna use.

47
00:02:14,860 --> 00:02:17,090
And now let's see what else we would add.

48
00:02:17,090 --> 00:02:19,810
We wanna run this in detached mode

49
00:02:19,810 --> 00:02:21,890
and remove it when it shut down.

50
00:02:21,890 --> 00:02:25,200
Well great news, you don't need to specify this here

51
00:02:25,200 --> 00:02:27,900
by default when you use Docker Compose,

52
00:02:27,900 --> 00:02:29,910
when you bring your services down,

53
00:02:29,910 --> 00:02:33,840
they will be removed and regarding the detached mode,

54
00:02:33,840 --> 00:02:36,500
that's also something we can specify

55
00:02:36,500 --> 00:02:39,090
when we then start all services together.

56
00:02:39,090 --> 00:02:43,800
So we don't need to add the remove or detach flag here

57
00:02:43,800 --> 00:02:47,720
to our MongoDB container configuration here.

58
00:02:47,720 --> 00:02:51,170
We definitely do wanna add our volume though.

59
00:02:51,170 --> 00:02:53,380
I'll come back to the network in a second by the way,

60
00:02:53,380 --> 00:02:55,020
but I'll start with the volume

61
00:02:55,020 --> 00:02:57,270
and we can add that to a container

62
00:02:57,270 --> 00:02:59,943
by adding the volumes' key here.

63
00:03:00,930 --> 00:03:04,300
So next to image nested below MongoDB

64
00:03:04,300 --> 00:03:07,770
on the same level as image I add volumes.

65
00:03:07,770 --> 00:03:08,660
And now again,

66
00:03:08,660 --> 00:03:12,150
we can have more nested values for our volumes

67
00:03:12,150 --> 00:03:14,660
because we can have more than one volume.

68
00:03:14,660 --> 00:03:18,420
And here the syntax is really, really easy.

69
00:03:18,420 --> 00:03:22,550
For every volume you wanna have here, you add a dash

70
00:03:22,550 --> 00:03:25,780
so that you have a list of dashes here eventually

71
00:03:25,780 --> 00:03:27,750
if you have multiple volumes.

72
00:03:27,750 --> 00:03:29,500
But here we only need one

73
00:03:29,500 --> 00:03:33,240
we'll see multiple volumes later for other containers.

74
00:03:33,240 --> 00:03:35,300
And then after this dash

75
00:03:35,300 --> 00:03:38,260
you define a volume as you learned it.

76
00:03:38,260 --> 00:03:41,120
So for a named volume named data,

77
00:03:41,120 --> 00:03:44,910
which is the volume name we used here for the database data,

78
00:03:44,910 --> 00:03:49,910
you add data:data/db for the container internal path.

79
00:03:53,490 --> 00:03:55,657
So this syntax is exactly the same

80
00:03:55,657 --> 00:03:59,220
as we used it here with dash V flag,

81
00:03:59,220 --> 00:04:02,530
we add our volume name, then a colon,

82
00:04:02,530 --> 00:04:05,094
and then the container internal path.

83
00:04:05,094 --> 00:04:06,930
And with the number of colon,

84
00:04:06,930 --> 00:04:10,000
we could always add extra options, like read only,

85
00:04:10,000 --> 00:04:11,807
which of course doesn't make sense here,

86
00:04:11,807 --> 00:04:13,243
but we could add this.

87
00:04:14,590 --> 00:04:18,149
So with that, we added all the volumes, the single volume,

88
00:04:18,149 --> 00:04:20,149
the MongoDB container needs.

89
00:04:20,149 --> 00:04:23,000
And now we are left with the two environment variables

90
00:04:23,000 --> 00:04:24,400
and the network.

91
00:04:24,400 --> 00:04:26,710
Now let's start with the environment variables,

92
00:04:26,710 --> 00:04:30,660
here we can add an environment option

93
00:04:30,660 --> 00:04:33,090
on the same level as image and volumes

94
00:04:33,090 --> 00:04:35,490
because it configures the same container

95
00:04:35,490 --> 00:04:38,160
not nested in volumes of course.

96
00:04:38,160 --> 00:04:41,120
And then here we got two alternative syntaxes

97
00:04:41,120 --> 00:04:43,980
for specifying our environment variables.

98
00:04:43,980 --> 00:04:46,480
We can specify the environment variable

99
00:04:46,480 --> 00:04:51,440
like MONGO_INITDB_ROOT_USERNAME, then a colon

100
00:04:51,440 --> 00:04:55,073
and then the value for this variable, for example max,

101
00:04:56,140 --> 00:05:00,160
or if we don't wanna do that, if I comment this out,

102
00:05:00,160 --> 00:05:02,380
which you can do by adding a hash in front of it,

103
00:05:02,380 --> 00:05:04,130
then this will have no effect.

104
00:05:04,130 --> 00:05:05,883
If we don't wanna do this,

105
00:05:06,720 --> 00:05:08,780
then we can also add a dash here

106
00:05:08,780 --> 00:05:13,780
and specify the environment variable as a key value player

107
00:05:13,900 --> 00:05:18,360
with an equal sign in between, like this.

108
00:05:18,360 --> 00:05:21,690
Both is possible, it's up to you, what you prefer.

109
00:05:21,690 --> 00:05:24,070
I prefer this first Syntax,

110
00:05:24,070 --> 00:05:25,540
therefore I will use that

111
00:05:25,540 --> 00:05:29,490
and I'll do the same for the root password.

112
00:05:29,490 --> 00:05:30,323
And with that,

113
00:05:30,323 --> 00:05:32,380
we're specifying the environment variables

114
00:05:32,380 --> 00:05:34,393
for this MongoDB container.

115
00:05:35,360 --> 00:05:37,200
You also have another option,

116
00:05:37,200 --> 00:05:41,310
you also can specify an environment variable file.

117
00:05:41,310 --> 00:05:44,450
And I also wanna show this to you as a side note,

118
00:05:44,450 --> 00:05:47,040
we also can do this with Docker Run of course

119
00:05:47,040 --> 00:05:49,490
I showed this earlier in the course already,

120
00:05:49,490 --> 00:05:53,900
you can add the --env-file option here,

121
00:05:53,900 --> 00:05:55,900
and then point at a file that contains

122
00:05:55,900 --> 00:05:57,650
the environment variables.

123
00:05:57,650 --> 00:06:01,160
But the equivalent for that here for Docker Compose

124
00:06:01,160 --> 00:06:04,880
would be that you for example, at a sub folder env

125
00:06:04,880 --> 00:06:09,590
and then in there let's say we have the mongo.env file.

126
00:06:12,020 --> 00:06:15,620
And in here we could then grab this,

127
00:06:15,620 --> 00:06:20,593
the environment variables and add them, however,

128
00:06:22,670 --> 00:06:27,500
like this with an equal sign and removing all wide space

129
00:06:27,500 --> 00:06:30,020
that might be in front of the environment variables.

130
00:06:30,020 --> 00:06:32,148
And then we can read in this file

131
00:06:32,148 --> 00:06:35,200
by not using this environment key here,

132
00:06:35,200 --> 00:06:36,930
which absolutely is an option.

133
00:06:36,930 --> 00:06:39,580
But in case you wanna move your environment variables

134
00:06:39,580 --> 00:06:41,290
out of this conflict file

135
00:06:41,290 --> 00:06:43,280
so that you have them in a separate file,

136
00:06:43,280 --> 00:06:46,500
which you then may be exclude from get and so on

137
00:06:46,500 --> 00:06:48,450
to not share it in a repository,

138
00:06:48,450 --> 00:06:50,700
that's why you might wanna use a file.

139
00:06:50,700 --> 00:06:54,000
Then you can add the env_file option here

140
00:06:54,000 --> 00:06:56,400
and then add all the environment files

141
00:06:56,400 --> 00:07:00,100
that should be used for this container in a list.

142
00:07:00,100 --> 00:07:00,933
So again,

143
00:07:00,933 --> 00:07:04,440
with a dash here and then a relative path

144
00:07:04,440 --> 00:07:07,650
seen from your docker-compose-yaml file on

145
00:07:07,650 --> 00:07:10,170
to the environment variable file you wanna use.

146
00:07:10,170 --> 00:07:13,280
In this case, this would be created with dot slash,

147
00:07:13,280 --> 00:07:16,930
which means start in the same directory as I'm currently in

148
00:07:16,930 --> 00:07:18,570
so the directory,

149
00:07:18,570 --> 00:07:20,720
the docker-compose-yaml file is in

150
00:07:20,720 --> 00:07:24,910
and then going in the env folder and then the mongo.env

151
00:07:24,910 --> 00:07:27,330
is the file we wanna use.

152
00:07:27,330 --> 00:07:30,370
And then the environment variables stored in this file

153
00:07:30,370 --> 00:07:34,343
would be read in and would be considered in this container.

154
00:07:35,360 --> 00:07:37,950
So that's how you can add environment variables.

155
00:07:37,950 --> 00:07:40,910
And in case it's not clear why we need the dash here

156
00:07:40,910 --> 00:07:43,690
and why we don't need it the environment variables

157
00:07:43,690 --> 00:07:47,010
if we specify them like this, you need a dash,

158
00:07:47,010 --> 00:07:49,750
whatever you have a single value here.

159
00:07:49,750 --> 00:07:52,350
So whenever you have a list of single values,

160
00:07:52,350 --> 00:07:56,600
if you have key value peers, so if you have a colon here,

161
00:07:56,600 --> 00:07:59,290
then this creates a yaml object

162
00:07:59,290 --> 00:08:01,490
and or you don't need dashes.

163
00:08:01,490 --> 00:08:03,100
So for a single values

164
00:08:03,100 --> 00:08:05,620
where you don't have a colon and a wide space,

165
00:08:05,620 --> 00:08:06,830
you need a dash.

166
00:08:06,830 --> 00:08:10,290
If you have key value pairs with name,

167
00:08:10,290 --> 00:08:15,160
colon wide space value, then you don't need dashes.

168
00:08:15,160 --> 00:08:16,683
That's just another side note.

169
00:08:18,180 --> 00:08:21,560
And with that, I talked a lot about these different options,

170
00:08:21,560 --> 00:08:23,830
but it is all brand new, I guess.

171
00:08:23,830 --> 00:08:28,160
But we're now done with MongoDB with one exception

172
00:08:28,160 --> 00:08:30,170
and that's the network.

173
00:08:30,170 --> 00:08:35,169
Now you can add a network by adding the networks' key here,

174
00:08:36,200 --> 00:08:38,480
and then you can specify all the networks

175
00:08:38,480 --> 00:08:40,470
this container should belong to.

176
00:08:40,470 --> 00:08:44,120
But in many cases, you don't need to do this.

177
00:08:44,120 --> 00:08:45,750
You don't need to do this

178
00:08:45,750 --> 00:08:48,269
because when you use Docker Compose,

179
00:08:48,269 --> 00:08:52,700
Docker will automatically create a new environment

180
00:08:52,700 --> 00:08:57,220
for all the services specified in this compose file

181
00:08:57,220 --> 00:09:00,340
and it will add all the services to that network

182
00:09:00,340 --> 00:09:01,890
out of the box.

183
00:09:01,890 --> 00:09:05,580
So all these services which are defined in one

184
00:09:05,580 --> 00:09:09,180
and the same compose file will already be part

185
00:09:09,180 --> 00:09:13,160
of one and the same network that was created for them

186
00:09:13,160 --> 00:09:14,550
by Docker.

187
00:09:14,550 --> 00:09:16,320
So that's a little convenience feature,

188
00:09:16,320 --> 00:09:19,490
which we get out of the box and which is why here

189
00:09:19,490 --> 00:09:22,460
we don't need to specify a network.

190
00:09:22,460 --> 00:09:24,460
You could, of course do it though.

191
00:09:24,460 --> 00:09:28,100
You could add a network's key, for example,

192
00:09:28,100 --> 00:09:31,120
to add them to the goals-net network,

193
00:09:31,120 --> 00:09:33,900
which we added in the last course section.

194
00:09:33,900 --> 00:09:36,190
If you wanna use your own network like this,

195
00:09:36,190 --> 00:09:38,560
this is absolutely something you can do

196
00:09:38,560 --> 00:09:40,320
there's nothing wrong with that.

197
00:09:40,320 --> 00:09:42,670
In this case, the MongoDB service

198
00:09:42,670 --> 00:09:45,790
would not just be added to the default network

199
00:09:45,790 --> 00:09:49,320
that is created, but also to this special network,

200
00:09:49,320 --> 00:09:50,930
which you specified here.

201
00:09:50,930 --> 00:09:52,310
So that is something you can do

202
00:09:52,310 --> 00:09:55,070
you can manage and use your own networks.

203
00:09:55,070 --> 00:09:57,810
But here I will actually go with the default network

204
00:09:57,810 --> 00:10:00,310
Docker Compose creates for us, and they are fried

205
00:10:00,310 --> 00:10:02,903
don't specify the networks key here.

206
00:10:03,930 --> 00:10:06,760
But with that, we're done with MongoDB.

207
00:10:06,760 --> 00:10:09,020
We should have all the configuration we need

208
00:10:09,020 --> 00:10:11,570
for bringing up that MongoDB service,

209
00:10:11,570 --> 00:10:14,660
and therefore I'll comment out backend and frontend,

210
00:10:14,660 --> 00:10:16,650
and I'll give this a try.

211
00:10:16,650 --> 00:10:19,370
Now all that I've ever ever known about volumes

212
00:10:19,370 --> 00:10:21,590
for named volumes,

213
00:10:21,590 --> 00:10:26,550
you should also add a volumes' key next to services

214
00:10:26,550 --> 00:10:30,280
so not indented instead on the same level as services

215
00:10:30,280 --> 00:10:32,040
at top level volumes' key

216
00:10:32,040 --> 00:10:35,750
and any named volumes you're using in your services

217
00:10:35,750 --> 00:10:39,450
have to be listed here in this case data.

218
00:10:39,450 --> 00:10:42,060
And you're simply added like this with a colon

219
00:10:42,060 --> 00:10:44,230
and then no value thereafter.

220
00:10:44,230 --> 00:10:45,680
This might look strange,

221
00:10:45,680 --> 00:10:47,900
but this is the syntax Docker wants

222
00:10:47,900 --> 00:10:50,660
for being aware of named volumes

223
00:10:50,660 --> 00:10:52,803
it should create for your services.

224
00:10:53,760 --> 00:10:55,910
As an additional note,

225
00:10:55,910 --> 00:10:59,890
if you then use the same volume name in different services,

226
00:10:59,890 --> 00:11:03,160
the volume will be shared so different containers

227
00:11:03,160 --> 00:11:04,900
can use the same volume,

228
00:11:04,900 --> 00:11:07,440
the same folder on your hosting machine.

229
00:11:07,440 --> 00:11:09,770
That is something which is possible.

230
00:11:09,770 --> 00:11:12,913
So named volumes should be specified here.

231
00:11:13,770 --> 00:11:16,230
Anonymous volumes and bind mounts

232
00:11:16,230 --> 00:11:18,173
don't need to specified here.

