1
00:00:02,240 --> 00:00:04,770
And for that, inside of the source folder,

2
00:00:04,770 --> 00:00:07,580
inside of this newly created Laravel application,

3
00:00:07,580 --> 00:00:10,100
I will first of all go to the .env file

4
00:00:10,100 --> 00:00:11,710
which you should find in there.

5
00:00:11,710 --> 00:00:13,460
This was generated by Laravel

6
00:00:13,460 --> 00:00:16,010
and it holds some configuration for Laravel.

7
00:00:16,010 --> 00:00:19,233
So not for our Docker setup but for Laravel.

8
00:00:20,530 --> 00:00:22,930
And the most important piece of information

9
00:00:22,930 --> 00:00:25,290
for us is this block here,

10
00:00:25,290 --> 00:00:27,740
which holds the connection information

11
00:00:27,740 --> 00:00:32,451
Laravel will use for connecting to a MySQL database.

12
00:00:32,451 --> 00:00:36,110
And we need to adjust this such that Laravel is able

13
00:00:36,110 --> 00:00:38,310
to connect to the database.

14
00:00:38,310 --> 00:00:41,920
Now for that, we first of all need to tweak the database

15
00:00:41,920 --> 00:00:44,600
it's trying to use, the name of the database,

16
00:00:44,600 --> 00:00:46,560
the username and the password.

17
00:00:46,560 --> 00:00:49,140
And here we should of course work with the values

18
00:00:49,140 --> 00:00:53,380
we set up for launching our MySQL server.

19
00:00:53,380 --> 00:00:56,020
And here you see I picked a username of homestead

20
00:00:56,020 --> 00:00:57,813
and then a password of secret.

21
00:00:58,680 --> 00:01:03,680
So back in .env, we should use homestead here

22
00:01:04,260 --> 00:01:08,380
as a database name because that's what I also used there,

23
00:01:08,380 --> 00:01:13,040
and then as a username add secret as a password.

24
00:01:13,040 --> 00:01:14,670
And then very important,

25
00:01:14,670 --> 00:01:17,970
the host here is not this IP address,

26
00:01:17,970 --> 00:01:21,870
instead it is the name of our MySQL service.

27
00:01:21,870 --> 00:01:23,923
In this case mysql.

28
00:01:24,840 --> 00:01:28,430
Because this request will then be sent

29
00:01:28,430 --> 00:01:31,430
from inside the PHP Laravel application

30
00:01:31,430 --> 00:01:33,600
and therefore from inside to the container,

31
00:01:33,600 --> 00:01:35,270
and hence Docker will be able

32
00:01:35,270 --> 00:01:38,710
to resolve this container name to an IP address,

33
00:01:38,710 --> 00:01:41,660
since these application servers will run

34
00:01:41,660 --> 00:01:43,103
in the same network.

35
00:01:44,300 --> 00:01:46,000
So with these adjustments made,

36
00:01:46,000 --> 00:01:49,163
we don't need to adjust anything else here for now.

37
00:01:50,030 --> 00:01:52,270
We can save this .env file

38
00:01:52,270 --> 00:01:54,453
and we can try running this application.

39
00:01:55,320 --> 00:01:59,573
And to do that, we need to use docker-compose again.

40
00:02:01,350 --> 00:02:04,830
And we will be able to use docker-compose up

41
00:02:04,830 --> 00:02:07,670
to bring up the services we wanna start.

42
00:02:07,670 --> 00:02:11,070
But before we use that, let's quickly pause

43
00:02:11,070 --> 00:02:14,150
and let's evaluate which services that will be.

44
00:02:14,150 --> 00:02:19,150
Well it will be the server, the PHP and the MySQL services.

45
00:02:19,440 --> 00:02:21,850
The server is our main entry point

46
00:02:21,850 --> 00:02:23,700
which will serve the application

47
00:02:23,700 --> 00:02:25,720
and which will then forward requests

