1
00:00:02,240 --> 00:00:05,860
So that's our application running with Docker now

2
00:00:05,860 --> 00:00:07,220
and it works, right?

3
00:00:07,220 --> 00:00:09,590
Everything is great.

4
00:00:09,590 --> 00:00:12,090
Well, here's what's not so great.

5
00:00:12,090 --> 00:00:15,290
First of all, let's stop this application

6
00:00:15,290 --> 00:00:16,460
which you of course can do.

7
00:00:16,460 --> 00:00:18,550
Let's stop this container.

8
00:00:18,550 --> 00:00:23,210
And since I added dash dash RM when I created the container

9
00:00:23,210 --> 00:00:26,293
it is also automatically removed when it's stopped.

10
00:00:28,230 --> 00:00:30,940
Once it's stopped, let's restart it.

11
00:00:30,940 --> 00:00:33,380
And since I did add dash dash RM

12
00:00:33,380 --> 00:00:37,370
we have to use Docker run again to create a new container

13
00:00:37,370 --> 00:00:41,023
because thanks to RM the previous container was deleted.

14
00:00:42,150 --> 00:00:45,970
Now let's run this container without dash dash RM.

15
00:00:45,970 --> 00:00:50,680
So it's a new container again, with the name feedback app

16
00:00:50,680 --> 00:00:54,350
and therefore, I can of course reload the app and it works.

17
00:00:54,350 --> 00:00:59,150
But you'll notice that if I try to view feedbackawesome.txt,

18
00:00:59,150 --> 00:01:00,890
this file doesn't exist.

19
00:01:00,890 --> 00:01:02,993
It fails to get this file.

20
00:01:03,850 --> 00:01:06,980
And of course, this worked a couple of minutes ago.

21
00:01:06,980 --> 00:01:08,880
Now of course, that's the case

22
00:01:08,880 --> 00:01:11,000
because we deleted the container

23
00:01:11,000 --> 00:01:13,533
when we stopped it the first time.

24
00:01:15,140 --> 00:01:18,790
If I for example, here, readd this,

25
00:01:18,790 --> 00:01:22,820
added again in the newly started container,

26
00:01:22,820 --> 00:01:25,223
we can view it here.

27
00:01:27,140 --> 00:01:31,270
And if I now stop this container, feedback app

28
00:01:31,270 --> 00:01:34,430
and keep in mind that now it will not be removed

29
00:01:34,430 --> 00:01:37,860
because we did not add dash dash RM

30
00:01:37,860 --> 00:01:39,690
on the Docker run command.

31
00:01:39,690 --> 00:01:43,130
So if I now stop it without the container being removed

32
00:01:43,130 --> 00:01:46,020
and I restart it thereafter,

33
00:01:46,020 --> 00:01:47,980
you will notice that now if I reload,

34
00:01:47,980 --> 00:01:50,600
the file is still there.

35
00:01:50,600 --> 00:01:54,620
So the file was not lost because we stopped the container

36
00:01:54,620 --> 00:01:57,280
but because we removed the container.

37
00:01:57,280 --> 00:02:00,370
And I guess that makes a lot of sense.

38
00:02:00,370 --> 00:02:04,330
I talked about this isolation concept in great depth

39
00:02:04,330 --> 00:02:07,800
and I explained depth with the Docker file we build an image

40
00:02:07,800 --> 00:02:11,460
and that image has its own internal file system

41
00:02:11,460 --> 00:02:15,520
which is detached from the hosting machine file system.

42
00:02:15,520 --> 00:02:20,020
And when we then launch Docker container based on the image,

43
00:02:20,020 --> 00:02:24,310
that container is added as a extra thin read-write layer

44
00:02:24,310 --> 00:02:25,890
on top of this image.

45
00:02:25,890 --> 00:02:29,270
So it basically has access to this image file system

46
00:02:29,270 --> 00:02:31,680
and it's able to read and write there

47
00:02:31,680 --> 00:02:33,820
without manipulating the image though.

48
00:02:33,820 --> 00:02:35,710
But it's able to read and write there.

49
00:02:35,710 --> 00:02:38,430
It has its own copy of that you could say

50
00:02:38,430 --> 00:02:41,030
managed in a very efficient way though.

51
00:02:41,030 --> 00:02:44,470
So that makes all sense that we can read and write in there.

52
00:02:44,470 --> 00:02:45,940
But the problem is,

53
00:02:45,940 --> 00:02:48,960
that the file system is inside of the container.

