1
00:00:02,210 --> 00:00:03,600
Now up to this point,

2
00:00:03,600 --> 00:00:07,980
we always had a look at this demo Node.js application

3
00:00:07,980 --> 00:00:10,510
which spin up that web server,

4
00:00:10,510 --> 00:00:15,290
and indeed web development and running web servers

5
00:00:15,290 --> 00:00:18,637
and other processes that deal with incoming requests

6
00:00:18,637 --> 00:00:23,550
and so on, that is the main selling point of Docker.

7
00:00:23,550 --> 00:00:27,140
That is the use case it's getting used for the most

8
00:00:27,140 --> 00:00:29,610
no matter if you then use Node.js

9
00:00:29,610 --> 00:00:31,640
for building that web service

10
00:00:31,640 --> 00:00:36,080
or if you use PHP, Python, whatever it is.

11
00:00:36,080 --> 00:00:40,730
But it's not just that, Docker is not restricted to that,

12
00:00:40,730 --> 00:00:44,090
and attached you find another example.

13
00:00:44,090 --> 00:00:46,840
A very simple Python application

14
00:00:46,840 --> 00:00:48,720
if you wanna call it like this

15
00:00:48,720 --> 00:00:51,844
which is built for Python version three

16
00:00:51,844 --> 00:00:54,360
and that's important here,

17
00:00:54,360 --> 00:00:59,360
which does not create any web server or anything like that.

18
00:00:59,940 --> 00:01:03,730
Instead of this application, in case you don't know Python

19
00:01:03,730 --> 00:01:08,320
fetches two user inputs a min and a max number,

20
00:01:08,320 --> 00:01:12,300
stores those in variables, runs a quick check,

21
00:01:12,300 --> 00:01:15,780
seeing whether the min number really is smaller

22
00:01:15,780 --> 00:01:17,030
than the max number,

23
00:01:17,030 --> 00:01:20,710
and then we calculate a random value here

24
00:01:20,710 --> 00:01:22,473
and output that value.

25
00:01:23,460 --> 00:01:27,490
Now you can install Python on your system to run this

26
00:01:27,490 --> 00:01:30,920
or you put it into a Docker image and container,

27
00:01:30,920 --> 00:01:32,850
and that's what I wanna do here

28
00:01:32,850 --> 00:01:36,500
for two main reasons, of course, to show you

29
00:01:36,500 --> 00:01:40,700
that Docker is not just about node and web servers

30
00:01:40,700 --> 00:01:44,840
I said this before, but I also wanna show it to you.

31
00:01:44,840 --> 00:01:48,610
But the second reason is related to what we learned

32
00:01:48,610 --> 00:01:52,670
in the last lecture attached and detached containers.

33
00:01:52,670 --> 00:01:55,723
So containers running in the front and in the background.

34
00:01:56,570 --> 00:02:00,480
Here, this will play a greater role because this app

35
00:02:00,480 --> 00:02:04,230
actually needs the user to interact with the app.

36
00:02:04,230 --> 00:02:08,020
Running this example here in background

37
00:02:08,020 --> 00:02:12,880
won't really work, but step-by-step, let's Dockerize this

38
00:02:12,880 --> 00:02:15,100
and of course, if you feel like this

39
00:02:15,100 --> 00:02:17,710
definitely also try it on your own.

40
00:02:17,710 --> 00:02:20,380
Otherwise you can now follow along with me

41
00:02:20,380 --> 00:02:23,913
as I will write a Docker file for this application.

42
00:02:24,910 --> 00:02:28,113
Now, how could we run this with Docker?

43
00:02:29,760 --> 00:02:32,900
Well, in the end I wanna build a custom image

44
00:02:32,900 --> 00:02:36,630
which uses Python as a base image

45
00:02:36,630 --> 00:02:40,360
so that I can copy my code, the Python code file here

46
00:02:40,360 --> 00:02:44,770
into that image and executed with the help of Python.

47
00:02:44,770 --> 00:02:49,650
Now on Docker hub I can therefore search for Python

48
00:02:49,650 --> 00:02:53,350
and will find official Python images here

