1
00:00:02,330 --> 00:00:05,130
So let's get started writing some code here.

2
00:00:05,130 --> 00:00:06,900
I'm in a empty folder,

3
00:00:06,900 --> 00:00:09,240
and I just wanna really emphasize

4
00:00:09,240 --> 00:00:12,660
that I don't have Composer and so on installed.

5
00:00:12,660 --> 00:00:16,020
If I try to run this, I get command not found.

6
00:00:16,020 --> 00:00:18,980
So here, I'm really missing some of the tools

7
00:00:18,980 --> 00:00:22,820
which we need to build a Laravel application.

8
00:00:22,820 --> 00:00:25,770
And therefore, we're going to use Docker for that.

9
00:00:25,770 --> 00:00:30,750
And I am going to start by adding a docker-compose.yaml file

10
00:00:30,750 --> 00:00:34,810
because we are going to build multiple containers,

11
00:00:34,810 --> 00:00:37,320
and these containers need to interact,

12
00:00:37,320 --> 00:00:40,480
and I also, even if it would be just one container,

13
00:00:40,480 --> 00:00:44,290
I also like to have this configuration in a text file

14
00:00:44,290 --> 00:00:47,090
which is easy to read and change.

15
00:00:47,090 --> 00:00:48,620
So we'll use Docker Compose

16
00:00:48,620 --> 00:00:52,150
to set up this entire application, this entire project,

17
00:00:52,150 --> 00:00:55,150
and it will hold both the application containers

18
00:00:55,150 --> 00:00:57,460
as well as the utility containers.

19
00:00:57,460 --> 00:00:59,930
And I will also show you how you can run all

20
00:00:59,930 --> 00:01:03,103
or just some of these containers throughout this module.

21
00:01:04,010 --> 00:01:06,250
So let's get started by adding a version.

22
00:01:06,250 --> 00:01:09,950
And, again, I'll lock in version 3.8 here.

23
00:01:09,950 --> 00:01:13,600
And then, as a next step, let's specify our services.

24
00:01:13,600 --> 00:01:16,330
So our containers, which we want here.

25
00:01:16,330 --> 00:01:18,220
Now, I showed you on the slide

26
00:01:18,220 --> 00:01:21,260
that we'll have six different services.

27
00:01:21,260 --> 00:01:26,100
We'll have our server service, the nginx server,

28
00:01:26,100 --> 00:01:29,090
which will in the end take all the incoming requests

29
00:01:29,090 --> 00:01:31,453
and trigger the PHP interpreter.

30
00:01:32,292 --> 00:01:35,590
We'll have the PHP container therefore,

31
00:01:35,590 --> 00:01:40,210
which is responsible for running and executing our PHP code

32
00:01:40,210 --> 00:01:42,340
and therefore also the Laravel code

33
00:01:42,340 --> 00:01:45,780
because Laravel is just a PHP framework.

34
00:01:45,780 --> 00:01:48,640
We'll have the MySQL container,

35
00:01:48,640 --> 00:01:51,240
which holds the MySQL database,

36
00:01:51,240 --> 00:01:54,550
and then we'll also have these utility containers.

37
00:01:54,550 --> 00:01:56,740
We'll have the composer container,

38
00:01:56,740 --> 00:01:58,900
we'll have the npm container,

39
00:01:58,900 --> 00:02:03,440
and, maybe in front of that, but the order does not matter,

40
00:02:03,440 --> 00:02:06,423
still here, I'll have my artisan container.

41
00:02:07,420 --> 00:02:10,979
So these are the six containers we need here.

42
00:02:10,979 --> 00:02:13,950
And I will, of course, add them step by step.

43
00:02:13,950 --> 00:02:15,913
And let's start with the server.

44
00:02:16,880 --> 00:02:20,180
Now, the server, as I mentioned, will use nginx,

45
00:02:20,180 --> 00:02:22,210
which is a very popular, and powerful,

46
00:02:22,210 --> 00:02:24,550
and efficient web server.

47
00:02:24,550 --> 00:02:28,640
Now, the great thing is that if you Google for docker nginx,

48
00:02:28,640 --> 00:02:33,640
you find an official nginx image which you can use.

49
00:02:33,830 --> 00:02:36,890
So just as with MongoDB or Node,

50
00:02:36,890 --> 00:02:39,590
we also have an official image for that.

51
00:02:39,590 --> 00:02:44,080
And we can simply use that to set up a nginx server.

52
00:02:44,080 --> 00:02:47,050
You will also find documentation

