1
00:00:02,070 --> 00:00:03,969
Using these utility containers

2
00:00:03,969 --> 00:00:06,000
as we did it here works.

3
00:00:06,000 --> 00:00:09,460
But as I mentioned, it can be cumbersome to write

4
00:00:09,460 --> 00:00:13,060
these long commands, especially with the bind mounts.

5
00:00:13,060 --> 00:00:16,360
Good news is, we can also use Docker Compose for that

6
00:00:16,360 --> 00:00:18,710
even though it's just one container.

7
00:00:18,710 --> 00:00:20,710
But I did mention that Docker Compose

8
00:00:20,710 --> 00:00:23,140
can also be helpful for just one container.

9
00:00:23,140 --> 00:00:25,890
And maybe you also have projects with multiple containers,

10
00:00:25,890 --> 00:00:28,520
some app containers, some utility containers,

11
00:00:28,520 --> 00:00:29,950
you can mix and match that,

12
00:00:29,950 --> 00:00:33,100
and you will see all of that in action in a bigger setup

13
00:00:33,100 --> 00:00:36,230
and a bigger project right after this module.

14
00:00:36,230 --> 00:00:38,080
when we dive into Level.

15
00:00:38,080 --> 00:00:40,980
And there we'll set up a Level application

16
00:00:40,980 --> 00:00:44,040
and also use some utility containers.

17
00:00:44,040 --> 00:00:47,219
But, step-by-step, how can we use Compose here?

18
00:00:47,219 --> 00:00:49,520
Well, as always, we have to add

19
00:00:49,520 --> 00:00:52,477
a docker dash compose dot yaml file,

20
00:00:52,477 --> 00:00:56,880
and in there we define our services as you learned it.

21
00:00:56,880 --> 00:00:58,580
So we started with the version

22
00:00:58,580 --> 00:01:00,850
which is 3.8 for me here,

23
00:01:00,850 --> 00:01:03,140
and then I add my services key.

24
00:01:03,140 --> 00:01:07,179
And here I have only one service and I will name it npm.

25
00:01:07,179 --> 00:01:11,400
And this will be a container which should use node

26
00:01:11,400 --> 00:01:13,937
and which should den also give me

27
00:01:13,937 --> 00:01:16,240
this entry point and so on.

28
00:01:16,240 --> 00:01:18,420
So I will use this Docker file here,

29
00:01:18,420 --> 00:01:20,440
and hence in the npm service,

30
00:01:20,440 --> 00:01:23,870
we can use to build command and simply use dot slash

31
00:01:23,870 --> 00:01:26,470
to indicate that we're already in the folder

32
00:01:26,470 --> 00:01:30,240
in which this Docker file can be found for this container.

33
00:01:30,240 --> 00:01:34,320
So this now tells compose that this image should be built

34
00:01:34,320 --> 00:01:36,870
when this container is being used.

35
00:01:36,870 --> 00:01:39,710
Now, the cool thing is, we can also now add

36
00:01:39,710 --> 00:01:43,480
this standard input open flag and the tdy flag

37
00:01:43,480 --> 00:01:44,930
to set both to true,

38
00:01:44,930 --> 00:01:47,700
to ensure that if the command needs our input,

39
00:01:47,700 --> 00:01:49,977
we can provide that input.

40
00:01:49,977 --> 00:01:54,630
And we can also now add our volumes here,

41
00:01:54,630 --> 00:01:58,660
and then add our bind mount by connecting the folder

42
00:01:58,660 --> 00:02:01,530
in which this docker compose dot yaml file is,

43
00:02:01,530 --> 00:02:06,530
to the app folder inside of the container, just like this.

44
00:02:08,190 --> 00:02:12,240
And that's essentially the command we executed before

45
00:02:12,240 --> 00:02:15,810
now moved into a docker compose yaml file.

46
00:02:15,810 --> 00:02:17,690
And there it's easier to maintain,

47
00:02:17,690 --> 00:02:20,670
and we don't have to rewrite it all the time.

48
00:02:20,670 --> 00:02:25,670
So now instead, we can simply use this docker compose file.

49
00:02:26,260 --> 00:02:28,580
And we learned that we can use up

50
00:02:28,580 --> 00:02:30,610
to bring up this container.

51
00:02:30,610 --> 00:02:33,300
Now, if I just run up like this however,

52
00:02:33,300 --> 00:02:36,080
it in the end does something strange.

53
00:02:36,080 --> 00:02:38,200
It starts building npm,

54
00:02:38,200 --> 00:02:40,163
and I don't know what it's doing there.

55
00:02:41,430 --> 00:02:45,650
The problem here is that now I executed just npm,

56
00:02:45,650 --> 00:02:48,120
because I have just npm as a entry point,

57
00:02:48,120 --> 00:02:50,093
and I didn't specify any other command.

58
00:02:51,140 --> 00:02:54,040
So instead we can use docker compose up

59
00:02:54,040 --> 00:02:57,103
and then maybe also add a comment thereafter, maybe init.

60
00:02:58,340 --> 00:03:02,090
But if I do that, that fails, no such service init.

61
00:03:02,090 --> 00:03:04,560
And indeed, we use Docker Compose

62
00:03:04,560 --> 00:03:07,810
differently than just a docker run command.

