1
00:00:04,150 --> 00:00:11,680
We just verified that after Docker compose down up commands all data inside of the mongo container was

2
00:00:11,680 --> 00:00:12,370
lost.

3
00:00:12,550 --> 00:00:19,090
And the reason for that is that Docker Compose creates a brand new container each time when you enter

4
00:00:19,090 --> 00:00:21,790
Docker, compose down and up commands.

5
00:00:22,240 --> 00:00:30,820
And now let's add volumes mapping for mongo container in order to be able to persist MongoDB data and

6
00:00:30,820 --> 00:00:39,580
any container that is started using Mongo image will utilize the same volume and all MongoDB data therefore

7
00:00:39,580 --> 00:00:47,260
will be persistent and this volume will be created inside of the docker, not on your local computer.

8
00:00:47,590 --> 00:00:48,400
Why?

9
00:00:48,430 --> 00:00:55,450
Because actually we could perform volume mapping similarly as we did, for example, for API service.

10
00:00:55,690 --> 00:01:04,310
Here we mapped a folder that is located on your computer here to the app folder inside of the container.

11
00:01:05,340 --> 00:01:13,650
And actually container is now able not just to read data from the folder when it is running, but also

12
00:01:13,650 --> 00:01:17,970
it is able to write data to the folder if necessary.

13
00:01:18,000 --> 00:01:20,190
It is bidirectional communication.

14
00:01:20,550 --> 00:01:27,930
And actually we are able to now create, for example, here a new folder and name it, for example MongoDB

15
00:01:28,110 --> 00:01:36,300
and afterwards map this volume to the mongo container here using similar approach as we did here.

16
00:01:36,630 --> 00:01:44,730
But in such case, Mongo will actually write actual data to the MongoDB folder on your computer and

17
00:01:44,730 --> 00:01:50,760
it's not really convenient because this is images gallery project folder here you should store only

18
00:01:50,760 --> 00:01:55,030
project related files like JavaScript files, python files and so on.

19
00:01:55,050 --> 00:01:58,990
You should not store here some database information.

20
00:01:59,010 --> 00:02:06,550
That's why such approach is not convenient and in Docker it is possible to create volume inside of the

21
00:02:06,550 --> 00:02:13,990
Docker itself because what Docker is in a nutshell, it is just virtual machine on your macOS or Windows

22
00:02:13,990 --> 00:02:14,860
computer.

23
00:02:14,950 --> 00:02:22,540
And inside of that virtual machine it is possible to create volumes and map those volumes to the containers.

24
00:02:23,020 --> 00:02:30,250
And in such case, those volumes inside of the Docker will be persistent and they will be reusable by

25
00:02:30,250 --> 00:02:31,810
different containers.

26
00:02:31,990 --> 00:02:37,360
Notice that if you are running Linux like system and install Docker there, you actually run Docker

27
00:02:37,360 --> 00:02:43,600
Community Edition, not Docker Desktop and their volumes will be created directly on your computer.

28
00:02:43,600 --> 00:02:50,590
But for Windows and macOS users, Docker is just virtual machine and you could create volumes inside

29
00:02:50,590 --> 00:02:54,640
of that virtual machine and reuse them inside of the containers.

30
00:02:54,640 --> 00:02:57,340
And that's what we are about to do right now.

31
00:02:57,610 --> 00:03:04,720
Let's create new volume and for that you need to add instructions here at the root of the Yaml file

32
00:03:04,720 --> 00:03:07,840
and instruction is volumes like that.

33
00:03:07,930 --> 00:03:16,300
And here let's add new volume named for example MongoDB underscore data and make sure to add here colon

34
00:03:16,300 --> 00:03:19,720
afterwards with such syntax in place.

35
00:03:19,750 --> 00:03:27,040
Docker compose will create new volume called MongoDB underscore data inside of the Docker.

36
00:03:27,490 --> 00:03:35,740
And now we are able to use such volume and map it to containers and let's map it to mongo container

37
00:03:35,740 --> 00:03:36,310
here.

38
00:03:36,730 --> 00:03:41,530
And syntax is similar as we saw here volumes.

39
00:03:42,150 --> 00:03:43,170
Like that.

40
00:03:43,320 --> 00:03:46,260
And on the next line will be minus sign.

41
00:03:46,260 --> 00:03:53,370
And here are type simply name of the volume you want to map and it is MongoDB data.

42
00:03:53,610 --> 00:03:57,900
And note this suggestion from vs code MongoDB underscore data.

43
00:03:58,830 --> 00:04:05,400
And notice that there was no dot or forward slash here in front of the name of the volume, because

44
00:04:05,400 --> 00:04:12,720
in such way Docker will understand that you want to map internal Docker volume called MongoDB data volume

45
00:04:12,720 --> 00:04:14,280
that was created here.

46
00:04:15,210 --> 00:04:22,350
If you add here dot and forward slash, it will mean for Docker that you want to map volume on your

47
00:04:22,350 --> 00:04:28,590
local computer and it will try to find folder MongoDB underscore data in your project.

48
00:04:28,620 --> 00:04:34,890
Similarly, as it finds a folder or front end folder for corresponding containers.

49
00:04:35,160 --> 00:04:37,050
That's why you need to type here.

50
00:04:37,050 --> 00:04:39,670
Simply MongoDB underscore data.

51
00:04:39,690 --> 00:04:46,920
If you want to map volume from the Docker itself and here after colon, we need to specify destination

52
00:04:46,920 --> 00:04:54,420
folder inside of the mongo container and all data inside of the mongo is stored in following path.

53
00:04:54,450 --> 00:04:56,310
Data slash debe.

54
00:04:56,670 --> 00:05:03,540
It is default database path inside of the mongo server and that's actually it.

55
00:05:03,570 --> 00:05:06,810
Let's save changes and quickly summarize here.

56
00:05:06,810 --> 00:05:13,530
We created custom volume inside of the docker and this volume is called MongoDB Underscore data.

57
00:05:13,560 --> 00:05:17,050
You could give other name to this volume if you want to do so.

58
00:05:17,050 --> 00:05:20,910
And afterwards we map such custom volume.

59
00:05:20,920 --> 00:05:30,760
MongoDB underscore data to the data DB folder inside of the mongo container and in such case, any container

60
00:05:30,760 --> 00:05:37,810
that will be started by Docker compose will actually be connected to MongoDB, underscore data volume

61
00:05:37,810 --> 00:05:45,070
and any data that is created by one container inside of this volume will be reusable by other containers

62
00:05:45,070 --> 00:05:47,300
that are connected to the same volume.

63
00:05:47,320 --> 00:05:51,790
That's how we will persist data in the Mongo database.

64
00:05:51,880 --> 00:05:57,250
And now let's of course verify that on practice and let's proceed with that after the small pause.