49
00:02:53,350 --> 00:02:55,020
and that of course means

50
00:02:55,020 --> 00:02:59,993
that here we can absolutely use Python as base image.

51
00:03:01,160 --> 00:03:05,460
So we wanna use the Python base image as a next step,

52
00:03:05,460 --> 00:03:07,740
I also wanna set a working directory

53
00:03:07,740 --> 00:03:09,850
and I'll again, set this to app

54
00:03:09,850 --> 00:03:11,533
this of course is up to you.

55
00:03:13,020 --> 00:03:14,410
Now as a next step

56
00:03:14,410 --> 00:03:16,830
I wanna copy in my code files

57
00:03:16,830 --> 00:03:20,470
or in this case, the single code file which I have here

58
00:03:20,470 --> 00:03:22,410
still I can use copy dot

59
00:03:22,410 --> 00:03:24,940
and copy everything in the local folder

60
00:03:24,940 --> 00:03:27,640
which essentially is the Python file

61
00:03:27,640 --> 00:03:29,563
therefore into this app folder.

62
00:03:31,600 --> 00:03:35,320
Now, as a last step we can add the command here

63
00:03:35,320 --> 00:03:38,900
that should be executed when this container is started

64
00:03:38,900 --> 00:03:42,120
and here, I wanna use the Python executable

65
00:03:42,120 --> 00:03:45,720
which will be available, thanks to the Python image

66
00:03:45,720 --> 00:03:49,410
and run the RNG dot PY file with it.

67
00:03:49,410 --> 00:03:53,260
That's my command with which this container

68
00:03:53,260 --> 00:03:55,150
should start when we started

69
00:03:57,280 --> 00:03:59,773
and that could be our Docker file here.

70
00:04:00,670 --> 00:04:03,070
Now with that created

71
00:04:03,070 --> 00:04:08,070
we of course can run Docker build dot to build this image,

72
00:04:10,870 --> 00:04:13,570
and since this is the first time I'm building this

73
00:04:13,570 --> 00:04:17,970
it will, of course also download this Python base image

74
00:04:17,970 --> 00:04:21,529
and they offered this initial building takes a bit longer.

75
00:04:21,529 --> 00:04:24,130
So let me wait for this to finish

76
00:04:25,240 --> 00:04:28,260
and now with that done, this image is built.

77
00:04:28,260 --> 00:04:31,570
Now we can use the image ID here

78
00:04:31,570 --> 00:04:35,650
to then bring up a container by running Docker run.

79
00:04:35,650 --> 00:04:38,820
Now here, I don't wanna publish any port

80
00:04:38,820 --> 00:04:41,210
because we're not exposing any port here,

81
00:04:41,210 --> 00:04:43,630
it's not a network based application

82
00:04:43,630 --> 00:04:45,670
so no needs to do that

83
00:04:45,670 --> 00:04:47,643
instead I really just wanna run it.

84
00:04:48,700 --> 00:04:53,480
And if I run it like this though I get an error here.

85
00:04:53,480 --> 00:04:55,920
To be precise, I see some output here

86
00:04:55,920 --> 00:04:58,200
because with Docker run as you learned

87
00:04:58,200 --> 00:05:01,080
by default we start in attached mode,

88
00:05:01,080 --> 00:05:04,940
but I don't see anything else besides this error message

89
00:05:04,940 --> 00:05:08,530
I'm not able to interact with this running container

90
00:05:08,530 --> 00:05:11,820
and with the application running into container

91
00:05:11,820 --> 00:05:15,450
and the reason for that is that by default,

92
00:05:15,450 --> 00:05:17,350
yes, when using Docker run

93
00:05:17,350 --> 00:05:19,730
we are attached to the container

94
00:05:19,730 --> 00:05:24,150
so we can listen to output printed by the container

95
00:05:24,150 --> 00:05:25,760
but we're not attached to it

96
00:05:25,760 --> 00:05:29,540
in the sense of being able to enter anything,

97
00:05:29,540 --> 00:05:33,090
we can't input anything into the container