48
00:02:25,720 --> 00:02:28,010
to the PHP interpreter for example.

49
00:02:28,010 --> 00:02:30,840
And the PHP interpreter will eventually

50
00:02:30,840 --> 00:02:33,430
indirectly talk to the MySQL database,

51
00:02:33,430 --> 00:02:36,470
because in our code we will connect to that.

52
00:02:36,470 --> 00:02:38,440
But we'll have a problem here.

53
00:02:38,440 --> 00:02:41,810
Our server, which is the main entry point currently

54
00:02:41,810 --> 00:02:44,880
doesn't know anything about our source code.

55
00:02:44,880 --> 00:02:47,990
Our interpreter does but that's not enough.

56
00:02:47,990 --> 00:02:52,240
The incoming request hits our server first,

57
00:02:52,240 --> 00:02:56,130
and it only forwards requests to PHP files

58
00:02:56,130 --> 00:02:58,400
to the PHP interpreter.

59
00:02:58,400 --> 00:03:01,570
That means that these PHP files need to be exposed

60
00:03:01,570 --> 00:03:04,130
to the server and that simply means that we need

61
00:03:04,130 --> 00:03:06,370
to add an extra volume here.

62
00:03:06,370 --> 00:03:09,880
And that extra volume, will be another bind mount

63
00:03:09,880 --> 00:03:12,270
where I bind my source folder

64
00:03:12,270 --> 00:03:17,270
to the var www html folder inside of the container.

65
00:03:19,550 --> 00:03:23,610
And I'm picking this folder because in the nginx config,

66
00:03:23,610 --> 00:03:27,210
it's the var www html folder

67
00:03:27,210 --> 00:03:29,380
from which we're serving our content

68
00:03:29,380 --> 00:03:32,320
and where we are looking for files.

69
00:03:32,320 --> 00:03:34,670
Well, of course we're looking in the public folder

70
00:03:34,670 --> 00:03:37,630
but that is a folder which exists in the source folder.

71
00:03:37,630 --> 00:03:41,810
So binding source to var www html is

72
00:03:41,810 --> 00:03:43,930
what we need to do to ensure

73
00:03:43,930 --> 00:03:48,730
that our content is made available through that server.

74
00:03:48,730 --> 00:03:51,110
Adding this volume is important.

75
00:03:51,110 --> 00:03:54,803
And now we can look into starting our services.

76
00:03:55,640 --> 00:03:58,570
So now to run our services

77
00:03:58,570 --> 00:04:00,450
and run our application containers

78
00:04:00,450 --> 00:04:02,610
and test this application,

79
00:04:02,610 --> 00:04:05,100
we use docker-compose up again.

80
00:04:05,100 --> 00:04:06,900
And up to this point in the course,

81
00:04:06,900 --> 00:04:09,223
we always used it just like this.

82
00:04:10,130 --> 00:04:13,350
Now however, I don't wanna bring up all the services.

83
00:04:13,350 --> 00:04:16,019
I don't wanna bring up composer for example,

84
00:04:16,019 --> 00:04:18,320
but I wanna bring up three of the services

85
00:04:18,320 --> 00:04:20,490
I specified in docker-compose,

86
00:04:20,490 --> 00:04:23,390
server, PHP and MySQL.

87
00:04:23,390 --> 00:04:26,590
And the great thing is that the docker-compose up command

88
00:04:26,590 --> 00:04:29,240
has a special feature here.

89
00:04:29,240 --> 00:04:31,820
We can see it if we enter --help here

90
00:04:31,820 --> 00:04:35,120
and have a look at the documentation so to say.

91
00:04:35,120 --> 00:04:38,540
We can also target specific services,

92
00:04:38,540 --> 00:04:42,560
one or more services which should be brought up.

93
00:04:42,560 --> 00:04:46,090
By default, if we just run docker-compose up

