1
00:00:02,270 --> 00:00:04,050
So if we consider this error

2
00:00:04,050 --> 00:00:06,970
that was thrown, what exactly could be the problem

3
00:00:06,970 --> 00:00:10,020
with this newly added bind mount here?

4
00:00:10,020 --> 00:00:13,700
Well, keep in mind that we're binding this folder,

5
00:00:13,700 --> 00:00:17,110
everything in that folder to the app folder.

6
00:00:17,110 --> 00:00:19,600
Now isn't the end kind of means, of course,

7
00:00:19,600 --> 00:00:22,680
that we overwrite the app folder inside

8
00:00:22,680 --> 00:00:25,732
of the container with our local folder, right.

9
00:00:25,732 --> 00:00:30,732
Now we copy everything into that folder, initially here when

10
00:00:31,050 --> 00:00:34,420
the image is created, and we install all dependencies,

11
00:00:34,420 --> 00:00:37,660
but in the end we render all these steps,

12
00:00:37,660 --> 00:00:41,410
which we performed during image creation worthless,

13
00:00:41,410 --> 00:00:44,280
if we then blind this mount to the container,

14
00:00:44,280 --> 00:00:47,640
because we overwrite everything in the app folder anyways

15
00:00:47,640 --> 00:00:49,950
with our local folder.

16
00:00:49,950 --> 00:00:52,650
And this local folder doesn't have

17
00:00:52,650 --> 00:00:54,290
the node modules folder

18
00:00:54,290 --> 00:00:56,600
with all the dependencies this app needs,

19
00:00:56,600 --> 00:00:59,110
and that's the reason for this error we're getting.

20
00:00:59,110 --> 00:01:02,340
The server JS file needs the express package,

21
00:01:02,340 --> 00:01:05,930
the express dependency, and it exists in a container,

22
00:01:05,930 --> 00:01:10,330
because of npm install, it does not exist in my local setup,

23
00:01:10,330 --> 00:01:13,860
because I never ran npm install there.

24
00:01:13,860 --> 00:01:15,990
And since I mount my local folder

25
00:01:15,990 --> 00:01:18,880
to the app folder, we overwrite all

26
00:01:18,880 --> 00:01:21,240
the work we did here when we set up

27
00:01:21,240 --> 00:01:23,450
the image and therefore also the container.

28
00:01:23,450 --> 00:01:26,590
We overwrite this with our local folder,

29
00:01:26,590 --> 00:01:28,530
and that is the problem here.

30
00:01:28,530 --> 00:01:30,940
Now how can we solve this?

31
00:01:30,940 --> 00:01:33,826
Let's first of all understand how containers interact

32
00:01:33,826 --> 00:01:36,400
with volumes and bind mounts.

33
00:01:36,400 --> 00:01:37,628
If we have a container,

34
00:01:37,628 --> 00:01:40,330
and we don't have a volume, and a bind mount,

35
00:01:40,330 --> 00:01:42,330
we can mount both into the container

36
00:01:42,330 --> 00:01:43,860
with the -V flag,

37
00:01:43,860 --> 00:01:46,590
which I showed you a couple of minutes ago.

38
00:01:46,590 --> 00:01:49,730
That means that some folders inside of the container

39
00:01:49,730 --> 00:01:52,150
are mounted, or are connected

40
00:01:52,150 --> 00:01:54,083
to folders on the host machine.

41
00:01:55,280 --> 00:01:59,490
Now let's say we already had files inside of the container.

42
00:01:59,490 --> 00:02:03,708
In that case, they also now exist in the outside volume,

43
00:02:03,708 --> 00:02:06,610
and if you write a new file, it's also added

44
00:02:06,610 --> 00:02:08,789
in the folder on the host machine.

45
00:02:08,789 --> 00:02:10,880
If the container then stands up,

46
00:02:10,880 --> 00:02:14,360
and it finds files in the volume,

47
00:02:14,360 --> 00:02:17,020
and it doesn't have any internal files yet,

48
00:02:17,020 --> 00:02:19,308
it loads the files from the volume.

49
00:02:19,308 --> 00:02:22,570
That's actually what we utilize with the bind mount.

50
00:02:22,570 --> 00:02:25,040
Here we don't have any files inside

51
00:02:25,040 --> 00:02:26,600
of the container, let's say,

52
00:02:26,600 --> 00:02:29,750
but we have files on the local host machine.

53
00:02:29,750 --> 00:02:32,050
In that case, these files