53
00:02:47,050 --> 00:02:49,340
on how you may use this image

54
00:02:49,340 --> 00:02:52,350
here on this Docker Hub page, of course.

55
00:02:52,350 --> 00:02:55,360
But we, of course, will set it up together anyways.

56
00:02:55,360 --> 00:02:57,760
So back in the docker-compose file,

57
00:02:57,760 --> 00:03:00,520
we can now specify an image for the server,

58
00:03:00,520 --> 00:03:04,210
and we'll use an official image here, the nginx image.

59
00:03:04,210 --> 00:03:06,700
And now, there again, we've got multiple tags,

60
00:03:06,700 --> 00:03:09,320
multiple versions of that image we could use,

61
00:03:09,320 --> 00:03:13,710
and I will use this stable-alpine tag here

62
00:03:13,710 --> 00:03:16,190
to give us an image, which again is based

63
00:03:16,190 --> 00:03:20,060
on this very slim Linux operating system layer,

64
00:03:20,060 --> 00:03:23,713
and then obviously a stable version of this nginx image.

65
00:03:24,610 --> 00:03:29,363
So here, it's :stable-alpine, which I wanna use here.

66
00:03:30,620 --> 00:03:32,300
And even though you don't have to,

67
00:03:32,300 --> 00:03:34,600
I'll put that between quotes.

68
00:03:34,600 --> 00:03:37,010
It's often a bit safer to do that for text

69
00:03:37,010 --> 00:03:39,850
to ensure that nothing is somehow interpreted

70
00:03:39,850 --> 00:03:41,720
in a wrong way.

71
00:03:41,720 --> 00:03:45,790
Great, so that's the image we wanna use for our web server.

72
00:03:45,790 --> 00:03:48,833
Now, this web server also exposes a port.

73
00:03:49,790 --> 00:03:51,090
And how do I know that?

74
00:03:51,090 --> 00:03:54,010
Well, the official documentation tells us.

75
00:03:54,010 --> 00:03:59,010
It tells us here for example that we can expose this port 80

76
00:03:59,360 --> 00:04:03,883
which is, as it seems, exposed internally by that image.

77
00:04:04,780 --> 00:04:07,160
So therefore I wanna bind this port

78
00:04:07,160 --> 00:04:12,160
with the ports option here to some port on my host machine.

79
00:04:12,700 --> 00:04:16,500
And there, I'll use port 8000

80
00:04:16,500 --> 00:04:20,170
and bind this to the port 80 exposed by this image,

81
00:04:20,170 --> 00:04:22,720
and therefore by the running container.

82
00:04:22,720 --> 00:04:24,970
Now, we'll be able to spin up a container

83
00:04:24,970 --> 00:04:26,670
which has a server in it.

84
00:04:26,670 --> 00:04:29,900
But this server out of the box isn't too useful.

85
00:04:29,900 --> 00:04:32,480
It won't really know what it should do.

86
00:04:32,480 --> 00:04:35,000
And what it should do in this application

87
00:04:35,000 --> 00:04:38,090
is that it should have a look at incoming requests

88
00:04:38,090 --> 00:04:41,420
and eventually funnel them to our PHP container,

89
00:04:41,420 --> 00:04:42,550
which we'll have later,

90
00:04:42,550 --> 00:04:46,290
and let that container execute our PHP code.

91
00:04:46,290 --> 00:04:48,620
So to provide our own configuration,

92
00:04:48,620 --> 00:04:51,943
I'll add a bind mount here with the volumes key.

93
00:04:53,410 --> 00:04:56,210
And here, I wanna bind a local folder,

94
00:04:56,210 --> 00:04:59,800
let's say a nginx folder which we have yet to add,

95
00:04:59,800 --> 00:05:03,260
and in there, a nginx.conf file,

96
00:05:03,260 --> 00:05:05,920
and you can also target individual files

97
00:05:05,920 --> 00:05:08,350
in case you don't wanna bind a complete folder,

98
00:05:08,350 --> 00:05:12,250
but you just wanna copy one specific file into a container,

99
00:05:12,250 --> 00:05:15,320
then you can target that specific file.

100
00:05:15,320 --> 00:05:20,320
And, in the container, I wanna bind it to an absolute path,

101
00:05:20,680 --> 00:05:23,340
which should be in the etc folder,

102
00:05:23,340 --> 00:05:27,430
then nginx, and then nginx.conf.

103
00:05:27,430 --> 00:05:31,800
And in case you wonder why I picked this path specifically,

