1
00:00:02,170 --> 00:00:05,331
So how can we now add such a volume

2
00:00:05,331 --> 00:00:07,343
to our container?

3
00:00:08,290 --> 00:00:11,180
One of the easiest ways of adding a volume

4
00:00:11,180 --> 00:00:13,370
is that we add a special instruction

5
00:00:13,370 --> 00:00:16,120
to the Docker file that creates the image

6
00:00:16,120 --> 00:00:19,240
which we'll later use to create a container.

7
00:00:19,240 --> 00:00:22,500
There we can add the volume instruction.

8
00:00:22,500 --> 00:00:25,190
And this takes an array full of texts,

9
00:00:25,190 --> 00:00:26,980
full of strings and quotes,

10
00:00:26,980 --> 00:00:30,840
where we can specify the different paths

11
00:00:30,840 --> 00:00:33,230
inside of the container file system.

12
00:00:33,230 --> 00:00:34,900
So, which are going to get used

13
00:00:34,900 --> 00:00:39,370
inside of our application code, which we wanna persist.

14
00:00:39,370 --> 00:00:41,525
So in our example, for example

15
00:00:41,525 --> 00:00:46,525
I am saving my permanent files to the feedback folder.

16
00:00:47,520 --> 00:00:50,280
I'm also saving files to the temp folder

17
00:00:50,280 --> 00:00:54,030
but as the name implies, this folder is only temporary.

18
00:00:54,030 --> 00:00:56,920
It does not need to be saved.

19
00:00:56,920 --> 00:01:00,860
But everything in the feedback folder should be saved.

20
00:01:00,860 --> 00:01:02,720
So in my application code,

21
00:01:02,720 --> 00:01:04,940
I'm writing to the feedback folder

22
00:01:04,940 --> 00:01:08,670
which of course in the end is inside of the app folder

23
00:01:08,670 --> 00:01:11,200
since that's our general working directory

24
00:01:11,200 --> 00:01:14,300
where we copied the entire application into.

25
00:01:14,300 --> 00:01:16,300
And therefore the volume

26
00:01:16,300 --> 00:01:21,300
which I wanna save is /app/feedback.

27
00:01:21,660 --> 00:01:25,480
That is the path inside of my container

28
00:01:25,480 --> 00:01:28,290
and that's important, inside of my container,

29
00:01:28,290 --> 00:01:32,440
which should be mapped to some folder

30
00:01:32,440 --> 00:01:34,010
outside of the container

31
00:01:34,010 --> 00:01:37,143
and where data should therefore survive.

32
00:01:38,180 --> 00:01:39,790
Now you might be wondering

33
00:01:39,790 --> 00:01:43,390
how can we control to which folder

34
00:01:43,390 --> 00:01:45,880
on our hosting machine does this map?

35
00:01:45,880 --> 00:01:47,430
I will come back to this later.

36
00:01:47,430 --> 00:01:49,750
At the moment we don't control this.

37
00:01:49,750 --> 00:01:52,060
We let docker control this

38
00:01:52,060 --> 00:01:55,080
and I'll come back to why this makes sense.

39
00:01:55,080 --> 00:01:56,820
So if I saved this ,

40
00:01:56,820 --> 00:01:59,170
this Docker file with this instruction added

41
00:02:00,070 --> 00:02:04,290
we can rebuild this image with docker build.

42
00:02:04,290 --> 00:02:08,400
Now that we added this volume instruction, docker build.

43
00:02:08,400 --> 00:02:11,300
and of course, maybe give it again, a tag

44
00:02:11,300 --> 00:02:15,420
and I will name this feedback-node

45
00:02:17,330 --> 00:02:19,550
and give it a tag of volumes.

46
00:02:19,550 --> 00:02:22,440
We already have feedback-node latest.

47
00:02:22,440 --> 00:02:26,550
Now I give it a special volumes edition, you could say,

48
00:02:26,550 --> 00:02:28,740
which is the same image as before,

49
00:02:28,740 --> 00:02:31,143
but with this extra volume feature.

