1
00:00:02,090 --> 00:00:05,160
At the moment, this basic utility container,

2
00:00:05,160 --> 00:00:09,210
which I created here, allows us to run any command.

3
00:00:09,210 --> 00:00:13,190
Now that is useful, that is definitely not too bad.

4
00:00:13,190 --> 00:00:17,390
But maybe we know that it should be a utility container,

5
00:00:17,390 --> 00:00:21,960
a utility image, which allows us to run npm commands.

6
00:00:21,960 --> 00:00:25,480
So not any command, but restricted to npm.

7
00:00:25,480 --> 00:00:28,730
For example, npm init for initialization.

8
00:00:28,730 --> 00:00:31,720
But then npm install for installing.

9
00:00:31,720 --> 00:00:32,960
Of course, we can do this.

10
00:00:32,960 --> 00:00:35,090
I can run npm install here,

11
00:00:35,090 --> 00:00:38,600
but maybe I just want to run docker run, it,

12
00:00:38,600 --> 00:00:40,970
then my bind mount, then the image name,

13
00:00:40,970 --> 00:00:42,700
and then just install.

14
00:00:42,700 --> 00:00:45,820
And under the hood, inside of the container

15
00:00:45,820 --> 00:00:50,800
that should prepend npm here to run npm install.

16
00:00:50,800 --> 00:00:54,200
So that we have a more restricted utility container

17
00:00:54,200 --> 00:00:55,960
where we can't do everything.

18
00:00:55,960 --> 00:00:58,110
Also to protect ourselves,

19
00:00:58,110 --> 00:01:00,730
so that I don't accidentally run some command

20
00:01:00,730 --> 00:01:03,560
in the container, which deletes everything.

21
00:01:03,560 --> 00:01:05,360
And then because of the bind mount,

22
00:01:05,360 --> 00:01:08,260
it starts deleting content on my host machine as well.

23
00:01:08,260 --> 00:01:10,960
So for this reason, as well, it might be interesting

24
00:01:10,960 --> 00:01:13,970
to restrict the commands we can run.

25
00:01:13,970 --> 00:01:17,480
And for that, we got an extra instruction in the Dockerfile,

26
00:01:17,480 --> 00:01:19,440
which we haven't used thus far.

27
00:01:19,440 --> 00:01:22,800
And that's the entrypoint instruction.

28
00:01:22,800 --> 00:01:26,130
It is quite similar to the command instruction,

29
00:01:26,130 --> 00:01:28,730
but it has one key difference.

30
00:01:28,730 --> 00:01:33,280
If we add a command after the image name on docker run,

31
00:01:33,280 --> 00:01:36,740
then this command, in this case npm install

32
00:01:36,740 --> 00:01:40,720
overwrites the command specified in a Dockerfile.

33
00:01:40,720 --> 00:01:41,810
If there is one.

34
00:01:41,810 --> 00:01:43,360
We didn't have one thus far.

35
00:01:43,360 --> 00:01:46,600
But if we had one, this command in the Dockerfile

36
00:01:46,600 --> 00:01:49,210
would be overwritten by this command

37
00:01:49,210 --> 00:01:52,270
after the image name on docker run.

38
00:01:52,270 --> 00:01:54,830
With the entrypoint that's different.

39
00:01:54,830 --> 00:01:58,630
For the entrypoint, whatever you enter here

40
00:01:58,630 --> 00:02:00,810
after your image name on docker run

41
00:02:00,810 --> 00:02:04,400
is appended after the entry point.

42
00:02:04,400 --> 00:02:05,880
And that's pretty cool.

43
00:02:05,880 --> 00:02:09,860
That means that here, we could specify npm.

44
00:02:09,860 --> 00:02:14,860
And now we could append any npm command after our image name

45
00:02:15,410 --> 00:02:17,750
once we rebuild this image.

46
00:02:17,750 --> 00:02:18,640
So let's do that.

47
00:02:18,640 --> 00:02:21,000
Let's add this entrypoint.

48
00:02:21,000 --> 00:02:23,930
And then let's build this image again,

49
00:02:23,930 --> 00:02:26,370
maybe with a tag of mynpm,

50
00:02:26,370 --> 00:02:29,670
because it's my own version of npm in a container.

51
00:02:29,670 --> 00:02:31,590
So let's do that.

52
00:02:31,590 --> 00:02:35,880
And then we can run docker run mynpm in interactive mode,

53
00:02:35,880 --> 00:02:39,090
in case some input is required from our side

54
00:02:39,090 --> 00:02:41,010
with our volume added.

55
00:02:41,010 --> 00:02:43,800
So again, I'll copy this path,

56
00:02:43,800 --> 00:02:46,390
I will delete this package.json file by the way,

57
00:02:46,390 --> 00:02:49,630
so that we can recreate it and see that in action.