98
00:05:33,090 --> 00:05:36,090
or into the application running into container

99
00:05:36,090 --> 00:05:39,510
and that is something I need to fix here.

100
00:05:39,510 --> 00:05:42,577
Now for that let's execute dash, dash help

101
00:05:42,577 --> 00:05:45,280
the dash dash help flag on Docker run

102
00:05:45,280 --> 00:05:49,700
and see which useful configuration options we got here.

103
00:05:49,700 --> 00:05:53,420
And here we actually have two options that will help us.

104
00:05:53,420 --> 00:05:56,330
The first one is the dash I option

105
00:05:56,330 --> 00:06:01,280
which allows to launch this container in interactive mode,

106
00:06:01,280 --> 00:06:04,070
you could also use dash dash interactive instead

107
00:06:04,070 --> 00:06:07,710
but of course, dash I is shorter, so we'll use that

108
00:06:07,710 --> 00:06:11,580
this keeps standard input open even if not attached

109
00:06:11,580 --> 00:06:14,640
which means we will be able to input

110
00:06:14,640 --> 00:06:16,660
something in a container.

111
00:06:16,660 --> 00:06:20,840
However, typically we combine this with dash T

112
00:06:20,840 --> 00:06:22,780
this allocates a pseudo TTY

113
00:06:24,090 --> 00:06:27,540
and in the end this means it creates a terminal

114
00:06:27,540 --> 00:06:31,870
and therefore if you combine the I and the T flag

115
00:06:31,870 --> 00:06:34,250
we are able to input something

116
00:06:34,250 --> 00:06:36,670
so the container will listen for our input

117
00:06:36,670 --> 00:06:40,110
and we'll also get a terminal exposed by the container

118
00:06:40,110 --> 00:06:43,293
which is actually the device where we enter the input.

119
00:06:44,770 --> 00:06:48,210
So with that, we can rerun this container

120
00:06:48,210 --> 00:06:53,210
but now by adding the dash I and the dash T flag

121
00:06:53,220 --> 00:06:56,860
and we can also combine this to just one flag dash IT

122
00:06:58,510 --> 00:07:01,560
and with this now you see it doesn't crash

123
00:07:01,560 --> 00:07:04,140
because now it's getting the input it needs

124
00:07:04,140 --> 00:07:07,820
and we can now type something here in our terminal

125
00:07:07,820 --> 00:07:10,940
which communicates with this pseudo terminal

126
00:07:10,940 --> 00:07:12,700
exposed by the container.

127
00:07:12,700 --> 00:07:15,090
Which then in turn, also is connected

128
00:07:15,090 --> 00:07:19,053
to the container process that listens for user input.

129
00:07:19,910 --> 00:07:22,930
So here I can then enter a min and a max number

130
00:07:22,930 --> 00:07:26,183
and I get back this random number.

131
00:07:27,230 --> 00:07:30,470
And there after you see the container shuts down

132
00:07:30,470 --> 00:07:32,070
it's no longer running

133
00:07:32,070 --> 00:07:35,840
we see it with dash A but that is okay here.

134
00:07:35,840 --> 00:07:40,213
We could now restarted with Docker start using that name

135
00:07:43,370 --> 00:07:45,500
and it would be running again.

136
00:07:45,500 --> 00:07:48,530
However, of course, now you see we have a problem

137
00:07:48,530 --> 00:07:53,500
now, since Docker starts in detached mode by default

138
00:07:53,500 --> 00:07:55,873
we can't communicate with the container.

139
00:07:57,000 --> 00:07:58,750
So how can we now deal with that?

140
00:07:58,750 --> 00:08:00,890
If we restarted the container.

141
00:08:00,890 --> 00:08:02,590
Well, there are two ways,

142
00:08:02,590 --> 00:08:05,430
for one, let me stop this again real quick

143
00:08:05,430 --> 00:08:08,020
we can also restart the container

144
00:08:08,020 --> 00:08:09,940
in attached mode, of course.

145
00:08:09,940 --> 00:08:14,940
So if I do that Docker start and I add dash A here

