[Intro Music]
We have just successfully created
a small Express web server
application using Node.js, and
we have executed this application.
And before that we have installed
some additional packages, namely
express package along with its
dependencies using 'npm install'
command. And all of that we have
performed inside of the Node.js
container. There was no NPM, or
Node running on our local computer.
But now there is a challenge for
us, and you see that our
application is not terminated when
we press key combination Ctrl+C.
And when SIGINT signal is sent to our
Node.js process, it is sent
directly to our process. And notice
that in this command, we have
actually overridden default command,
this node command, using custom
command, and we run index.js file
using Node interpreter. And we have
also mapped port of our Express
application, and we have exposed it
to external port 3000. And we have
also used the customer working
directory, and we have mapped our local
folder using this option. And we
have mapped this folder to 'app'
folder inside of the container.
Great. Let's now modify a bit our
application, and let's add handling
of the SIGINT signal. I want to
show you that because it is
important for understanding of how
we're actually connected to node
process using Docker. Let's go to
our tiny application here, and
let's open up index.js file
located in the 'express' folder. Let
me hide this left pane. And let's
add additional package here, and
it is actually default package Node.js
package called 'process'. And
afterwards, we will use this
package for handling of the SIGINT
signal. Let's do that. Let's
require additional package 'const
process = require('process')', like so.
And next, for example, here let's
add following lines, 'process.on()'
And here first argument will
be 'SIGINT', and second will be
callback function, I'll use arrow
function expression syntax. Again,
if you don't know Node.js syntax, and
don't know JavaScript, please
nevermind, I want to just show you
how you're able to handle SIGINT
signal inside of the process inside
of the container. Okay, let's add
here arrow function syntax like so.
And here inside of this arrow
function, let's do following.
Let's print to the terminal message
'Application is being terminated
...', like so. And here on the
next line, let's exit from the
process using the following syntax,
'process.exit'(), and here let's
call the method of this process,
and here let's pass single argument
0. And it will mean that this
process will terminate correctly
with exit code 0. Let's add one
more line break here like so. And
that's actually all that we need to
modify in our application in order
to handle SIGINT signal. Let's
save this file. Let's go to our
terminal. And actually, we need to
stop this container first, and
afterwards rerun it over. Let's go
here, and let's list containers,
'docker ps'. And now let's use 'docker
kill' command in order to quickly
terminate process inside of the
container because our application
does not react to SIGTERM command
as well. Let's enter 'docker kill',
and here will be '1b', and
container was terminated. Let's go
back to this step, and let's rerun our
application using same command. And
of course now our changes made in
this file will be taken into
account and our application should
now react to this signal. Actually,
let me replace 'terminated' with
'interrupted' here, because we
actually interrupt our process.
Let's go back to this step and
let's rerun this command, and now
let's press Ctrl+C, and now our
process was terminated and I see text,
'Application is being terminated...'.
Basically, it seems that I have
not saved the file. Yes, that is the case. Let's save it,
and also let's add the handling of
the SIGTERM signal into our
application as well. Let me copy
this block, paste here, and here will
be SIGTERM, and with those lines
in place, we will be able also to
use 'docker stop' command that will
quickly stop corresponding process
inside of the container. And here
let's type, 'Application is being
terminated....', like so. Let's save
this, and let's now go back to our
application here. Let's clear
terminal and let's rerun our
application, 'CTRL+C'. I see,
'Application is being interrupted...'
And now process was terminated of
course and there are no containers
currently running. 'docker ps', and
yes, that's the case. Let's rerun our
container once again, this actually
brand new container. And now let's
go to this step 'docker ps', and
let's now use 'docker stop' command
in order to send SIGTERM signal
to our Node.js process, 'docker
stop'. And here let's use first
few characters of CONTAINER ID, 'c1';
and now container was stopped
pretty quickly. And that's because
we have added handling of SIGTERM
signal to our application. That's
all for this lecture where I have
demonstrated you how you're able to
add handling of the SIGINT and the
SIGTERM signals to your
application. And again, we are
required to do so because we have
connected directly to Node.js
process after entering such kind
of command 'node index.js', we lauched
a specific file using Node
interpreter and that's why we
connect directly to Node.js
process. And actually, I have proved
using those lines of code that when
you press Ctrl+C, Docker sends SIGINT
signal to corresponding process
and if you use 'docker stop' command,
Docker sends SIGTERM signal to
corresponding process. That's all
for this lecture. I hope that you
enjoyed it. And next you will get a
small challenge and you will need
to create tiny Node.js application
yourself. I'll see you next. Bye-Bye.
[no audio]