94
00:04:46,090 --> 00:04:50,950
all services specified in the docker-compose yaml file

95
00:04:50,950 --> 00:04:52,380
are brought up.

96
00:04:52,380 --> 00:04:56,340
But we could hold the target just server,

97
00:04:56,340 --> 00:05:00,713
PHP and MySQL and not composer.

98
00:05:01,580 --> 00:05:05,223
And then only these three services would be started.

99
00:05:06,110 --> 00:05:08,600
Now let's do that in detached mode here

100
00:05:08,600 --> 00:05:10,760
and see whether that works.

101
00:05:10,760 --> 00:05:15,760
If I hit enter here this now starts,

102
00:05:15,980 --> 00:05:18,350
but I'm not sure if it succeeded.

103
00:05:18,350 --> 00:05:20,250
If I inspect the running containers,

104
00:05:20,250 --> 00:05:23,173
I see that there are two containers running,

105
00:05:24,540 --> 00:05:27,563
but it looks like the nginx container did not start.

106
00:05:30,090 --> 00:05:32,680
Instead we can find that as a container

107
00:05:34,040 --> 00:05:37,510
which exited here.

108
00:05:37,510 --> 00:05:40,300
So something is not working about that.

109
00:05:40,300 --> 00:05:42,140
And the thing that is not working

110
00:05:42,140 --> 00:05:44,600
is a tiny mistake from my side.

111
00:05:44,600 --> 00:05:47,030
This config file which I'm copying

112
00:05:47,030 --> 00:05:50,440
or which I'm binding to my nginx volume

113
00:05:50,440 --> 00:05:52,610
is bound to the wrong path.

114
00:05:52,610 --> 00:05:56,330
We shouldn't bind it to this nginx config file,

115
00:05:56,330 --> 00:06:01,060
but instead we should target a conf.d folder

116
00:06:01,060 --> 00:06:06,010
in etc nginx and then they are a default.conf file.

117
00:06:06,940 --> 00:06:08,470
And that is a special file

118
00:06:08,470 --> 00:06:10,810
which in the end you could say will be merged

119
00:06:10,810 --> 00:06:13,090
into a bigger nginx config

120
00:06:13,090 --> 00:06:16,630
which is set up inside of the nginx image.

121
00:06:16,630 --> 00:06:20,010
So we should merge our own file into that file

122
00:06:20,010 --> 00:06:22,063
with this bind mount.

123
00:06:23,250 --> 00:06:26,110
With that, let's bring everything down

124
00:06:26,110 --> 00:06:28,390
with docker-compose down to make sure

125
00:06:28,390 --> 00:06:30,630
we got no running services anymore.

126
00:06:30,630 --> 00:06:33,730
And then let's repeat this docker-compose up command

127
00:06:33,730 --> 00:06:36,530
with server, PHP and MySQL.

128
00:06:36,530 --> 00:06:38,500
And this now looks better.

129
00:06:38,500 --> 00:06:43,210
Now, if we visit local host 8000 and reload this page,

130
00:06:43,210 --> 00:06:46,470
we should see our Laravel application here.

131
00:06:46,470 --> 00:06:49,790
We should see the Laravel starting screen.

132
00:06:49,790 --> 00:06:53,000
And that is from the Laravel application we created

133
00:06:53,000 --> 00:06:54,343
in this folder.

134
00:06:55,460 --> 00:06:56,420
Now one other word

135
00:06:56,420 --> 00:06:59,040
about the docker-compose up command though,

136
00:06:59,040 --> 00:07:01,140
it works fine like this,

137
00:07:01,140 --> 00:07:03,840
but naming all the individual services

138
00:07:03,840 --> 00:07:06,990
which should be started is a bit annoying.

139
00:07:06,990 --> 00:07:09,097
It would be better if we could just name

140
00:07:09,097 --> 00:07:11,620
the server service for example,

141
00:07:11,620 --> 00:07:13,610
and the other two services would