54
00:02:48,960 --> 00:02:52,830
So if we stop it and restart it everything is fine

55
00:02:52,830 --> 00:02:54,770
because the container never changed.

56
00:02:54,770 --> 00:02:57,670
And just because it was stopped does not mean

57
00:02:57,670 --> 00:03:01,050
that its file system is cleared or erased.

58
00:03:01,050 --> 00:03:02,230
If that would be the case

59
00:03:02,230 --> 00:03:05,210
our application code would be gone as well, right?

60
00:03:05,210 --> 00:03:07,460
So that's not happening.

61
00:03:07,460 --> 00:03:10,930
But if we remove a container, then that's different.

62
00:03:10,930 --> 00:03:13,700
Then all the data in the container is cleared

63
00:03:13,700 --> 00:03:17,250
because the container overall is cleared and deleted.

64
00:03:17,250 --> 00:03:20,230
And if we then run a new container,

65
00:03:20,230 --> 00:03:22,510
even if it's spaced on the same image,

66
00:03:22,510 --> 00:03:25,170
all the data that was created and stored

67
00:03:25,170 --> 00:03:27,660
in the previous container is lost,

68
00:03:27,660 --> 00:03:30,800
because the image is read-only.

69
00:03:30,800 --> 00:03:34,620
So the container, when a file is generated in the container

70
00:03:34,620 --> 00:03:37,540
does not write this file into the image.

71
00:03:37,540 --> 00:03:40,620
It writes it in its own read-write layer

72
00:03:40,620 --> 00:03:42,170
which is added on top.

73
00:03:42,170 --> 00:03:44,660
Therefore, if the container is removed

74
00:03:44,660 --> 00:03:48,180
we're just left with the image which was never changed.

75
00:03:48,180 --> 00:03:50,620
So when we then start a new container

76
00:03:50,620 --> 00:03:54,600
well, that starts with the same basic file system again

77
00:03:54,600 --> 00:03:57,500
without the changes made by the previous container

78
00:03:57,500 --> 00:04:00,670
which might've been executed on the same image.

79
00:04:00,670 --> 00:04:04,110
And that's a core idea of Docker, multiple containers

80
00:04:04,110 --> 00:04:07,770
based on the same image are totally isolated

81
00:04:07,770 --> 00:04:08,980
from each other.

82
00:04:08,980 --> 00:04:10,990
But here of course, that's a problem.

83
00:04:10,990 --> 00:04:13,570
It means that we lose this text file

84
00:04:13,570 --> 00:04:18,029
if we stop the container and remove it thereafter.

85
00:04:18,029 --> 00:04:22,019
And in many applications, this behavior is a problem

86
00:04:22,019 --> 00:04:23,550
because here, for example,

87
00:04:23,550 --> 00:04:26,860
we wanna keep those feedback text files around

88
00:04:26,860 --> 00:04:29,100
even if a container is deleted.

89
00:04:29,100 --> 00:04:32,420
We would have a comparable scenario if we would be dealing

90
00:04:32,420 --> 00:04:36,600
with user accounts or with product data submitted by users

91
00:04:36,600 --> 00:04:38,770
or any other kind of data

92
00:04:38,770 --> 00:04:41,690
which shouldn't be disappearing suddenly

93
00:04:41,690 --> 00:04:46,000
but which instead should survive containers being deleted.

94
00:04:46,000 --> 00:04:47,810
Because in reality, you're going

95
00:04:47,810 --> 00:04:49,970
to remove containers quite a bit.

96
00:04:49,970 --> 00:04:52,080
If we change something about our code

97
00:04:52,080 --> 00:04:55,130
and be built a new image and start a new container

98
00:04:55,130 --> 00:04:57,400
will not restart the old container.

99
00:04:57,400 --> 00:04:59,030
We wanna use the new container

100
00:04:59,030 --> 00:05:01,860
which uses the latest code snapshot.

101
00:05:01,860 --> 00:05:05,350
So therefore, in that case, we would be losing all the data.

102
00:05:05,350 --> 00:05:08,010
And that's exactly the problem I've been describing

103
00:05:08,010 --> 00:05:09,750
over the last minutes.

104
00:05:09,750 --> 00:05:12,500
So now that we know the problem really well

105
00:05:12,500 --> 00:05:16,430
and that we know that containers are able to write data

106
00:05:16,430 --> 00:05:20,140
but that this data is lost when the container is removed

107
00:05:20,140 --> 00:05:22,270
now that we know all of that,

108
00:05:22,270 --> 00:05:23,603
what is the solution?

