1
00:00:02,190 --> 00:00:04,780
So let's no work on that PHP container

2
00:00:04,780 --> 00:00:07,130
now that we got the nginx container.

3
00:00:07,130 --> 00:00:11,840
The PHP container will be relatively straight forward.

4
00:00:11,840 --> 00:00:16,129
Here, I actually want to use a custom docker file though

5
00:00:16,129 --> 00:00:18,160
because there is no finished image

6
00:00:18,160 --> 00:00:20,290
which has everything I need.

7
00:00:20,290 --> 00:00:24,190
Just to clarify that, if you search for a PHP on docker hub,

8
00:00:24,190 --> 00:00:26,270
you will find an official image

9
00:00:26,270 --> 00:00:28,330
and we are going to use that.

10
00:00:28,330 --> 00:00:30,830
But I actually wanna build a custom image,

11
00:00:30,830 --> 00:00:34,610
building up on that image because I don't just want PHP,

12
00:00:34,610 --> 00:00:37,410
I also want to install some extra extensions

13
00:00:37,410 --> 00:00:38,773
which laravel needs.

14
00:00:39,630 --> 00:00:41,530
So therefore I'll add a new folder

15
00:00:41,530 --> 00:00:45,910
next to the nginx folder, which I'll name dockerfiles.

16
00:00:45,910 --> 00:00:48,843
And in there, I'll add a php.dockerfile.

17
00:00:51,790 --> 00:00:53,470
The name is generally up to you,

18
00:00:53,470 --> 00:00:55,160
but by using this naming pattern,

19
00:00:55,160 --> 00:00:57,160
I ensure that visuals to do code,

20
00:00:57,160 --> 00:00:58,810
the texters as a docker file

21
00:00:58,810 --> 00:01:01,770
and helps me out with some auto-completion.

22
00:01:01,770 --> 00:01:04,780
So in here, I will start with the PHP base image,

23
00:01:04,780 --> 00:01:06,690
which I just showed you.

24
00:01:06,690 --> 00:01:08,080
I'll use that.

25
00:01:08,080 --> 00:01:09,870
And here if you go to tags,

26
00:01:09,870 --> 00:01:12,963
we also have multiple tags to choose from.

27
00:01:13,850 --> 00:01:18,850
I will use the 7.4-fpm-alpine image,

28
00:01:19,490 --> 00:01:21,140
which is a great PHP image,

29
00:01:21,140 --> 00:01:23,683
very slim, and has everything we need here.

30
00:01:24,690 --> 00:01:26,980
Then I will run a command.

31
00:01:26,980 --> 00:01:29,790
And that's the reason why I'm building this custom image.

32
00:01:29,790 --> 00:01:32,030
I'm running a command where I will install

33
00:01:32,030 --> 00:01:34,400
some extra dependencies which we need

34
00:01:34,400 --> 00:01:37,160
and conveniently, in this base image

35
00:01:37,160 --> 00:01:40,370
there is a tool which we can use to install that.

36
00:01:40,370 --> 00:01:45,370
And that's the docker-php-ext-install tool.

37
00:01:46,330 --> 00:01:48,350
Yes, it has a strange name.

38
00:01:48,350 --> 00:01:50,810
Where we can install the pdo,

39
00:01:50,810 --> 00:01:54,040
and pdo_mysql extensions

40
00:01:54,040 --> 00:01:57,850
and these are our PHP extensions, which we need.

41
00:01:57,850 --> 00:01:59,640
Now, we need to ensure that this command

42
00:01:59,640 --> 00:02:01,530
is executed in the right folder.

43
00:02:01,530 --> 00:02:03,640
And therefore we should set a working directory

44
00:02:03,640 --> 00:02:08,583
to /var/www/html.

45
00:02:09,460 --> 00:02:11,540
This folder might look strange,

46
00:02:11,540 --> 00:02:15,230
but it is a pretty standard folder on web servers

47
00:02:15,230 --> 00:02:17,410
to serve your website from.

48
00:02:17,410 --> 00:02:20,840
So we will actually ensure that in all the containers

49
00:02:20,840 --> 00:02:22,850
we're going to build throughout this module,

50
00:02:22,850 --> 00:02:25,430
we always use this as the folder

51
00:02:25,430 --> 00:02:28,730
which should hold our final application.

52
00:02:28,730 --> 00:02:31,830
At the moment, we already saw this folder once,

53
00:02:31,830 --> 00:02:33,900
here in the nginx config,

54
00:02:33,900 --> 00:02:37,440
assuming that it finds the web application files

55
00:02:37,440 --> 00:02:41,490
in that folder, or to be precise in a sub folder of it.