142
00:07:13,610 --> 00:07:15,870
then be started automatically.

143
00:07:15,870 --> 00:07:18,630
And we can let docker-compose know

144
00:07:18,630 --> 00:07:20,780
that something like this should happen

145
00:07:20,780 --> 00:07:25,023
by adding dependencies to this server service.

146
00:07:25,970 --> 00:07:30,793
We can add depends_on and specify PHP and MySQL as services

147
00:07:32,581 --> 00:07:35,960
on which this server service depends.

148
00:07:35,960 --> 00:07:37,530
Which makes a lot of sense.

149
00:07:37,530 --> 00:07:40,820
The nginx server really only works,

150
00:07:40,820 --> 00:07:43,700
if it's able to talk to the PHP service

151
00:07:43,700 --> 00:07:47,530
and if that service is able to talk to MySQL.

152
00:07:47,530 --> 00:07:51,130
So in the end we depend on both services here.

153
00:07:51,130 --> 00:07:55,560
Alternatively, we specify MySQL as a dependency of PHP,

154
00:07:55,560 --> 00:07:56,950
that would also work.

155
00:07:56,950 --> 00:07:59,100
But I'll stick to this setup.

156
00:07:59,100 --> 00:08:00,440
And with this added,

157
00:08:00,440 --> 00:08:03,600
with this depends_on configuration added,

158
00:08:03,600 --> 00:08:06,550
docker-compose makes sure that if we bring up

159
00:08:06,550 --> 00:08:08,480
this server service,

160
00:08:08,480 --> 00:08:10,840
it also automatically brings up the services

161
00:08:10,840 --> 00:08:11,890
on which depend.

162
00:08:11,890 --> 00:08:14,580
In this case PHP and MySQL.

163
00:08:14,580 --> 00:08:17,130
So if I save this and I hit enter,

164
00:08:17,130 --> 00:08:20,880
we still start all these dependent services.

165
00:08:20,880 --> 00:08:23,330
Doesn't look like it here,

166
00:08:23,330 --> 00:08:25,963
for that let me first of all bring everything down,

167
00:08:27,540 --> 00:08:29,780
and then let me run up again.

168
00:08:29,780 --> 00:08:33,303
And you see it still starts all three services.

169
00:08:34,299 --> 00:08:36,330
So with that I'll bring it down again,

170
00:08:36,330 --> 00:08:39,700
because I want to tweak the up command even more.

171
00:08:39,700 --> 00:08:43,549
It works absolutely fine the way we're using it right now,

172
00:08:43,549 --> 00:08:45,720
but there is a little extra option

173
00:08:45,720 --> 00:08:48,780
which I wanna add to the up command.

174
00:08:48,780 --> 00:08:51,050
The little tweak I wanna make is related

175
00:08:51,050 --> 00:08:54,860
to our custom images like the PHP image.

176
00:08:54,860 --> 00:08:58,430
Right now, what docker-compose does as a default

177
00:08:58,430 --> 00:09:00,950
is it looks if an image exists,

178
00:09:00,950 --> 00:09:03,200
and if it exists it use a that.

179
00:09:03,200 --> 00:09:06,570
It never rebuilds that image.

180
00:09:06,570 --> 00:09:10,300
Which means that if we made a change to the Docker file

181
00:09:10,300 --> 00:09:13,260
or to some files that were copied in through

182
00:09:13,260 --> 00:09:16,250
the Docker file, something we're not doing here,

183
00:09:16,250 --> 00:09:18,510
but which we could be doing of course,

184
00:09:18,510 --> 00:09:22,160
then those changes would not be picked up by docker-compose.

185
00:09:22,160 --> 00:09:25,270
It would not rebuild our images.

186
00:09:25,270 --> 00:09:27,160
And I guess that makes sense.

187
00:09:27,160 --> 00:09:28,640
Without docker-compose,

188
00:09:28,640 --> 00:09:31,590
we also had to run docker build manually