146
00:08:15,560 --> 00:08:17,420
we start an attached mode

147
00:08:17,420 --> 00:08:20,750
and now we can also enter something here

148
00:08:20,750 --> 00:08:24,840
but only once, there after it behaves strangely.

149
00:08:24,840 --> 00:08:27,030
So that doesn't look right.

150
00:08:27,030 --> 00:08:28,970
So let me cancel this

151
00:08:30,150 --> 00:08:33,020
and then also stop this container again

152
00:08:33,020 --> 00:08:37,809
and show you the other way of restarting this container

153
00:08:37,809 --> 00:08:40,460
and being able to input something again.

154
00:08:40,460 --> 00:08:42,970
Because if we have a look at Docker start

155
00:08:42,970 --> 00:08:46,660
configuration options, you'll see we don't just have dash A

156
00:08:46,660 --> 00:08:48,130
for listening to output

157
00:08:48,130 --> 00:08:53,130
but all the dash I to again, be able to input something

158
00:08:53,360 --> 00:08:54,920
into the container.

159
00:08:54,920 --> 00:08:57,580
Now here will not need the dash T flag

160
00:08:57,580 --> 00:08:59,840
because that was still memorized,

161
00:08:59,840 --> 00:09:03,200
if we ran the container originally with that flag,

162
00:09:03,200 --> 00:09:05,610
with the Docker run command which we did,

163
00:09:05,610 --> 00:09:08,390
but I need to kind of re-emphasize

164
00:09:08,390 --> 00:09:11,623
that when I restarted here, I also again wanna listen.

165
00:09:12,730 --> 00:09:16,030
So now, I can again start this container

166
00:09:16,030 --> 00:09:19,230
but now also add dash A and dash I

167
00:09:19,230 --> 00:09:22,820
and now I can enter a min and a max number

168
00:09:22,820 --> 00:09:25,660
and I get back my random number.

169
00:09:25,660 --> 00:09:27,830
So this is how we can now restart this

170
00:09:27,830 --> 00:09:30,650
and still attach us to the container

171
00:09:30,650 --> 00:09:32,900
but not just in the listen only mode

172
00:09:32,900 --> 00:09:36,723
but also in the I want to input something mode.

173
00:09:37,940 --> 00:09:40,170
And this hopefully again, shows you the idea

174
00:09:40,170 --> 00:09:42,430
behind being attached or detached

175
00:09:42,430 --> 00:09:45,120
and why some applications like this one

176
00:09:45,120 --> 00:09:47,690
might need us to be attached

177
00:09:47,690 --> 00:09:51,920
not just for listening to output, but also to provide input

178
00:09:51,920 --> 00:09:55,240
because Docker is not just about web servers

179
00:09:55,240 --> 00:09:59,720
and web applications and long running processes.

180
00:09:59,720 --> 00:10:02,830
Docker can also be used to dockerize

181
00:10:02,830 --> 00:10:05,950
simple utility applications like this

182
00:10:05,950 --> 00:10:08,280
which need input and provide some output

183
00:10:08,280 --> 00:10:10,340
and eventually might be done.

184
00:10:10,340 --> 00:10:13,840
That is also something you can use Docker for,

185
00:10:13,840 --> 00:10:17,513
Well, and that is how you can use Docker for this.

186
00:10:18,350 --> 00:10:19,470
Now, with that said,

187
00:10:19,470 --> 00:10:22,180
I will go back to this node application

188
00:10:22,180 --> 00:10:25,200
which will serve as an example for the next lectures,

189
00:10:25,200 --> 00:10:27,960
but I hope it's clear that you cannot just use node

190
00:10:27,960 --> 00:10:30,170
and not just use web servers

191
00:10:30,170 --> 00:10:33,650
and that you can interact with the container

192
00:10:33,650 --> 00:10:35,480
exactly as you need to,

193
00:10:35,480 --> 00:10:38,000
if you don't need interaction you don't have to,

194
00:10:38,000 --> 00:10:39,200
but if you do need it

195
00:10:39,200 --> 00:10:42,513
you can communicate just as shown in the lecture.