58
00:02:49,630 --> 00:02:52,940
Then use my absolute path here for the bind mount.

59
00:02:52,940 --> 00:02:54,920
And bind this to slash app,

60
00:02:54,920 --> 00:02:58,820
which is the folder inside of the container

61
00:02:58,820 --> 00:03:01,810
against which these commands will be executed.

62
00:03:01,810 --> 00:03:05,250
Entrypoint also adheres to work directory.

63
00:03:05,250 --> 00:03:08,820
And then here after my npm, so after this image name,

64
00:03:08,820 --> 00:03:12,570
I just need to run init, not npm init,

65
00:03:12,570 --> 00:03:13,740
but just init.

66
00:03:13,740 --> 00:03:16,900
Because we have npm already in the entrypoint.

67
00:03:16,900 --> 00:03:18,370
And if I hit Enter,

68
00:03:18,370 --> 00:03:21,170
now again, we can go through these questions

69
00:03:21,170 --> 00:03:24,470
and once we're done, we got this package.json file.

70
00:03:24,470 --> 00:03:27,830
And now we can run any npm commands against this.

71
00:03:27,830 --> 00:03:29,810
For example, npm install.

72
00:03:29,810 --> 00:03:32,170
If I add install here, this now installs

73
00:03:32,170 --> 00:03:34,763
all the dependencies it finds in package.json.

74
00:03:35,640 --> 00:03:37,490
And keep in mind by the way,

75
00:03:37,490 --> 00:03:39,560
that the container always shuts down

76
00:03:39,560 --> 00:03:41,130
once the command is done.

77
00:03:41,130 --> 00:03:44,220
But it was still able to run npm install here,

78
00:03:44,220 --> 00:03:47,940
because of course, since my local folder is the bind mount,

79
00:03:47,940 --> 00:03:50,860
the package.json file, which I already had

80
00:03:50,860 --> 00:03:53,560
was copied into the app folder,

81
00:03:53,560 --> 00:03:57,010
and therefore npm install being executed in the app folder,

82
00:03:57,010 --> 00:03:59,810
was able to pick up this package.json file

83
00:03:59,810 --> 00:04:02,300
and use it inside of the container.

84
00:04:02,300 --> 00:04:04,400
Now I deleted the package lock.json file,

85
00:04:04,400 --> 00:04:06,350
because of course here the install command

86
00:04:06,350 --> 00:04:07,690
didn't do too much.

87
00:04:07,690 --> 00:04:10,280
Since we got absolutely no dependencies specified

88
00:04:10,280 --> 00:04:12,220
in package.json.

89
00:04:12,220 --> 00:04:14,290
Well, we could add them manually here

90
00:04:14,290 --> 00:04:17,350
by adding a dependencies node and adding dependencies

91
00:04:17,350 --> 00:04:19,709
in case we know their names and versions.

92
00:04:19,709 --> 00:04:21,620
Or again, we don't do this,

93
00:04:21,620 --> 00:04:25,440
and instead I run my npm image with install

94
00:04:25,440 --> 00:04:29,740
and then here I could add express

95
00:04:29,740 --> 00:04:32,490
and this would install the express dependency,

96
00:04:32,490 --> 00:04:34,780
maybe with the dash dash save flag,

97
00:04:34,780 --> 00:04:37,610
which is a flag the npm install command accepts

98
00:04:37,610 --> 00:04:42,610
to add express as a package as a dependency to this project.

99
00:04:42,930 --> 00:04:46,350
So for now hit Enter this now installs express,

100
00:04:46,350 --> 00:04:49,100
and you see it creates the node_modules file

101
00:04:49,100 --> 00:04:53,580
and mirrors that back into our local folder here

102
00:04:53,580 --> 00:04:55,123
because of that bind mount.

103
00:04:56,240 --> 00:04:58,040
So that's really useful

104
00:04:58,040 --> 00:05:00,210
and that's a pattern you will also see

105
00:05:00,210 --> 00:05:01,820
in many Docker project,

106
00:05:01,820 --> 00:05:04,290
and it's also a pattern we will use

107
00:05:04,290 --> 00:05:06,303
in some parts of this course.

108
00:05:07,270 --> 00:05:08,103
Now nonetheless,

109
00:05:08,103 --> 00:05:10,510
at the moment our approach here has a downside.

110
00:05:10,510 --> 00:05:12,210
It has a disadvantage.

111
00:05:12,210 --> 00:05:15,820
We have to run these relatively long commands

112
00:05:15,820 --> 00:05:18,340
in the terminal in the command prompt.

113
00:05:18,340 --> 00:05:21,170
And we already learned about a solution for that.

114
00:05:21,170 --> 00:05:23,190
We learned about Docker-compose.

115
00:05:23,190 --> 00:05:25,913
So maybe it can help us here as well.