189
00:09:31,590 --> 00:09:34,220
to build a changed image again.

190
00:09:34,220 --> 00:09:36,940
So therefore, to force docker-compose

191
00:09:36,940 --> 00:09:39,430
to reevaluate our Docker files

192
00:09:39,430 --> 00:09:41,860
and rebuild images if required,

193
00:09:41,860 --> 00:09:45,883
we should also add the --build option here.

194
00:09:47,060 --> 00:09:49,520
This forces docker-compose to go

195
00:09:49,520 --> 00:09:51,280
through the Docker files again

196
00:09:51,280 --> 00:09:54,540
and then recreate the images if something changed.

197
00:09:54,540 --> 00:09:56,410
If nothing changed because

198
00:09:56,410 --> 00:09:58,640
of that layer concept in images

199
00:09:58,640 --> 00:10:00,810
and because layers are cached,

200
00:10:00,810 --> 00:10:02,870
it will of course not rebuild the image.

201
00:10:02,870 --> 00:10:05,060
It will effectively not rebuild anything,

202
00:10:05,060 --> 00:10:07,683
it will just reuse these cached layers.

203
00:10:08,660 --> 00:10:10,010
So if I now hit enter,

204
00:10:10,010 --> 00:10:14,210
you'll see initially it goes through the image build process

205
00:10:14,210 --> 00:10:15,710
for the PHP image.

206
00:10:15,710 --> 00:10:19,010
It's super quick because nothing really changed there,

207
00:10:19,010 --> 00:10:20,920
so it's able to use the cache.

208
00:10:20,920 --> 00:10:23,540
But still we ensure that any changes

209
00:10:23,540 --> 00:10:26,540
we might've made will be picked up.

210
00:10:26,540 --> 00:10:28,550
And with that it still runs fine,

211
00:10:28,550 --> 00:10:31,400
but now that's the command I recommend using here

212
00:10:31,400 --> 00:10:35,290
to ensure that you always use the latest images.

213
00:10:35,290 --> 00:10:36,890
Of course the alternative is

214
00:10:36,890 --> 00:10:39,490
that you use it without --build

215
00:10:39,490 --> 00:10:43,990
and you only add --build if you know that something changed

216
00:10:43,990 --> 00:10:46,820
about your image because you changed the Docker file,

217
00:10:46,820 --> 00:10:49,640
or because you changed some folders

218
00:10:49,640 --> 00:10:52,320
or files that are copied into the image

219
00:10:52,320 --> 00:10:53,960
through the Docker file.

220
00:10:53,960 --> 00:10:55,660
But you can also always add it

221
00:10:55,660 --> 00:10:59,020
to always check this and that's what I'll do here.

222
00:10:59,020 --> 00:11:01,950
But now, we got a Laravel application up and running

223
00:11:02,890 --> 00:11:07,420
and we can change our code here in the source folder.

224
00:11:07,420 --> 00:11:10,150
For example we can go to resources views

225
00:11:10,150 --> 00:11:12,350
and go to this welcome file

226
00:11:12,350 --> 00:11:16,187
and simply add h1 tag here, Hello.

227
00:11:22,280 --> 00:11:27,280
Save this, reload and we see it here.

228
00:11:27,620 --> 00:11:29,980
So now we can work on that code

229
00:11:29,980 --> 00:11:34,133
and work on this Laravel application with this setup.

230
00:11:35,170 --> 00:11:37,620
Now, there's just a little work left

231
00:11:37,620 --> 00:11:40,700
and that would be the other two utility containers

232
00:11:40,700 --> 00:11:44,040
which we don't need to just start the application,

233
00:11:44,040 --> 00:11:46,990
but which we for example need to run migrations

234
00:11:46,990 --> 00:11:49,810
or handle a client side JavaScript code.

235
00:11:49,810 --> 00:11:52,223
So let's now tackle these two containers.

