1 00:00:00,000 --> 00:00:04,300 [Intro Music] 2 00:00:04,380 --> 00:00:06,270 We have just successfully created 3 00:00:06,270 --> 00:00:08,039 a small Express web server 4 00:00:08,039 --> 00:00:10,500 application using Node.js, and 5 00:00:10,500 --> 00:00:12,930 we have executed this application. 6 00:00:13,230 --> 00:00:15,030 And before that we have installed 7 00:00:15,060 --> 00:00:16,830 some additional packages, namely 8 00:00:16,830 --> 00:00:18,630 express package along with its 9 00:00:18,630 --> 00:00:20,910 dependencies using 'npm install' 10 00:00:20,910 --> 00:00:22,890 command. And all of that we have 11 00:00:23,040 --> 00:00:25,140 performed inside of the Node.js 12 00:00:25,140 --> 00:00:27,720 container. There was no NPM, or 13 00:00:27,720 --> 00:00:29,940 Node running on our local computer. 14 00:00:30,300 --> 00:00:31,710 But now there is a challenge for 15 00:00:31,710 --> 00:00:33,210 us, and you see that our 16 00:00:33,210 --> 00:00:35,400 application is not terminated when 17 00:00:35,430 --> 00:00:37,980 we press key combination Ctrl+C. 18 00:00:38,100 --> 00:00:41,490 And when SIGINT signal is sent to our 19 00:00:41,520 --> 00:00:43,500 Node.js process, it is sent 20 00:00:43,530 --> 00:00:45,930 directly to our process. And notice 21 00:00:45,930 --> 00:00:47,310 that in this command, we have 22 00:00:47,310 --> 00:00:49,890 actually overridden default command, 23 00:00:49,920 --> 00:00:51,600 this node command, using custom 24 00:00:51,600 --> 00:00:54,540 command, and we run index.js file 25 00:00:54,570 --> 00:00:56,910 using Node interpreter. And we have 26 00:00:56,940 --> 00:00:59,310 also mapped port of our Express 27 00:00:59,430 --> 00:01:01,470 application, and we have exposed it 28 00:01:01,470 --> 00:01:03,900 to external port 3000. And we have 29 00:01:03,900 --> 00:01:05,670 also used the customer working 30 00:01:05,670 --> 00:01:08,130 directory, and we have mapped our local 31 00:01:08,130 --> 00:01:10,500 folder using this option. And we 32 00:01:10,500 --> 00:01:12,090 have mapped this folder to 'app' 33 00:01:12,090 --> 00:01:13,950 folder inside of the container. 34 00:01:14,370 --> 00:01:16,410 Great. Let's now modify a bit our 35 00:01:16,410 --> 00:01:18,600 application, and let's add handling 36 00:01:18,780 --> 00:01:21,570 of the SIGINT signal. I want to 37 00:01:21,570 --> 00:01:23,010 show you that because it is 38 00:01:23,040 --> 00:01:25,050 important for understanding of how 39 00:01:25,050 --> 00:01:27,000 we're actually connected to node 40 00:01:27,000 --> 00:01:29,760 process using Docker. Let's go to 41 00:01:29,790 --> 00:01:32,160 our tiny application here, and 42 00:01:32,160 --> 00:01:33,960 let's open up index.js file 43 00:01:33,990 --> 00:01:36,090 located in the 'express' folder. Let 44 00:01:36,090 --> 00:01:38,250 me hide this left pane. And let's 45 00:01:38,430 --> 00:01:40,770 add additional package here, and 46 00:01:40,800 --> 00:01:42,600 it is actually default package Node.js 47 00:01:42,600 --> 00:01:44,550 package called 'process'. And 48 00:01:44,550 --> 00:01:46,440 afterwards, we will use this 49 00:01:46,470 --> 00:01:48,870 package for handling of the SIGINT 50 00:01:48,900 --> 00:01:50,820 signal. Let's do that. Let's 51 00:01:50,850 --> 00:01:52,860 require additional package 'const 52 00:01:52,860 --> 00:01:58,140 process = require('process')', like so. 53 00:01:58,380 --> 00:02:00,690 And next, for example, here let's 54 00:02:00,720 --> 00:02:03,810 add following lines, 'process.on()' 55 00:02:04,080 --> 00:02:06,060 And here first argument will 56 00:02:06,060 --> 00:02:09,090 be 'SIGINT', and second will be 57 00:02:09,090 --> 00:02:10,770 callback function, I'll use arrow 58 00:02:10,770 --> 00:02:12,539 function expression syntax. Again, 59 00:02:12,539 --> 00:02:14,820 if you don't know Node.js syntax, and 60 00:02:14,820 --> 00:02:16,560 don't know JavaScript, please 61 00:02:16,590 --> 00:02:19,080 nevermind, I want to just show you 62 00:02:19,110 --> 00:02:21,300 how you're able to handle SIGINT 63 00:02:21,300 --> 00:02:23,460 signal inside of the process inside 64 00:02:23,460 --> 00:02:25,500 of the container. Okay, let's add 65 00:02:25,500 --> 00:02:27,810 here arrow function syntax like so. 66 00:02:27,990 --> 00:02:30,450 And here inside of this arrow 67 00:02:30,450 --> 00:02:31,920 function, let's do following. 68 00:02:32,130 --> 00:02:34,080 Let's print to the terminal message 69 00:02:34,290 --> 00:02:37,620 'Application is being terminated 70 00:02:39,090 --> 00:02:41,370 ...', like so. And here on the 71 00:02:41,370 --> 00:02:43,200 next line, let's exit from the 72 00:02:43,200 --> 00:02:44,820 process using the following syntax, 73 00:02:44,940 --> 00:02:47,220 'process.exit'(), and here let's 74 00:02:47,220 --> 00:02:48,600 call the method of this process, 75 00:02:48,630 --> 00:02:50,580 and here let's pass single argument 76 00:02:50,610 --> 00:02:52,620 0. And it will mean that this 77 00:02:52,620 --> 00:02:54,660 process will terminate correctly 78 00:02:54,660 --> 00:02:57,960 with exit code 0. Let's add one 79 00:02:57,960 --> 00:03:00,000 more line break here like so. And 80 00:03:00,000 --> 00:03:01,560 that's actually all that we need to 81 00:03:01,560 --> 00:03:03,240 modify in our application in order 82 00:03:03,240 --> 00:03:05,790 to handle SIGINT signal. Let's 83 00:03:05,820 --> 00:03:08,220 save this file. Let's go to our 84 00:03:08,220 --> 00:03:10,230 terminal. And actually, we need to 85 00:03:10,230 --> 00:03:11,730 stop this container first, and 86 00:03:11,730 --> 00:03:14,340 afterwards rerun it over. Let's go 87 00:03:14,340 --> 00:03:16,560 here, and let's list containers, 88 00:03:16,590 --> 00:03:18,990 'docker ps'. And now let's use 'docker 89 00:03:18,990 --> 00:03:20,820 kill' command in order to quickly 90 00:03:20,820 --> 00:03:22,200 terminate process inside of the 91 00:03:22,200 --> 00:03:23,940 container because our application 92 00:03:24,090 --> 00:03:26,310 does not react to SIGTERM command 93 00:03:26,340 --> 00:03:28,560 as well. Let's enter 'docker kill', 94 00:03:28,710 --> 00:03:31,500 and here will be '1b', and 95 00:03:31,500 --> 00:03:33,570 container was terminated. Let's go 96 00:03:33,570 --> 00:03:36,000 back to this step, and let's rerun our 97 00:03:36,000 --> 00:03:38,340 application using same command. And 98 00:03:38,340 --> 00:03:40,890 of course now our changes made in 99 00:03:40,920 --> 00:03:42,930 this file will be taken into 100 00:03:42,930 --> 00:03:44,970 account and our application should 101 00:03:45,000 --> 00:03:48,120 now react to this signal. Actually, 102 00:03:48,150 --> 00:03:50,790 let me replace 'terminated' with 103 00:03:50,820 --> 00:03:53,250 'interrupted' here, because we 104 00:03:53,250 --> 00:03:54,960 actually interrupt our process. 105 00:03:55,140 --> 00:03:56,460 Let's go back to this step and 106 00:03:56,460 --> 00:03:59,340 let's rerun this command, and now 107 00:03:59,340 --> 00:04:02,040 let's press Ctrl+C, and now our 108 00:04:02,040 --> 00:04:04,050 process was terminated and I see text, 109 00:04:04,080 --> 00:04:05,670 'Application is being terminated...'. 110 00:04:06,030 --> 00:04:07,950 Basically, it seems that I have 111 00:04:07,950 --> 00:04:11,400 not saved the file. Yes, that is the case. Let's save it, 112 00:04:11,490 --> 00:04:14,310 and also let's add the handling of 113 00:04:14,310 --> 00:04:16,050 the SIGTERM signal into our 114 00:04:16,050 --> 00:04:18,240 application as well. Let me copy 115 00:04:18,269 --> 00:04:21,750 this block, paste here, and here will 116 00:04:21,750 --> 00:04:25,410 be SIGTERM, and with those lines 117 00:04:25,410 --> 00:04:27,540 in place, we will be able also to 118 00:04:27,540 --> 00:04:29,940 use 'docker stop' command that will 119 00:04:29,940 --> 00:04:32,070 quickly stop corresponding process 120 00:04:32,100 --> 00:04:33,900 inside of the container. And here 121 00:04:33,900 --> 00:04:36,150 let's type, 'Application is being 122 00:04:36,360 --> 00:04:39,450 terminated....', like so. Let's save 123 00:04:39,450 --> 00:04:42,150 this, and let's now go back to our 124 00:04:42,210 --> 00:04:44,100 application here. Let's clear 125 00:04:44,100 --> 00:04:46,350 terminal and let's rerun our 126 00:04:46,350 --> 00:04:48,600 application, 'CTRL+C'. I see, 127 00:04:48,600 --> 00:04:50,400 'Application is being interrupted...' 128 00:04:50,580 --> 00:04:52,590 And now process was terminated of 129 00:04:52,590 --> 00:04:54,510 course and there are no containers 130 00:04:54,510 --> 00:04:57,780 currently running. 'docker ps', and 131 00:04:57,780 --> 00:05:00,000 yes, that's the case. Let's rerun our 132 00:05:00,000 --> 00:05:02,700 container once again, this actually 133 00:05:02,700 --> 00:05:04,650 brand new container. And now let's 134 00:05:04,650 --> 00:05:07,410 go to this step 'docker ps', and 135 00:05:07,410 --> 00:05:09,660 let's now use 'docker stop' command 136 00:05:09,690 --> 00:05:12,120 in order to send SIGTERM signal 137 00:05:12,180 --> 00:05:14,760 to our Node.js process, 'docker 138 00:05:14,790 --> 00:05:16,830 stop'. And here let's use first 139 00:05:16,830 --> 00:05:19,391 few characters of CONTAINER ID, 'c1'; 140 00:05:19,391 --> 00:05:21,960 and now container was stopped 141 00:05:21,990 --> 00:05:23,640 pretty quickly. And that's because 142 00:05:23,670 --> 00:05:25,920 we have added handling of SIGTERM 143 00:05:25,920 --> 00:05:28,470 signal to our application. That's 144 00:05:28,500 --> 00:05:29,940 all for this lecture where I have 145 00:05:29,940 --> 00:05:31,860 demonstrated you how you're able to 146 00:05:31,860 --> 00:05:34,080 add handling of the SIGINT and the 147 00:05:34,080 --> 00:05:35,820 SIGTERM signals to your 148 00:05:35,850 --> 00:05:38,100 application. And again, we are 149 00:05:38,100 --> 00:05:39,930 required to do so because we have 150 00:05:39,930 --> 00:05:42,270 connected directly to Node.js 151 00:05:42,270 --> 00:05:44,490 process after entering such kind 152 00:05:44,490 --> 00:05:47,190 of command 'node index.js', we lauched 153 00:05:47,220 --> 00:05:49,230 a specific file using Node 154 00:05:49,230 --> 00:05:51,210 interpreter and that's why we 155 00:05:51,210 --> 00:05:53,580 connect directly to Node.js 156 00:05:53,580 --> 00:05:56,070 process. And actually, I have proved 157 00:05:56,130 --> 00:05:59,040 using those lines of code that when 158 00:05:59,040 --> 00:06:02,400 you press Ctrl+C, Docker sends SIGINT 159 00:06:02,400 --> 00:06:04,000 signal to corresponding process 160 00:06:04,290 --> 00:06:06,360 and if you use 'docker stop' command, 161 00:06:06,420 --> 00:06:08,910 Docker sends SIGTERM signal to 162 00:06:08,910 --> 00:06:11,100 corresponding process. That's all 163 00:06:11,100 --> 00:06:12,330 for this lecture. I hope that you 164 00:06:12,330 --> 00:06:13,950 enjoyed it. And next you will get a 165 00:06:13,950 --> 00:06:15,570 small challenge and you will need 166 00:06:15,570 --> 00:06:17,850 to create tiny Node.js application 167 00:06:17,880 --> 00:06:20,202 yourself. I'll see you next. Bye-Bye. 168 00:06:20,202 --> 00:06:22,229 [no audio]