1
00:00:02,160 --> 00:00:04,090
So to put this into a container,

2
00:00:04,090 --> 00:00:06,890
I will first of all quit my running service here,

3
00:00:06,890 --> 00:00:09,570
which ran locally on my host machine.

4
00:00:09,570 --> 00:00:12,570
I can delete package-lock JSON and Node modules,

5
00:00:12,570 --> 00:00:14,900
because I don't need them locally anymore.

6
00:00:14,900 --> 00:00:17,330
Instead, we're going to dockerize this.

7
00:00:17,330 --> 00:00:19,820
And I already have a Docker file here,

8
00:00:19,820 --> 00:00:22,150
it was also part of the attachment.

9
00:00:22,150 --> 00:00:25,400
It's a straightforward Docker file for dockerizing

10
00:00:25,400 --> 00:00:27,400
this as a Node application,

11
00:00:27,400 --> 00:00:31,420
and it does what we did many times before in this course.

12
00:00:31,420 --> 00:00:35,910
Hence, we can go ahead and just run docker build

13
00:00:35,910 --> 00:00:37,250
and build this Docker file.

14
00:00:37,250 --> 00:00:39,500
And maybe also add a name and a tag,

15
00:00:39,500 --> 00:00:43,380
and I'm going to name it favorites-node.

16
00:00:43,380 --> 00:00:44,480
And I could add a tag,

17
00:00:44,480 --> 00:00:46,500
but I will go with the latest tag

18
00:00:46,500 --> 00:00:47,963
which is automatically added.

19
00:00:49,240 --> 00:00:51,790
So with that, that's built,

20
00:00:51,790 --> 00:00:52,910
this image is built.

21
00:00:52,910 --> 00:00:55,520
And now we can run a container based on it.

22
00:00:55,520 --> 00:00:59,990
So we can use docker run to run a container

23
00:00:59,990 --> 00:01:02,250
on this favorites-node image.

24
00:01:02,250 --> 00:01:05,560
And I'll give this container a name of favorites.

25
00:01:05,560 --> 00:01:07,780
I'll run it in detached mode and remove it

26
00:01:07,780 --> 00:01:09,113
when it shuts down.

27
00:01:10,030 --> 00:01:12,810
We definitely need to publish a port,

28
00:01:12,810 --> 00:01:17,810
because I do work with a port in there with localhost 3000,

29
00:01:19,560 --> 00:01:22,120
or I'm using the 3000 port.

30
00:01:22,120 --> 00:01:24,670
And we could use environment variables for that,

31
00:01:24,670 --> 00:01:27,170
but I'm keeping it simple here, it's hard coded.

32
00:01:27,170 --> 00:01:31,300
And therefore I will expose the 3000 port

33
00:01:31,300 --> 00:01:34,053
on 3000 thousand port of our local machine.

34
00:01:35,040 --> 00:01:38,220
Now I won't add any volume or bind mount here.

35
00:01:38,220 --> 00:01:41,640
We could definitely add our code here as a bind mount,

36
00:01:41,640 --> 00:01:44,310
but I wanna keep this instruction simple here

37
00:01:44,310 --> 00:01:45,300
in the terminal.

38
00:01:45,300 --> 00:01:48,670
And we need no volume because this application

39
00:01:48,670 --> 00:01:51,250
does not write anything to any file

40
00:01:51,250 --> 00:01:52,960
which would need to survive

41
00:01:52,960 --> 00:01:55,210
a container shut down and removal.

42
00:01:55,210 --> 00:01:57,840
There is nothing which needs to survive that

43
00:01:57,840 --> 00:02:00,710
except for the data stored in the database,

44
00:02:00,710 --> 00:02:04,050
but that database is not part of this container.

45
00:02:04,050 --> 00:02:05,860
And I really wanna emphasize this,

46
00:02:05,860 --> 00:02:08,639
this MongoDB database which I'm connecting here

47
00:02:08,639 --> 00:02:10,550
is not part of the container.

48
00:02:10,550 --> 00:02:12,990
Sure, my Node code is part of it,

49
00:02:12,990 --> 00:02:17,180
and therefore, also this code where I try to connect,

50
00:02:17,180 --> 00:02:19,680
but the installation of MongoDB

51
00:02:19,680 --> 00:02:22,000
is not part of this container.

52
00:02:22,000 --> 00:02:25,680
We have no code in the Docker file to install MongoDB

53
00:02:25,680 --> 00:02:27,020
or anything like that.

54
00:02:27,020 --> 00:02:29,640
So therefore, I really wanna emphasize this,

55
00:02:29,640 --> 00:02:32,430
this container only contains the Node app

56
00:02:32,430 --> 00:02:34,290
and all the code that belongs to it,

57
00:02:34,290 --> 00:02:37,303
it does not contain the MongoDB database.

58
00:02:38,780 --> 00:02:42,070
So therefore, if we now run this, let's see what we get.

59
00:02:42,070 --> 00:02:45,510
If I hit Enter, this of course works.

60
00:02:45,510 --> 00:02:47,790
But if I now have a look at my running containers

61
00:02:47,790 --> 00:02:51,483
with docker ps, I see no containers there.

62
00:02:53,720 --> 00:02:55,750
And of course for stopped containers,

63
00:02:55,750 --> 00:02:57,070
we also don't see it,

64
00:02:57,070 --> 00:03:01,203
because we started this container with the --rm flag.

65
00:03:02,200 --> 00:03:05,540
So to see what's wrong, let's again start it

66
00:03:05,540 --> 00:03:08,343
but now in non-detached mode.

67
00:03:09,830 --> 00:03:12,980
Let's start it, and we see this crashes.

68
00:03:12,980 --> 00:03:15,540
And it crashes with an error message