56
00:02:41,490 --> 00:02:44,900
And we'll stick to that folder for all this entire module

57
00:02:44,900 --> 00:02:46,700
because this will be the folder,

58
00:02:46,700 --> 00:02:49,170
which in the end inside of the containers

59
00:02:49,170 --> 00:02:53,080
should hold our laravel PHP application.

60
00:02:53,080 --> 00:02:54,990
So that's why I switched to this folder

61
00:02:54,990 --> 00:02:57,800
as a working directory and why I then install

62
00:02:57,800 --> 00:02:59,990
these extra dependencies there.

63
00:02:59,990 --> 00:03:03,310
Now with that, we're finished with this image

64
00:03:03,310 --> 00:03:04,960
or with this docker file,

65
00:03:04,960 --> 00:03:06,830
and you will know it does one thing.

66
00:03:06,830 --> 00:03:10,090
We don't have a command or an entry point here,

67
00:03:10,090 --> 00:03:13,680
instead we end with the run instruction.

68
00:03:13,680 --> 00:03:15,770
Now up to this point, in the course,

69
00:03:15,770 --> 00:03:17,130
in our docker files,

70
00:03:17,130 --> 00:03:20,150
we always had some command instruction at the end,

71
00:03:20,150 --> 00:03:21,660
here we don't.

72
00:03:21,660 --> 00:03:25,700
If you don't have a command or entry point at the end,

73
00:03:25,700 --> 00:03:29,260
then the command or entry point of the base image

74
00:03:29,260 --> 00:03:31,130
will be used if it has any.

75
00:03:31,130 --> 00:03:34,730
And this base image here, this php base image

76
00:03:34,730 --> 00:03:37,430
will have a default command which is executed

77
00:03:37,430 --> 00:03:39,810
which in the end is a command that invokes

78
00:03:39,810 --> 00:03:42,220
the PHP interpreter.

79
00:03:42,220 --> 00:03:44,640
So this image which we're building here

80
00:03:44,640 --> 00:03:47,200
will then automatically run this default command

81
00:03:47,200 --> 00:03:48,350
of the base image.

82
00:03:48,350 --> 00:03:50,910
And therefore it will be able to deal

83
00:03:50,910 --> 00:03:54,900
with incoming PHP files that should be interpreted

84
00:03:54,900 --> 00:03:58,150
because our base image is invoking this interpreter.

85
00:03:58,150 --> 00:04:00,810
And that's just something I wanted to clarify here

86
00:04:00,810 --> 00:04:03,480
because it is important to be aware of that.

87
00:04:03,480 --> 00:04:06,200
With that, we prepared this docker file.

88
00:04:06,200 --> 00:04:09,560
In the docker compose file we can now reference

89
00:04:09,560 --> 00:04:11,200
this docker file.

90
00:04:11,200 --> 00:04:13,380
So here we can add the build,

91
00:04:13,380 --> 00:04:16,980
set up the build configuration

92
00:04:16,980 --> 00:04:18,945
and now it's not enough to just point

93
00:04:18,945 --> 00:04:21,459
at the docker files folder,

94
00:04:21,459 --> 00:04:25,830
because the file in there is not named just dockerfile.

95
00:04:25,830 --> 00:04:28,850
Instead, we now need the more detailed configuration

96
00:04:28,850 --> 00:04:30,880
which I showed you in the last section.

97
00:04:30,880 --> 00:04:33,420
here, we now specify the context,

98
00:04:33,420 --> 00:04:36,330
which is the folder to find this file in,

99
00:04:36,330 --> 00:04:38,800
in this case, it's dockerfiles.

100
00:04:38,800 --> 00:04:41,260
And then we specify dockerfile

101
00:04:41,260 --> 00:04:44,860
to let composer know about the name of the docker file

102
00:04:44,860 --> 00:04:46,280
that should be used here.

103
00:04:46,280 --> 00:04:50,880
And in this case, that's php.docker file written like this.

104
00:04:50,880 --> 00:04:54,730
And then docker compose will be able to find that file

105
00:04:54,730 --> 00:04:56,663
and built the respective image.

106
00:04:58,300 --> 00:05:01,680
Okay, so now we've got the image set up for this container.

107
00:05:01,680 --> 00:05:04,920
Now, there are two other things we got to do here.

108
00:05:04,920 --> 00:05:07,780
The first very important thing we have to do here

109
00:05:07,780 --> 00:05:11,830
is we have to ensure that this php interpreter

110
00:05:11,830 --> 00:05:14,220
can reach our source code.

111
00:05:14,220 --> 00:05:16,470
Now we have no source code yet,

