1
00:00:02,040 --> 00:00:03,390
So let's get started.

2
00:00:03,390 --> 00:00:06,290
We get these three building blocks the database,

3
00:00:06,290 --> 00:00:08,770
the backend API node server

4
00:00:08,770 --> 00:00:11,253
and this frontend react application.

5
00:00:12,240 --> 00:00:16,850
Now, I will start with the MongoDB database.

6
00:00:16,850 --> 00:00:21,240
For that I shut down my locally installed MongoDB database,

7
00:00:21,240 --> 00:00:23,330
which was running off screen.

8
00:00:23,330 --> 00:00:25,790
And I will open a new terminal here.

9
00:00:25,790 --> 00:00:30,470
I still have my node and my react application running.

10
00:00:30,470 --> 00:00:31,770
So that still works.

11
00:00:31,770 --> 00:00:33,990
Node and react is still running.

12
00:00:33,990 --> 00:00:36,190
But now I opened a new terminal

13
00:00:36,190 --> 00:00:39,890
where I wanna spin up a container for MongoDB

14
00:00:39,890 --> 00:00:44,163
to which we then can talk from our backend node API here.

15
00:00:45,750 --> 00:00:46,583
And for this,

16
00:00:46,583 --> 00:00:49,080
I'm going to use the official Mongo image again

17
00:00:49,080 --> 00:00:50,950
which you find on Docker Hub.

18
00:00:50,950 --> 00:00:54,173
And I already mentioned that we learn how we can use it

19
00:00:54,173 --> 00:00:57,080
in the documentation here.

20
00:00:57,080 --> 00:01:01,960
Now we can start by simply running docker run mongo.

21
00:01:01,960 --> 00:01:03,460
This image is available

22
00:01:03,460 --> 00:01:07,250
since it's an official Docker Hub registered image.

23
00:01:07,250 --> 00:01:10,650
And this would spin up a container based on this image.

24
00:01:10,650 --> 00:01:15,570
Now I will give my container a name, mongodb maybe.

25
00:01:15,570 --> 00:01:17,130
And I will also ensure

26
00:01:17,130 --> 00:01:19,940
that it's removed automatically if it's stopped.

27
00:01:19,940 --> 00:01:21,863
And I will run it in detached mode.

28
00:01:23,130 --> 00:01:25,070
Now, that's the most basic form

29
00:01:25,070 --> 00:01:27,240
of starting this MongoDB container.

30
00:01:27,240 --> 00:01:31,242
And that's essentially what we also did in the last module.

31
00:01:31,242 --> 00:01:33,400
There, we also added a network.

32
00:01:33,400 --> 00:01:35,460
But I'll come back to that later.

33
00:01:35,460 --> 00:01:37,740
Now, this would start this database.

34
00:01:37,740 --> 00:01:40,780
But especially if my backend here

35
00:01:40,780 --> 00:01:43,330
and my frontend is not dockerized yet,

36
00:01:43,330 --> 00:01:45,910
I wanna do one additional thing here.

37
00:01:45,910 --> 00:01:49,610
I wanna publish one of the ports made available

38
00:01:49,610 --> 00:01:51,880
by this Mongo image.

39
00:01:51,880 --> 00:01:55,960
Because as long as the backend API is not dockerized,

40
00:01:55,960 --> 00:01:58,930
this API, this node API,

41
00:01:58,930 --> 00:02:03,370
still talks to the database here when it connects

42
00:02:03,370 --> 00:02:06,303
as if it would be running on my local host machine.

43
00:02:07,330 --> 00:02:09,770
Now if we spin up a Docker container

44
00:02:09,770 --> 00:02:11,260
containing the database,

45
00:02:11,260 --> 00:02:15,460
for this to work, I need to expose this port 27017

46
00:02:16,960 --> 00:02:20,230
from my Docker container to my local machine.

47
00:02:20,230 --> 00:02:23,010
So that our services can connect to it

48
00:02:23,010 --> 00:02:25,430
through my local machine.

49
00:02:25,430 --> 00:02:28,610
And that's something we can do on this Mongo image.

50
00:02:28,610 --> 00:02:31,840
Actually, if you search for 27017

51
00:02:31,840 --> 00:02:34,170
which is the default MongoDB port,

52
00:02:34,170 --> 00:02:38,390
you'll see that this image actually in the end

53
00:02:38,390 --> 00:02:40,710
exposes this port.

54
00:02:40,710 --> 00:02:43,360
So what we can do without any issues here,

55
00:02:43,360 --> 00:02:47,680
is we can expose this port on the same port

56
00:02:47,680 --> 00:02:49,333
on our local host machine.

57
00:02:50,340 --> 00:02:51,173
And with that,

58
00:02:51,173 --> 00:02:54,350
we can ensure that MongoDB runs in a container.

59
00:02:54,350 --> 00:02:57,580
But our locally running backend,

60
00:02:57,580 --> 00:03:00,010
which currently is not dockerized yet,

61
00:03:00,010 --> 00:03:02,730
will still be able to talk to it.

62
00:03:02,730 --> 00:03:04,420
So that's the command I wanna run.

63
00:03:04,420 --> 00:03:06,090
To ensure that it works,

64
00:03:06,090 --> 00:03:09,110
I'll first of all run docker container prune

65
00:03:09,110 --> 00:03:11,610
to remove all stopped containers

66
00:03:11,610 --> 00:03:14,530
to ensure I don't have any old MongoDB containers

67
00:03:14,530 --> 00:03:16,260
lying around.

68
00:03:16,260 --> 00:03:18,740
And thereafter, I'll run this command.

69
00:03:18,740 --> 00:03:20,070
And with docker ps,

70
00:03:20,070 --> 00:03:23,383
we there after see this container to be up and running.

71
00:03:24,690 --> 00:03:28,860
And now I will go back to my other terminal

72
00:03:28,860 --> 00:03:31,290
where the node API is running.

73
00:03:31,290 --> 00:03:34,860
With control+ C, we can stop this or I can stop this here.

74
00:03:34,860 --> 00:03:36,700
You probably don't have it up and running.

75
00:03:36,700 --> 00:03:38,160
But I can stop this here.

76
00:03:38,160 --> 00:03:41,040
We're soon going to dockerize it as well.

77
00:03:41,040 --> 00:03:44,060
And I can then restart it with node app.js.

78
00:03:44,060 --> 00:03:46,920
And it successfully connects to MongoDB,

79
00:03:46,920 --> 00:03:51,010
even though I shut down my locally installed MongoDB.

80
00:03:51,010 --> 00:03:54,120
And that means that indeed, it must have connected

81
00:03:54,120 --> 00:03:57,180
to this MongoDB container we started here.

82
00:03:57,180 --> 00:03:59,140
And we can have a look there

83
00:03:59,140 --> 00:04:02,790
by looking into the logs of this MongoDB container.

84
00:04:02,790 --> 00:04:05,490
And this is quite hard to read here for sure.

85
00:04:05,490 --> 00:04:09,020
But actually, if we read through this,

86
00:04:09,020 --> 00:04:13,830
well, huge amount of lines here, we see that something

87
00:04:13,830 --> 00:04:16,279
based on the (indistinct) has connected here.

88
00:04:16,279 --> 00:04:18,913
And that's indeed my local node application.

89
00:04:19,890 --> 00:04:24,403
So that's working. We now dockerized does MongoDB database.