50
00:02:32,400 --> 00:02:36,510
Hit enter and now this image is created and tagged.

51
00:02:36,510 --> 00:02:40,860
And now we can run a container based on this image.

52
00:02:40,860 --> 00:02:45,790
So docker run feedback-node:volumes

53
00:02:45,790 --> 00:02:48,340
So using that tag, which I just assigned

54
00:02:48,340 --> 00:02:50,560
and I'll run it in detached mode,

55
00:02:50,560 --> 00:02:52,750
I'll publish the internal port 80

56
00:02:52,750 --> 00:02:55,460
to my host port 3000.

57
00:02:55,460 --> 00:03:00,460
And we can also add the --rm flect to remove that container

58
00:03:00,690 --> 00:03:04,290
if we stop it, because thanks to volumes,

59
00:03:04,290 --> 00:03:07,480
this should now not be a problem anymore.

60
00:03:07,480 --> 00:03:11,133
We can also sign a name and I'll name it, feedback-app.

61
00:03:12,920 --> 00:03:14,986
If I enter, I get an error because I already have this app.

62
00:03:14,986 --> 00:03:19,710
I already have this container.

63
00:03:19,710 --> 00:03:21,760
It's actually even a running container.

64
00:03:21,760 --> 00:03:26,280
So I will stop this running container here.

65
00:03:26,280 --> 00:03:28,950
And then also remove it thereafter

66
00:03:28,950 --> 00:03:32,393
since that was not a container created with --rm.

67
00:03:33,470 --> 00:03:37,440
So I'll run docker rm feedback-app.

68
00:03:37,440 --> 00:03:38,840
And now with that removed

69
00:03:38,840 --> 00:03:43,620
now we can restart this container based on this new image.

70
00:03:43,620 --> 00:03:46,763
If I now hit enter, this is up and running.

71
00:03:48,240 --> 00:03:51,660
Therefore, of course, we can go back to local host 3000

72
00:03:52,700 --> 00:03:57,167
add this feedback one more time, save this

73
00:03:58,930 --> 00:04:01,560
and now we have a problem.

74
00:04:01,560 --> 00:04:05,170
Now this starts spinning and it crashes.

75
00:04:05,170 --> 00:04:07,840
So clearly something went wrong.

76
00:04:07,840 --> 00:04:10,780
And in order to see what went wrong,

77
00:04:10,780 --> 00:04:14,310
I'll run docker logs feedback-app

78
00:04:14,310 --> 00:04:17,149
to look into the output of this container.

79
00:04:17,149 --> 00:04:19,183
And we see that we got an error here,

80
00:04:19,183 --> 00:04:21,620
unhandled rejection warning.

81
00:04:21,620 --> 00:04:24,560
And the problem here seems to be

82
00:04:24,560 --> 00:04:27,630
that cross-device link is not permitted.

83
00:04:27,630 --> 00:04:30,630
So when we try to move the awesome.txt file

84
00:04:30,630 --> 00:04:35,400
from the temp folder to the feedback folder that crashes.

85
00:04:35,400 --> 00:04:36,980
And that must have something to do

86
00:04:36,980 --> 00:04:39,170
with our recently added volume

87
00:04:39,170 --> 00:04:40,980
because it worked before.

88
00:04:40,980 --> 00:04:43,840
Now this error, which we're facing

89
00:04:43,840 --> 00:04:47,780
is stemming from this specific code from the rename method

90
00:04:47,780 --> 00:04:50,350
which I'm using here in node to be precise.

91
00:04:50,350 --> 00:04:52,000
Now this is no node course

92
00:04:52,000 --> 00:04:54,600
so I won't bore you with the details.

93
00:04:54,600 --> 00:04:57,820
But in the end, the rename method doesn't work

94
00:04:57,820 --> 00:05:02,140
if the file gets moved across multiple devices.

95
00:05:02,140 --> 00:05:05,610
And thanks to the volume which we specified here

96
00:05:05,610 --> 00:05:08,600
this is kind of what's happening under the hood.