54
00:02:32,050 --> 00:02:34,933
are basically also usable inside of the container,

55
00:02:35,900 --> 00:02:38,690
but now we have kind of both things happening.

56
00:02:38,690 --> 00:02:42,290
We have files inside of the container in the app folder,

57
00:02:42,290 --> 00:02:43,880
because of these instructions,

58
00:02:43,880 --> 00:02:47,160
and we have files and folders outside of the container

59
00:02:47,160 --> 00:02:50,300
in this folder on our local host machine.

60
00:02:50,300 --> 00:02:51,560
And now the good thing is

61
00:02:51,560 --> 00:02:55,690
that Docker does not start overwriting our local files

62
00:02:55,690 --> 00:02:57,410
on our host machine.

63
00:02:57,410 --> 00:02:58,530
This would be pretty bad,

64
00:02:58,530 --> 00:03:00,330
if Docker would be doing that, right.

65
00:03:00,330 --> 00:03:02,440
We could delete a lot of important things

66
00:03:02,440 --> 00:03:04,610
on our computer by accident.

67
00:03:04,610 --> 00:03:06,180
So that's not what's happening.

68
00:03:06,180 --> 00:03:10,083
Docker will not overwrite our local host folder here.

69
00:03:11,080 --> 00:03:13,800
Instead, here, the local host folder,

70
00:03:13,800 --> 00:03:16,490
and the content in it overwrites what's

71
00:03:16,490 --> 00:03:18,290
in the Docker container,

72
00:03:18,290 --> 00:03:19,420
and that's the problem here,

73
00:03:19,420 --> 00:03:22,870
with that we got rid of node modules and so on.

74
00:03:22,870 --> 00:03:26,972
Now to solve this problem, we kind of need to tell Docker,

75
00:03:26,972 --> 00:03:30,150
that there are certain parts

76
00:03:30,150 --> 00:03:34,350
in its internal file system, which should not be overwritten

77
00:03:34,350 --> 00:03:37,370
from outside in case we have such a clash

78
00:03:37,370 --> 00:03:38,950
as we have it here.

79
00:03:38,950 --> 00:03:43,090
And the debt can be achieved with another volume,

80
00:03:43,090 --> 00:03:46,220
which we add to this Docker container

81
00:03:46,220 --> 00:03:48,790
with an anonymous volume actually.

82
00:03:48,790 --> 00:03:51,250
And this will now also show us a use case,

83
00:03:51,250 --> 00:03:54,423
where anonymous volumes can be helpful.

84
00:03:54,423 --> 00:03:57,364
If we add one more volume with -V,

85
00:03:57,364 --> 00:04:02,364
we can find the app/node modules folder.

86
00:04:03,220 --> 00:04:05,200
And it's an anonymous volume,

87
00:04:05,200 --> 00:04:07,000
which we also can add like this,

88
00:04:07,000 --> 00:04:09,850
and not just in the Docker file.

89
00:04:09,850 --> 00:04:12,860
It's an anonymous volume because it has no name,

90
00:04:12,860 --> 00:04:16,329
it would have a name if we add a colon in front of it,

91
00:04:16,329 --> 00:04:18,420
and assign some name here,

92
00:04:18,420 --> 00:04:21,640
but if we don't do that, it's an anonymous volume.

93
00:04:21,640 --> 00:04:23,850
So this is an anonymous volume,

94
00:04:23,850 --> 00:04:26,120
and adding it like this

95
00:04:26,120 --> 00:04:31,120
is equivalent to adding it like this here.

96
00:04:32,670 --> 00:04:35,510
You could do both, but I'll comment this out

97
00:04:35,510 --> 00:04:39,230
with a hash in front of it and go with this approach,

98
00:04:39,230 --> 00:04:42,750
because I then don't have to rebuild the image.

99
00:04:42,750 --> 00:04:45,290
Now why does this help here?

100
00:04:45,290 --> 00:04:49,080
Well, Docker always evaluates all volumes you

101
00:04:49,080 --> 00:04:50,800
are setting on a container,

102
00:04:50,800 --> 00:04:55,800
and if there are clashes, the longer internal path wins.

103
00:04:56,090 --> 00:04:58,120
So for example here we have a clash,

104
00:04:58,120 --> 00:05:02,820
we have app volume which is bound to something,

105
00:05:02,820 --> 00:05:06,400
and we have an app/node modules volume,