112
00:05:16,470 --> 00:05:18,640
but we will have some source code,

113
00:05:18,640 --> 00:05:22,710
our php laravel application source code later.

114
00:05:22,710 --> 00:05:26,320
And that source code in the end needs to be available

115
00:05:26,320 --> 00:05:29,570
in this folder instead of the container.

116
00:05:29,570 --> 00:05:32,470
So what we'll need is a bind mount,

117
00:05:32,470 --> 00:05:34,630
because I want to work on that source code

118
00:05:34,630 --> 00:05:36,940
in my project folder, of course.

119
00:05:36,940 --> 00:05:39,920
So I want to Mount my project folder

120
00:05:39,920 --> 00:05:43,830
or some sub folder here into this folder

121
00:05:43,830 --> 00:05:45,503
inside of the container.

122
00:05:47,550 --> 00:05:50,270
Hence I'll start by creating a new folder

123
00:05:50,270 --> 00:05:53,330
on my local host machine in this project folder,

124
00:05:53,330 --> 00:05:55,939
and I will name it SRC for source.

125
00:05:55,939 --> 00:05:59,763
But you can of course also pick a different name here.

126
00:06:00,630 --> 00:06:04,300
And then we can add volumes here to the PHP container

127
00:06:04,300 --> 00:06:06,640
and add one volume.

128
00:06:06,640 --> 00:06:09,330
And that's a point mount pointing at the source folder

129
00:06:09,330 --> 00:06:10,960
on our local machine.

130
00:06:10,960 --> 00:06:15,960
And we'll then mount this to the var/www/html path

131
00:06:16,450 --> 00:06:18,840
inside of the container.

132
00:06:18,840 --> 00:06:20,490
Here we can then actually, by the way,

133
00:06:20,490 --> 00:06:22,660
also improve performance a little bit

134
00:06:22,660 --> 00:06:27,090
by adding delegated after a colon at the end,

135
00:06:27,090 --> 00:06:28,730
we haven't seen that before,

136
00:06:28,730 --> 00:06:30,690
in the end that means that if the containers

137
00:06:30,690 --> 00:06:32,260
should write some data there,

138
00:06:32,260 --> 00:06:35,570
it's not instantly reflected back to the host machine,

139
00:06:35,570 --> 00:06:39,170
instead it is basically processed in batches, you could say,

140
00:06:39,170 --> 00:06:41,830
and therefore performance is a bit better.

141
00:06:41,830 --> 00:06:44,930
It's a optimization, we don't have to do it here,

142
00:06:44,930 --> 00:06:46,180
but we can add it.

143
00:06:46,180 --> 00:06:48,563
And does this a nice place to introduce it.

144
00:06:49,420 --> 00:06:52,430
So adding delegated here improves performance

145
00:06:52,430 --> 00:06:56,120
since this container shouldn't write frequently

146
00:06:56,120 --> 00:06:57,680
to this folder,

147
00:06:57,680 --> 00:07:00,730
or even if it does the things it does right

148
00:07:00,730 --> 00:07:02,500
don't need to be reflected back

149
00:07:02,500 --> 00:07:04,070
to the host machine instantly.

150
00:07:04,070 --> 00:07:06,210
Because it won't write anything which

151
00:07:06,210 --> 00:07:09,650
we need to access instantly on the host machine.

152
00:07:09,650 --> 00:07:11,580
Read only is not an option

153
00:07:11,580 --> 00:07:14,840
because it will write some data to our disc.

154
00:07:14,840 --> 00:07:17,440
The Laravel framework will also generate

155
00:07:17,440 --> 00:07:19,050
some files when it runs,

156
00:07:19,050 --> 00:07:21,310
for example the views which are sent back

157
00:07:21,310 --> 00:07:22,813
as part of responses.

158
00:07:23,970 --> 00:07:26,110
So therefore this is one important thing,

159
00:07:26,110 --> 00:07:29,070
adding this volume which allows us to have a folder

160
00:07:29,070 --> 00:07:31,120
for our source code and which allows us

161
00:07:31,120 --> 00:07:32,550
to work on the source code

162
00:07:32,550 --> 00:07:35,870
and expose it to the PHP interpreter.

163
00:07:35,870 --> 00:07:38,322
The other important thing is the port on which

164
00:07:38,322 --> 00:07:42,980
this PHP interpreter listens for work you could say.

165
00:07:42,980 --> 00:07:47,980
And this port is actually defined here in nginx conf.

166
00:07:48,170 --> 00:07:53,170
Here, I'm sending requests or PHP requests, you could say,

167
00:07:54,287 --> 00:07:57,720
We could say to port 3000.

