1 00:00:00,000 --> 00:00:04,500 [Intro Music] 2 00:00:04,529 --> 00:00:06,089 I have just demonstrated to you how 3 00:00:06,089 --> 00:00:08,759 you're able to use variables when 4 00:00:08,759 --> 00:00:10,913 you need to insert for example, path 5 00:00:10,913 --> 00:00:13,139 in the 'docker run' command. And 6 00:00:13,139 --> 00:00:14,819 notice that if you use variables, 7 00:00:14,939 --> 00:00:16,498 then it is important in which 8 00:00:16,498 --> 00:00:18,179 folder you are located, if you use 9 00:00:18,179 --> 00:00:20,759 specifically 'PWD' variable, because 10 00:00:20,789 --> 00:00:23,159 it will actually get path to 11 00:00:23,159 --> 00:00:25,679 current folder and place it inside 12 00:00:25,679 --> 00:00:27,989 of the 'docker run' command. Great. 13 00:00:28,169 --> 00:00:29,849 Now let me explain you actually 14 00:00:29,849 --> 00:00:32,609 what happens when you run container, 15 00:00:32,878 --> 00:00:34,949 and how you're able to run 16 00:00:34,949 --> 00:00:38,020 container in background using '-d' 17 00:00:38,020 --> 00:00:40,319 option. Okay, let's get started 18 00:00:40,319 --> 00:00:42,179 with hello-world container, and 19 00:00:42,179 --> 00:00:45,419 let's run it once again, 'docker run 20 00:00:45,629 --> 00:00:49,049 hello-world', Enter, you will see 21 00:00:49,049 --> 00:00:51,149 this result in terminal but 22 00:00:51,149 --> 00:00:53,399 container exits. And I have 23 00:00:53,399 --> 00:00:55,739 explained you before that basically 24 00:00:55,739 --> 00:00:58,709 container exits as soon as there 25 00:00:58,709 --> 00:01:01,289 are no processes running inside of 26 00:01:01,289 --> 00:01:04,108 the container. And in this example, 27 00:01:04,138 --> 00:01:06,688 there was process that has printed 28 00:01:06,718 --> 00:01:09,209 this text to this terminal, and 29 00:01:09,239 --> 00:01:10,919 afterwards this process was 30 00:01:10,979 --> 00:01:13,559 terminated. And Docker terminates 31 00:01:13,589 --> 00:01:16,949 container afterwards as well. Let's 32 00:01:16,949 --> 00:01:19,169 try to run another container, for 33 00:01:19,169 --> 00:01:21,479 example, alpine. Let's clear 34 00:01:21,479 --> 00:01:25,559 terminal, and 'docker run alpine'. 35 00:01:27,119 --> 00:01:30,209 Same happens here. Docker runs 36 00:01:30,209 --> 00:01:34,049 container, it creates process, but 37 00:01:34,049 --> 00:01:36,629 this process terminates, and 38 00:01:36,629 --> 00:01:38,159 afterwards Docker terminates 39 00:01:38,159 --> 00:01:40,949 container as well. But which 40 00:01:40,949 --> 00:01:43,499 process actually starts in this 41 00:01:43,529 --> 00:01:46,499 specific container? Let me enter 42 00:01:46,529 --> 00:01:50,519 'docker history alpine', and here at 43 00:01:50,519 --> 00:01:52,769 the top you will see command, 'CMD /bin/ 44 00:01:52,799 --> 00:01:55,589 sh', in this case, I'll explain you 45 00:01:55,589 --> 00:01:57,329 what this command actually means a 46 00:01:57,329 --> 00:01:59,489 bit later. But now you should 47 00:01:59,489 --> 00:02:01,319 understand and remember that with 48 00:02:01,319 --> 00:02:03,209 this command, you are able to find 49 00:02:03,239 --> 00:02:05,789 at the top specific command that is 50 00:02:05,819 --> 00:02:08,818 executed as soon as a new container 51 00:02:08,877 --> 00:02:11,818 is created based on specific image, 52 00:02:11,848 --> 00:02:13,709 in this case the image name is 53 00:02:13,709 --> 00:02:16,499 'alpine'. So, if you create a new 54 00:02:16,499 --> 00:02:19,139 container with 'alpine' image, it 55 00:02:19,169 --> 00:02:22,829 runs command '/bin/sh'. It is actually a 56 00:02:22,829 --> 00:02:26,249 shell. But this process, this process 57 00:02:26,249 --> 00:02:29,249 has standard in, 'stdin' and standard 58 00:02:29,309 --> 00:02:33,119 output, 'stdout'; and it waits that someone 59 00:02:33,209 --> 00:02:35,399 or some other process will connect 60 00:02:35,399 --> 00:02:37,589 to each standard in and standard 61 00:02:37,619 --> 00:02:40,259 output. And in such case, this 62 00:02:40,259 --> 00:02:43,379 process will be kept running, and 63 00:02:43,379 --> 00:02:45,839 will accept the some characters 64 00:02:45,959 --> 00:02:48,689 via standard input, and may produce 65 00:02:48,719 --> 00:02:52,310 some output via standard output, 'stdout'. 66 00:02:52,310 --> 00:02:55,619 But if we simply run container 67 00:02:55,619 --> 00:02:58,649 in such way, we don't connect to 68 00:02:58,649 --> 00:03:02,399 standard input of this process, and 69 00:03:02,399 --> 00:03:04,439 that's why this process terminates 70 00:03:04,469 --> 00:03:06,239 automatically, because there is no 71 00:03:06,269 --> 00:03:07,799 standard input connection or 72 00:03:07,799 --> 00:03:09,869 standard out connection. That's 73 00:03:09,899 --> 00:03:12,059 what happens under the hood. But 74 00:03:12,089 --> 00:03:14,369 you are able to connect to standard 75 00:03:14,399 --> 00:03:17,249 input of this process if you add 76 00:03:17,279 --> 00:03:19,589 two flags, '-i' and '-t'. 77 00:03:19,739 --> 00:03:22,217 Basically you could add only '-i' flag, 78 00:03:22,217 --> 00:03:23,969 let me show you that. 'docker 79 00:03:24,419 --> 00:03:29,729 run -i alpine', and now I am 80 00:03:29,729 --> 00:03:32,579 connected to standard in of this 81 00:03:32,579 --> 00:03:35,969 process '/bin/sh', and if I enter for 82 00:03:35,969 --> 00:03:39,149 example, 'ls', I will see a list of 83 00:03:39,149 --> 00:03:41,159 folders inside of the container. 84 00:03:42,209 --> 00:03:44,669 And if I go to another tab and 85 00:03:44,699 --> 00:03:47,759 enter 'docker ps', I will see a list 86 00:03:47,759 --> 00:03:49,889 of containers that are currently up 87 00:03:49,889 --> 00:03:52,259 and running. And again I see here 88 00:03:52,259 --> 00:03:54,179 same command as I have shown you 89 00:03:54,179 --> 00:03:57,059 just in 'docker history' output, it is 90 00:03:57,059 --> 00:03:58,859 command that was automatically 91 00:03:58,889 --> 00:04:01,049 executed when a container was 92 00:04:01,049 --> 00:04:03,449 created. And for this specific 93 00:04:03,479 --> 00:04:05,999 image, alpine image, this command is 94 00:04:06,029 --> 00:04:09,989 '/bin/sh'. But let me now try to exit 95 00:04:09,989 --> 00:04:12,389 from this connection to standard 96 00:04:12,419 --> 00:04:15,239 input of 'sh' shell inside of the 97 00:04:15,239 --> 00:04:17,789 container. As usually let's try to 98 00:04:17,789 --> 00:04:20,428 use Ctrl+C combination, and you see 99 00:04:20,459 --> 00:04:22,589 it doesn't work. The reason for 100 00:04:22,589 --> 00:04:24,599 that is that we are connected now 101 00:04:24,629 --> 00:04:28,048 directly to specific 'sh' process, and 102 00:04:28,048 --> 00:04:31,400 it does not accept such termination commands. 103 00:04:32,580 --> 00:04:34,800 You are also able to try to stop 104 00:04:34,800 --> 00:04:37,050 container here using 'stop' command, 105 00:04:37,320 --> 00:04:40,380 like 'docker stop', and here maybe 106 00:04:40,380 --> 00:04:43,050 either ID of the container or its 107 00:04:43,050 --> 00:04:45,120 name, for example this one. Let me 108 00:04:45,120 --> 00:04:47,400 copy the name, paste here, press 109 00:04:47,400 --> 00:04:50,910 Enter, and you'll see that Docker is 110 00:04:50,910 --> 00:04:53,760 trying to stop container. Let's 111 00:04:53,760 --> 00:04:57,700 wait a bit. It actually sends SIGTERM 112 00:04:57,700 --> 00:05:00,000 signal to 'process'. But in 113 00:05:00,060 --> 00:05:02,910 such case, 'sh' is running with 114 00:05:02,910 --> 00:05:04,950 process id 1, and it doesn't 115 00:05:04,950 --> 00:05:07,680 accept any such kind of commands. 116 00:05:08,070 --> 00:05:11,190 And when Docker is not able to stop 117 00:05:11,190 --> 00:05:13,920 container during 10 seconds, it 118 00:05:13,920 --> 00:05:15,630 will try to kill it, it has 119 00:05:15,630 --> 00:05:17,040 actually hard stopped the 120 00:05:17,040 --> 00:05:18,780 container, and in such case with 121 00:05:18,780 --> 00:05:20,190 'stop' command, it will try to 122 00:05:20,190 --> 00:05:22,950 perform, let's say, soft stop. So 123 00:05:22,950 --> 00:05:24,630 finally, in such case in 10 124 00:05:24,630 --> 00:05:26,670 seconds, Docker has stopped this 125 00:05:26,670 --> 00:05:28,410 container. And of course here in 126 00:05:28,410 --> 00:05:30,150 this step, we have disconnected 127 00:05:30,150 --> 00:05:32,460 from standard input of the 'sh'. 128 00:05:33,180 --> 00:05:35,820 Great. This is what happens when you 129 00:05:35,820 --> 00:05:38,190 try to connect to specific process 130 00:05:38,400 --> 00:05:40,800 that actually keeps container 131 00:05:40,800 --> 00:05:43,080 running. But as soon as process 132 00:05:43,110 --> 00:05:45,480 ends, container exits as well. Let's 133 00:05:45,480 --> 00:05:47,220 try something else. Let's clear 134 00:05:47,220 --> 00:05:49,410 terminal here, and let's try to run 135 00:05:49,440 --> 00:05:53,010 nginx server, 'docker run nginx', 136 00:05:54,750 --> 00:05:56,220 and here you see that we are 137 00:05:56,220 --> 00:05:58,440 actually connected to standard 138 00:05:58,470 --> 00:06:01,620 input of nginx process. We could 139 00:06:01,620 --> 00:06:03,870 verify that here. Let's go back to 140 00:06:03,870 --> 00:06:08,460 this step, clear it, and 'docker ps', 141 00:06:08,880 --> 00:06:11,310 and we see that nginx container is 142 00:06:11,310 --> 00:06:13,560 running. And here you see basically 143 00:06:13,560 --> 00:06:16,080 command that was executed. And here 144 00:06:16,080 --> 00:06:17,760 was actually name of process that 145 00:06:17,760 --> 00:06:19,560 is running inside of the container. 146 00:06:19,830 --> 00:06:23,070 This process name is nginx. And 147 00:06:23,070 --> 00:06:24,840 actually here on this step, we are 148 00:06:24,870 --> 00:06:27,000 able to get the output from this 149 00:06:27,000 --> 00:06:28,740 process. And actually in previous 150 00:06:28,740 --> 00:06:30,180 lectures, we have seen that it 151 00:06:30,180 --> 00:06:32,550 produces some logs when clients 152 00:06:32,580 --> 00:06:34,440 connect to this container. 153 00:06:34,620 --> 00:06:36,270 Actually, let me show you that quickly. 154 00:06:36,360 --> 00:06:38,100 Let me terminate this container. 155 00:06:38,160 --> 00:06:40,170 Actually here, I'm able to use Ctrl+ 156 00:06:40,170 --> 00:06:42,870 C, and let's use port mapping. 'docker 157 00:06:42,870 --> 00:06:46,320 run -p 8080:', here will be '80 158 00:06:46,350 --> 00:06:50,100 nginx'. Press Enter, and let's 159 00:06:50,100 --> 00:06:55,440 now try to connect to port 80, and 160 00:06:55,440 --> 00:06:57,360 connection was successful, and here 161 00:06:57,420 --> 00:06:59,610 nginx has set up default content. 162 00:06:59,940 --> 00:07:01,560 Let's go back to this output, and 163 00:07:01,560 --> 00:07:04,320 here we see actually some text that 164 00:07:04,320 --> 00:07:06,930 was produced into standard output 165 00:07:07,080 --> 00:07:09,420 by nginx process inside of the 166 00:07:09,420 --> 00:07:11,460 container, here is this process. 167 00:07:12,840 --> 00:07:15,630 Great. But how you're able actually 168 00:07:15,630 --> 00:07:19,050 to run container in background? You 169 00:07:19,050 --> 00:07:21,570 are able to use '-d' option. And 170 00:07:21,570 --> 00:07:23,820 in such case, those logs will be 171 00:07:23,820 --> 00:07:26,670 actually created and saved in 172 00:07:26,670 --> 00:07:29,040 background into log file, and you will 173 00:07:29,040 --> 00:07:31,140 be able to observe them later on. 174 00:07:31,500 --> 00:07:33,990 But with '-d' option you 175 00:07:33,990 --> 00:07:35,640 will get back to terminal because 176 00:07:35,640 --> 00:07:37,962 container will be running in background. 177 00:07:38,370 --> 00:07:39,930 Let me show you that. Let me 178 00:07:39,930 --> 00:07:41,970 terminate this one, clear terminal, 179 00:07:42,120 --> 00:07:45,030 and here run same container, but 180 00:07:45,030 --> 00:07:48,000 with option '-d' without any 181 00:07:48,000 --> 00:07:50,700 arguments, '-d', and you'll 182 00:07:50,700 --> 00:07:53,430 simply see ID of the container that 183 00:07:53,430 --> 00:07:55,530 was just created. We could verify 184 00:07:55,530 --> 00:07:58,110 that here in this step 'docker ps'. 185 00:07:58,560 --> 00:08:00,420 You see here, start characters 186 00:08:00,450 --> 00:08:02,880 of this ID, and you see that now 187 00:08:02,880 --> 00:08:05,010 this container is still running 188 00:08:05,040 --> 00:08:07,050 because there was running process, 189 00:08:07,080 --> 00:08:09,990 nginx process, but we cannot 190 00:08:09,990 --> 00:08:11,820 connect it to that process here. 191 00:08:12,780 --> 00:08:14,922 That's what actually this '-d' 192 00:08:14,922 --> 00:08:17,190 option does. With this option in 193 00:08:17,190 --> 00:08:19,560 place, container is started in 194 00:08:19,560 --> 00:08:21,900 background, and you will get back to 195 00:08:21,900 --> 00:08:23,820 your terminal window, but container 196 00:08:23,850 --> 00:08:25,860 will be still kept running in 197 00:08:25,860 --> 00:08:28,530 background. Let me try now to 198 00:08:28,530 --> 00:08:30,930 connect to our web page here, 199 00:08:30,930 --> 00:08:33,390 refresh page, go back here, and of 200 00:08:33,390 --> 00:08:35,220 course in this window I will not 201 00:08:35,220 --> 00:08:36,990 see any logs, because again 202 00:08:36,990 --> 00:08:38,100 container was running in 203 00:08:38,100 --> 00:08:40,230 background. But you are able to 204 00:08:40,230 --> 00:08:43,140 read those logs using 'docker logs' 205 00:08:43,140 --> 00:08:45,299 command, and use some characters 206 00:08:45,299 --> 00:08:47,190 from the ID of specific container 207 00:08:47,190 --> 00:08:49,260 that is up and running now. And I 208 00:08:49,260 --> 00:08:51,059 could use for example characters 209 00:08:51,059 --> 00:08:52,740 '92', that are starting 210 00:08:52,740 --> 00:08:55,049 characters of this ID here, and 211 00:08:55,049 --> 00:08:57,570 let's press Enter, and you see a 212 00:08:57,570 --> 00:08:59,970 log that tells us that there was 213 00:08:59,970 --> 00:09:02,580 connection to web server, and here 214 00:09:02,580 --> 00:09:04,530 was a response code, 'Not Modified'. 215 00:09:04,920 --> 00:09:06,810 That's how you're able to get logs 216 00:09:06,840 --> 00:09:09,000 that were produced by a specific 217 00:09:09,000 --> 00:09:10,740 process inside of the container. 218 00:09:10,980 --> 00:09:13,170 And again, this container, nginx 219 00:09:13,170 --> 00:09:15,240 container is still up and running 220 00:09:15,270 --> 00:09:17,340 here because there was a single 221 00:09:17,340 --> 00:09:19,260 process inside of that container. 222 00:09:19,920 --> 00:09:21,630 Now you are able if you want to 223 00:09:21,630 --> 00:09:23,430 stop this container using 'docker 224 00:09:23,430 --> 00:09:25,620 stop' command. Let me clear terminal, 225 00:09:25,770 --> 00:09:28,320 again 'docker ps', and get some 226 00:09:28,320 --> 00:09:31,110 characters from ID or use this name, 227 00:09:31,260 --> 00:09:34,410 and use 'docker stop' command, and 228 00:09:34,440 --> 00:09:36,990 type here for example '92', and 229 00:09:36,990 --> 00:09:38,880 now you see that this container was 230 00:09:38,880 --> 00:09:40,710 stopped much faster than 231 00:09:40,740 --> 00:09:42,300 previous container, because 232 00:09:42,300 --> 00:09:44,490 previously that container was not 233 00:09:44,490 --> 00:09:46,830 listening to any commands, and 234 00:09:46,830 --> 00:09:48,840 that's why finally in 10 seconds 235 00:09:48,870 --> 00:09:50,610 process was killed automatically. 236 00:09:50,790 --> 00:09:52,830 In such case this process, nginx 237 00:09:52,830 --> 00:09:55,140 process listens to such commands, 238 00:09:55,380 --> 00:09:58,500 and Docker was able to terminate 239 00:09:58,530 --> 00:10:00,720 this container in soft, let's say 240 00:10:00,750 --> 00:10:03,570 manner. That's what '-d' 241 00:10:03,600 --> 00:10:05,280 option does, and that's how you're 242 00:10:05,280 --> 00:10:07,530 able to run such containers as for 243 00:10:07,530 --> 00:10:10,380 example, nginx in background, it 244 00:10:10,380 --> 00:10:12,630 will not interrupt your current 245 00:10:12,660 --> 00:10:14,250 terminal window, and you will be 246 00:10:14,250 --> 00:10:16,590 able to perform any other actions, 247 00:10:16,590 --> 00:10:18,510 create other containers, and so on. 248 00:10:19,230 --> 00:10:21,180 Great. That's all for this lecture. 249 00:10:21,210 --> 00:10:22,860 And in the next one let me 250 00:10:22,860 --> 00:10:24,690 demonstrate you how you're able to 251 00:10:24,720 --> 00:10:26,760 run multiple containers 252 00:10:26,790 --> 00:10:29,430 simultaneously using same image. 253 00:10:30,090 --> 00:10:31,410 Let's do the next. Bye-Bye. 254 00:10:31,410 --> 00:10:35,172 [Outro Music] 255 00:10:35,172 --> 00:10:38,900 [no audio] 256 00:10:38,900 --> 00:10:42,100 [Clock Ticking] 257 00:10:42,100 --> 00:10:43,800 [no audio]