69
00:03:15,540 --> 00:03:18,410
where in the end some connection fails,

70
00:03:18,410 --> 00:03:20,580
and it's a Mongo network error.

71
00:03:20,580 --> 00:03:23,960
So in the end, what fails here is this connection

72
00:03:23,960 --> 00:03:27,010
where I try to connect my Node application

73
00:03:27,010 --> 00:03:29,240
to the MongoDB database.

74
00:03:29,240 --> 00:03:31,500
And that of course is a problem.

75
00:03:31,500 --> 00:03:36,410
It means that connecting to our host machine failed here.

76
00:03:36,410 --> 00:03:38,840
Because MongoDB the database itself

77
00:03:38,840 --> 00:03:40,800
is running on my host machine here.

78
00:03:40,800 --> 00:03:43,380
I installed it before this module started,

79
00:03:43,380 --> 00:03:45,463
so I have MongoDB installed.

80
00:03:46,550 --> 00:03:48,700
And if you follow the installation instructions

81
00:03:48,700 --> 00:03:49,950
on MongoDB.com,

82
00:03:49,950 --> 00:03:52,000
you can of course also follow along,

83
00:03:52,000 --> 00:03:55,340
try this on your own and you will get the same result.

84
00:03:55,340 --> 00:03:56,980
So that is good to know,

85
00:03:56,980 --> 00:03:59,900
out of the box connecting to the host machine like this

86
00:03:59,900 --> 00:04:02,210
with localhost fails.

87
00:04:02,210 --> 00:04:04,790
Now, before we look into fixing this,

88
00:04:04,790 --> 00:04:07,730
I wanna see if sending HTTP requests

89
00:04:07,730 --> 00:04:10,030
to some other website would work.

90
00:04:10,030 --> 00:04:12,150
And in order to test this,

91
00:04:12,150 --> 00:04:15,470
we can copy app listen 3000 here

92
00:04:15,470 --> 00:04:18,740
and move that out of this mongoose connect code

93
00:04:18,740 --> 00:04:22,713
and temporarily comment this mongoose connect code out.

94
00:04:23,570 --> 00:04:24,940
So that for the moment,

95
00:04:24,940 --> 00:04:27,830
we can start this Node container

96
00:04:27,830 --> 00:04:30,800
without connecting to the MongoDB database

97
00:04:30,800 --> 00:04:33,550
just by spinning up the web server.

98
00:04:33,550 --> 00:04:36,800
So I'm going to save this and then since I changed the code,

99
00:04:36,800 --> 00:04:41,800
I'm going to rebuild my image with the same name and tag.

100
00:04:42,030 --> 00:04:44,950
And thereafter I'm going to rerun the container.

101
00:04:44,950 --> 00:04:48,370
And I'll again do that in detached mode now.

102
00:04:48,370 --> 00:04:51,083
So running the container on the updated image.

103
00:04:52,090 --> 00:04:54,740
And with that if we now run docker ps,

104
00:04:54,740 --> 00:04:57,460
we see the container stays up and running.

105
00:04:57,460 --> 00:05:00,270
Of course, because now I'm not trying to connect

106
00:05:00,270 --> 00:05:04,003
to the database anymore and this was the part which failed.

107
00:05:04,920 --> 00:05:08,690
So therefore, of course parts of our application won't work.

108
00:05:08,690 --> 00:05:12,600
To be precise, the favorites endpoints won't work.

109
00:05:12,600 --> 00:05:16,350
But the movies endpoint and the people endpoint,

110
00:05:16,350 --> 00:05:18,720
these endpoints should work fine.

111
00:05:18,720 --> 00:05:22,120
Because there I'm not working with my local database,

112
00:05:22,120 --> 00:05:26,250
I'm instead reaching out to the Star Wars dummy API.

113
00:05:26,250 --> 00:05:29,290
Hence, if I now try sending a get request

114
00:05:29,290 --> 00:05:33,750
to localhost 3000 movies again, this works.

115
00:05:33,750 --> 00:05:38,300
And the same if I send it to localhost 3000 people,

116
00:05:38,300 --> 00:05:40,400
it works just fine.

117
00:05:40,400 --> 00:05:44,420
And this is now using the API running in our container,

118
00:05:44,420 --> 00:05:47,230
which is exposed on port 3000.

119
00:05:47,230 --> 00:05:51,770
So this works, and this has one important implication.

120
00:05:51,770 --> 00:05:53,130
Out of the box,

121
00:05:53,130 --> 00:05:58,130
containers can send requests to the World Wide Web.

122
00:05:58,260 --> 00:06:02,360
You can communicate with web API's and web pages

123
00:06:02,360 --> 00:06:06,010
from inside your dockerized applications.

124
00:06:06,010 --> 00:06:08,470
You don't need any special setup

125
00:06:08,470 --> 00:06:12,670
or any changes to your code, this code just works.

126
00:06:12,670 --> 00:06:14,940
Sending this HTTP request

127
00:06:14,940 --> 00:06:18,040
from inside the dockerized application works.

128
00:06:18,040 --> 00:06:20,980
It works just as it worked without containers

129
00:06:20,980 --> 00:06:22,860
and without Docker.

130
00:06:22,860 --> 00:06:27,000
And that's super important to understand and keep in mind.

131
00:06:27,000 --> 00:06:30,210
Sending requests from inside your container

132
00:06:30,210 --> 00:06:33,360
to the World Wide Web just works.

133
00:06:33,360 --> 00:06:37,010
It works out of the box without any special settings.

134
00:06:37,010 --> 00:06:39,720
So now let's have a look at the other two ways

135
00:06:39,720 --> 00:06:43,313
of communication and understand how we can make those work.