104
00:05:31,800 --> 00:05:33,800
well, because the official docs

105
00:05:33,800 --> 00:05:35,780
for this image tell me to do that,

106
00:05:35,780 --> 00:05:38,180
and that is how you work with these official images.

107
00:05:38,180 --> 00:05:41,860
You don't just know which ports are exposed

108
00:05:41,860 --> 00:05:45,000
or which folders do something special in the image,

109
00:05:45,000 --> 00:05:47,560
instead you have to read the documentation

110
00:05:47,560 --> 00:05:49,210
to learn such things.

111
00:05:49,210 --> 00:05:50,940
And here, we learned that we can pass in

112
00:05:50,940 --> 00:05:53,320
our own configuration by binding

113
00:05:53,320 --> 00:05:56,280
to this container internal path.

114
00:05:56,280 --> 00:05:59,230
And we can, of course, also set this to read only

115
00:05:59,230 --> 00:06:01,540
because the container should never change

116
00:06:01,540 --> 00:06:03,510
that configuration.

117
00:06:03,510 --> 00:06:07,150
And this will allow us to pass our own configuration file

118
00:06:07,150 --> 00:06:11,510
for this web server into this web server container.

119
00:06:11,510 --> 00:06:14,980
Now, we still need to create that on our host machine,

120
00:06:14,980 --> 00:06:17,490
so I will add an nginx folder here,

121
00:06:17,490 --> 00:06:20,620
and in that folder, an nginx.conf file.

122
00:06:20,620 --> 00:06:23,750
Now, you find the actual configuration file attached

123
00:06:23,750 --> 00:06:26,830
to this lecture because I already prepared it for you.

124
00:06:26,830 --> 00:06:29,333
So you can just use this finished file here.

125
00:06:30,240 --> 00:06:34,860
And this file in the end has some nginx configuration inside

126
00:06:34,860 --> 00:06:37,540
which basically listens on port 80,

127
00:06:37,540 --> 00:06:39,470
so we could change this here now

128
00:06:39,470 --> 00:06:41,620
if we wanted to listen on a different port

129
00:06:41,620 --> 00:06:43,180
inside of the container,

130
00:06:43,180 --> 00:06:46,360
which then handles requests to index files,

131
00:06:46,360 --> 00:06:48,240
which then also makes sure that we look

132
00:06:48,240 --> 00:06:51,620
in this directory for files

133
00:06:51,620 --> 00:06:55,250
that we can use to respond to the incoming request.

134
00:06:55,250 --> 00:06:57,610
This directory will become important soon.

135
00:06:57,610 --> 00:06:59,090
At the moment, we don't have it,

136
00:06:59,090 --> 00:07:01,260
neither in the container nor here,

137
00:07:01,260 --> 00:07:03,230
but I will come back to that.

138
00:07:03,230 --> 00:07:05,410
And then, we have redirection rules,

139
00:07:05,410 --> 00:07:08,710
basically ensuring that all incoming requests

140
00:07:08,710 --> 00:07:11,810
are redirected to index PHP files,

141
00:07:11,810 --> 00:07:15,390
or requests that do already target PHP files

142
00:07:15,390 --> 00:07:20,070
are in the end then forwarded to our PHP interpreter.

143
00:07:20,070 --> 00:07:21,800
And that's, of course, not something

144
00:07:21,800 --> 00:07:25,300
which will work right now, but it will work later.

145
00:07:25,300 --> 00:07:27,540
So let's save this config file

146
00:07:27,540 --> 00:07:30,840
and let's go back to the docker-compose YAML file.

147
00:07:30,840 --> 00:07:32,970
Because with that, we also ensured

148
00:07:32,970 --> 00:07:36,640
that we added our configuration here as a volume.

149
00:07:36,640 --> 00:07:39,850
And that is it for this server container.

150
00:07:39,850 --> 00:07:42,670
I am fully aware that this was brand new

151
00:07:42,670 --> 00:07:44,080
and maybe not something

152
00:07:44,080 --> 00:07:47,140
you would feel comfortable building from scratch,

153
00:07:47,140 --> 00:07:49,720
but that is why we're going through that together

154
00:07:49,720 --> 00:07:53,410
so that you see many examples and that you understand

155
00:07:53,410 --> 00:07:57,113
why we, for example built this container like this.

156
00:07:58,340 --> 00:08:01,890
With that though, we got this nginx container built,

157
00:08:01,890 --> 00:08:05,110
and now we can move on to the PHP container

158
00:08:05,110 --> 00:08:06,110
in the next lecture.