168
00:07:57,720 --> 00:08:00,550
But first of all here, I have an address.

169
00:08:00,550 --> 00:08:02,860
And that could be an IP address here,

170
00:08:02,860 --> 00:08:05,360
something like this.

171
00:08:05,360 --> 00:08:07,823
But I'm actually utilizing the fact

172
00:08:07,823 --> 00:08:11,910
that all the containers created in one docker compose file

173
00:08:11,910 --> 00:08:13,770
are part of the same network

174
00:08:13,770 --> 00:08:17,180
and can discover each other by name.

175
00:08:17,180 --> 00:08:21,440
So by the names specified here as services,

176
00:08:21,440 --> 00:08:24,320
as long as the code executes inside of the container,

177
00:08:24,320 --> 00:08:26,880
which definitely will be the case here.

178
00:08:26,880 --> 00:08:30,110
So here I'm referring to the PHP container

179
00:08:30,110 --> 00:08:31,590
by using its name.

180
00:08:31,590 --> 00:08:33,840
And hence, if you chose a different name here

181
00:08:33,840 --> 00:08:37,950
for this service, you also need a different name here.

182
00:08:37,950 --> 00:08:40,476
And then I basically want to,

183
00:08:40,476 --> 00:08:44,830
well send my request to handle some PHP file

184
00:08:44,830 --> 00:08:46,783
to port 3000.

185
00:08:47,740 --> 00:08:51,220
And that means that here in docker compose,

186
00:08:51,220 --> 00:08:54,970
I wanna ensure that this port is being used.

187
00:08:54,970 --> 00:08:56,990
Now by looking into the docker file,

188
00:08:56,990 --> 00:08:59,510
of this official PHP image,

189
00:08:59,510 --> 00:09:01,170
which I opened here on GitHub,

190
00:09:01,170 --> 00:09:03,640
we see that this image actually exposes

191
00:09:03,640 --> 00:09:06,120
port 9,000 internally.

192
00:09:06,120 --> 00:09:09,330
So that is what will be exposed by the container.

193
00:09:09,330 --> 00:09:13,530
And again, I'm on the GitHub page of this PHP Docker image,

194
00:09:13,530 --> 00:09:15,900
and then navigate it down into some folders

195
00:09:15,900 --> 00:09:19,003
to view the docker file of the image we're using.

196
00:09:20,360 --> 00:09:23,290
So the container exposes port 9000.

197
00:09:23,290 --> 00:09:26,740
I actually expect port 3000 here,

198
00:09:26,740 --> 00:09:27,900
and they are four here,

199
00:09:27,900 --> 00:09:32,350
we have to map 3000 to 9000.

200
00:09:32,350 --> 00:09:34,650
We have our host machine first

201
00:09:34,650 --> 00:09:38,640
and then we have our container internal port.

202
00:09:38,640 --> 00:09:40,960
However, this is now the tricky part.

203
00:09:40,960 --> 00:09:43,323
Is this really something we need here?

204
00:09:44,170 --> 00:09:47,750
Keep in mind that in the end, it's our nginx server

205
00:09:47,750 --> 00:09:50,610
which wants to talk to this PHP container.

206
00:09:50,610 --> 00:09:53,073
Which is why we're referencing it by name here.

207
00:09:54,020 --> 00:09:55,660
Well, if that's the case,

208
00:09:55,660 --> 00:09:58,780
this port is going straight to the container

209
00:09:58,780 --> 00:10:02,580
and it's not going to be sent from our local host machine.

210
00:10:02,580 --> 00:10:04,923
So this mapping won't do anything here.

211
00:10:05,860 --> 00:10:07,810
Hence, whilst we can map here

212
00:10:07,810 --> 00:10:10,990
in case we want to interact with that PHP container

213
00:10:10,990 --> 00:10:14,720
from our host machine, we actually don't need to do that

214
00:10:14,720 --> 00:10:17,640
just to send a request here.

215
00:10:17,640 --> 00:10:20,660
Instead we should change this and the nginx config

216
00:10:20,660 --> 00:10:22,520
to port 9000.

217
00:10:22,520 --> 00:10:25,450
And I just want to emphasize why we're doing that,

218
00:10:25,450 --> 00:10:29,200
because we have direct container to container communication.

219
00:10:29,200 --> 00:10:31,393
Not through our local host.

220
00:10:32,470 --> 00:10:35,350
So with that changed and yet nginx config,

221
00:10:35,350 --> 00:10:39,080
we are now done and we finished this PHP container.

222
00:10:39,080 --> 00:10:41,933
Let's now move on to the, mysql container.