63
00:03:07,810 --> 00:03:11,200
Docker compose up is really meant to bring up

64
00:03:11,200 --> 00:03:14,260
services defined in a docker compose yaml file.

65
00:03:14,260 --> 00:03:16,070
And here we're typically talking

66
00:03:16,070 --> 00:03:18,150
about application containers,

67
00:03:18,150 --> 00:03:19,950
so containers that are started

68
00:03:19,950 --> 00:03:22,290
and then should continue running.

69
00:03:22,290 --> 00:03:24,220
For these utility containers,

70
00:03:24,220 --> 00:03:26,340
we also got to command though.

71
00:03:26,340 --> 00:03:29,430
We get docker composed exec to run commands

72
00:03:29,430 --> 00:03:31,880
in already running containers

73
00:03:31,880 --> 00:03:34,800
which were created by Docker Compose,

74
00:03:34,800 --> 00:03:38,120
but we also got docker compose run,

75
00:03:38,120 --> 00:03:40,890
and that's now a useful command here.

76
00:03:40,890 --> 00:03:45,890
Docker compose run allows us to run as single service

77
00:03:46,910 --> 00:03:50,110
from this yaml file in case we had multiple services,

78
00:03:50,110 --> 00:03:53,070
here we have only one, but if we had multiple services,

79
00:03:53,070 --> 00:03:56,890
we could also target a single service by the service name.

80
00:03:56,890 --> 00:03:59,780
So in this case, npm, because I used npm up here,

81
00:03:59,780 --> 00:04:01,146
I use npm down there.

82
00:04:01,146 --> 00:04:06,146
And then, with any command that should be upended

83
00:04:06,400 --> 00:04:09,340
after our entry point of our choice,

84
00:04:09,340 --> 00:04:11,129
so for example, init.

85
00:04:11,129 --> 00:04:13,270
And now if I hit Enter,

86
00:04:13,270 --> 00:04:16,540
I'm getting all these questions again,

87
00:04:16,540 --> 00:04:19,630
and then it finishes and creates this project for me.

88
00:04:19,630 --> 00:04:23,030
And that's even better to see if I delete node modules

89
00:04:23,030 --> 00:04:27,290
and the package JSON files, and I run this again.

90
00:04:27,290 --> 00:04:28,850
So let's run it again.

91
00:04:28,850 --> 00:04:31,199
Let's quickly go through all these questions,

92
00:04:31,199 --> 00:04:34,010
confirmed this, and here we are.

93
00:04:34,010 --> 00:04:36,570
There's our package JSON file again.

94
00:04:36,570 --> 00:04:38,690
So now we use docker compose for that,

95
00:04:38,690 --> 00:04:41,193
with the docker compose run command.

96
00:04:42,330 --> 00:04:44,289
Now one important word about that though,

97
00:04:44,289 --> 00:04:47,140
if you inspect your running containers,

98
00:04:47,140 --> 00:04:50,580
you'll see no containers, but if you inspect all containers,

99
00:04:50,580 --> 00:04:52,630
you see a lot of containers here.

100
00:04:52,630 --> 00:04:55,410
And the reason for that is, that I never added

101
00:04:55,410 --> 00:04:59,310
the dash dash rm flag when I used docker run,

102
00:04:59,310 --> 00:05:01,510
and of course we could do that there.

103
00:05:01,510 --> 00:05:03,986
But for Docker compose we have something similar.

104
00:05:03,986 --> 00:05:08,653
When we start services with docker compose up,

105
00:05:09,920 --> 00:05:12,020
they are automatically deleted,

106
00:05:12,020 --> 00:05:15,690
so the containers are automatically removed if we use down.

107
00:05:15,690 --> 00:05:19,103
But with docker compose run, there is no up and down,

108
00:05:20,127 --> 00:05:22,630
a container starts, does its thing,

109
00:05:22,630 --> 00:05:25,840
and once the command finished, it shuts down.

110
00:05:25,840 --> 00:05:28,308
But with run, it's not removed automatically.

111
00:05:28,308 --> 00:05:32,040
You can change that though by not just using run like this,

112
00:05:32,040 --> 00:05:34,580
but by also adding dash dash rm here,

113
00:05:34,580 --> 00:05:37,080
and then this utility container will be removed

114
00:05:37,080 --> 00:05:38,830
once to command finish.

115
00:05:38,830 --> 00:05:41,340
So with that can still execute the command,

116
00:05:41,340 --> 00:05:43,447
but thereafter this container is gone.

117
00:05:43,447 --> 00:05:45,630
Of course for the moment we can clean up

118
00:05:45,630 --> 00:05:48,640
all the containers we have with docker container prune,

119
00:05:48,640 --> 00:05:51,550
and this will also remove all the unused containers.

120
00:05:51,550 --> 00:05:53,380
But I find it quite important to be aware

121
00:05:53,380 --> 00:05:56,820
of this dash dash rm flag on docker compose run,

122
00:05:56,820 --> 00:05:59,290
because that makes sure that you don't end up

123
00:05:59,290 --> 00:06:02,737
with all those unnecessary containers in the first place.

124
00:06:02,737 --> 00:06:06,030
And that's it, these are utility containers

125
00:06:06,030 --> 00:06:07,483
as I'd like to call them.