97
00:05:08,600 --> 00:05:12,440
Docker indeed will not just move the file

98
00:05:12,440 --> 00:05:16,500
to some other folder inside of the container file system

99
00:05:16,500 --> 00:05:19,120
but it moves it kind of out of the container.

100
00:05:19,120 --> 00:05:21,580
And it's this specific rename method

101
00:05:21,580 --> 00:05:24,200
which doesn't like things like this.

102
00:05:24,200 --> 00:05:26,490
Now the work around is simple.

103
00:05:26,490 --> 00:05:31,490
You can replace the rename method with the copyFile method

104
00:05:34,440 --> 00:05:37,370
and there after simply add a new line

105
00:05:37,370 --> 00:05:41,810
where you call fs.unlink and point at the tempFilePath

106
00:05:44,270 --> 00:05:46,540
and add await in front of that.

107
00:05:46,540 --> 00:05:47,653
That's all.

108
00:05:48,500 --> 00:05:50,240
This essentially will copy the file

109
00:05:50,240 --> 00:05:53,050
and manually delete it thereafter.

110
00:05:53,050 --> 00:05:55,610
If you change this, you should be good.

111
00:05:55,610 --> 00:05:58,020
Now of course, we need to rebuild our image

112
00:05:58,020 --> 00:05:59,810
because we changed the source code

113
00:05:59,810 --> 00:06:02,580
and that's still not being picked up automatically

114
00:06:02,580 --> 00:06:04,240
at this point.

115
00:06:04,240 --> 00:06:06,700
So to do that, I'll first of all remove the image

116
00:06:06,700 --> 00:06:09,640
with the docker rmi command

117
00:06:09,640 --> 00:06:14,240
and then there it's the feedback-node:volumes tag

118
00:06:14,240 --> 00:06:16,423
which I wanna remove like this.

119
00:06:17,860 --> 00:06:21,400
And then we just need to rerun this docker build command

120
00:06:21,400 --> 00:06:22,860
to rebuild the image.

121
00:06:22,860 --> 00:06:26,520
I'll use the same tag as before, since I just removed it.

122
00:06:26,520 --> 00:06:31,520
And once this was rebuilt, we can run this container again

123
00:06:31,910 --> 00:06:35,810
still using this image which we just rebuilt

124
00:06:35,810 --> 00:06:38,453
and removing the container if it shuts down.

125
00:06:39,890 --> 00:06:44,130
And if you now back to local hosts 3000/

126
00:06:44,130 --> 00:06:48,750
and you try this again, now this works.

127
00:06:48,750 --> 00:06:51,140
And now if you access feedback/

128
00:06:51,140 --> 00:06:55,703
whatever title you picked .txt, you see your file here.

129
00:06:56,550 --> 00:06:59,345
And now hopefully the file is still there

130
00:06:59,345 --> 00:07:03,970
if we stop and remove this container and restart it.

131
00:07:03,970 --> 00:07:08,403
So let's go back and let's stop this feedback app container.

132
00:07:10,760 --> 00:07:14,510
And once it is stopped, let's rerun it

133
00:07:14,510 --> 00:07:16,600
by running the docker run command again.

134
00:07:16,600 --> 00:07:19,570
And this will of course create a brand new container based

135
00:07:19,570 --> 00:07:21,940
on the same image as before.

136
00:07:21,940 --> 00:07:25,480
And if we do that, then go back and reload

137
00:07:25,480 --> 00:07:29,710
well, no, this file is still not there.

138
00:07:29,710 --> 00:07:32,660
So what's up with this volumes thing.

139
00:07:32,660 --> 00:07:35,230
Why am I teaching you about volumes

140
00:07:35,230 --> 00:07:38,870
if we just add an instruction that doesn't do anything

141
00:07:38,870 --> 00:07:41,020
except for break our code?

142
00:07:41,020 --> 00:07:42,370
What's the idea behind that?

143
00:07:42,370 --> 00:07:43,633
Why is it not working?