106
00:05:06,400 --> 00:05:08,290
which is also bound to something.

107
00:05:08,290 --> 00:05:09,690
We didn't assign a name,

108
00:05:09,690 --> 00:05:12,490
but keep in mind, even for anonymous volumes,

109
00:05:12,490 --> 00:05:14,290
they are managed by Docker,

110
00:05:14,290 --> 00:05:17,670
and there is some mapped folder somewhere

111
00:05:17,670 --> 00:05:19,360
on the local machine.

112
00:05:19,360 --> 00:05:21,780
It's just cleared when a container is removed,

113
00:05:21,780 --> 00:05:24,370
but there is a folder on the host machine,

114
00:05:24,370 --> 00:05:26,303
even for anonymous modules.

115
00:05:26,303 --> 00:05:30,690
So here, Docker sees that we have some volume mapped

116
00:05:30,690 --> 00:05:32,230
to the app folder,

117
00:05:32,230 --> 00:05:36,030
and some volume to the app/node modules folder.

118
00:05:36,030 --> 00:05:39,070
And in that case, the simple rule Docker has

119
00:05:39,070 --> 00:05:43,620
is that the longer the more specific path wins.

120
00:05:43,620 --> 00:05:46,890
So that means we can still bind to the app folder,

121
00:05:46,890 --> 00:05:50,631
but the node modules folder inside of the app folder,

122
00:05:50,631 --> 00:05:54,210
and the node modules folder is the folder created

123
00:05:54,210 --> 00:05:56,640
by the npm install command by the way.

124
00:05:56,640 --> 00:05:59,610
The node modules folder will survive,

125
00:05:59,610 --> 00:06:02,580
it will overwrite the folder that's coming in

126
00:06:02,580 --> 00:06:05,290
from outside because of this module,

127
00:06:05,290 --> 00:06:08,910
and here, we actually pass in no node modules folder,

128
00:06:08,910 --> 00:06:12,091
and therefore this node modules folder overwrites

129
00:06:12,091 --> 00:06:15,120
the non existent node modules folder,

130
00:06:15,120 --> 00:06:16,919
and hence long story short,

131
00:06:16,919 --> 00:06:20,460
the node modules folder, which was created during

132
00:06:20,460 --> 00:06:24,050
the image creation with npm install will survive,

133
00:06:24,050 --> 00:06:27,070
and will actually co exist together

134
00:06:27,070 --> 00:06:30,470
with the bind mount, which still also works.

135
00:06:30,470 --> 00:06:32,620
It's just a node modules folder,

136
00:06:32,620 --> 00:06:34,853
which is kind of excluded you could say.

137
00:06:36,360 --> 00:06:39,531
And therefore now after this long explanation,

138
00:06:39,531 --> 00:06:44,513
if we stop the currently running container,

139
00:06:45,700 --> 00:06:49,993
and we remove this container,

140
00:06:50,930 --> 00:06:53,410
we can run this container now

141
00:06:53,410 --> 00:06:56,150
with this extra anonymous volume added.

142
00:06:56,150 --> 00:07:00,278
If we want to also again with --RM added,

143
00:07:00,278 --> 00:07:02,467
and now this starts,

144
00:07:02,467 --> 00:07:07,280
and now this app also works again, test this works.

145
00:07:07,280 --> 00:07:12,280
Again, we'll see under feedback, awesome.txt,

146
00:07:13,300 --> 00:07:16,587
that file from earlier is also still there,

147
00:07:16,587 --> 00:07:20,040
but now we actually have one additional benefit.

148
00:07:20,040 --> 00:07:23,170
Now if we change something in our HTML file,

149
00:07:23,170 --> 00:07:26,220
for example, I removed that please text again,

150
00:07:26,220 --> 00:07:30,080
and I save that file, if I now reload we see

151
00:07:30,080 --> 00:07:33,030
that change instantly without rebuilding

152
00:07:33,030 --> 00:07:34,430
the image in between,

153
00:07:34,430 --> 00:07:35,680
and the reason for that

154
00:07:35,680 --> 00:07:38,750
is that now we added this bind mount,

155
00:07:38,750 --> 00:07:41,220
which in this case also only works,

156
00:07:41,220 --> 00:07:44,180
if we add this anonymous module to make sure

157
00:07:44,180 --> 00:07:47,360
that node modules folder doesn't get overwritten

158
00:07:47,360 --> 00:07:49,913
by our bind mount folder content.

