-: One thing that we have not discussed
in the docker world just yet
is how to deal with containers that crash
for some given reason.
So it's entirely possible that you and I
might be running a server
of some sort inside of a container
and maybe that server experiences some error
that cause it to hang or crash.
In this section, we're gonna start taking a look
at a couple of different ways
that we can mitigate that behavior
and figure out how to essentially restart a container
when the software inside of it has an error of some sort.
To get started, we're gonna first add in a little bit
of code to our index.js file to make sure
that our server crashes entirely anytime
someone visits our root route.
So we're gonna add in
kind of a arbitrary forced crash this thing
and then figure out how to deal with it
using docker compose.
So to get started inside of my index.js file,
I'm gonna add in a third require statement here at the top.
It's gonna say const process is require process
and then to make sure that my server crashes
anytime someone tries to access the root route,
I'll find the root route handler right here.
I'll add a new line inside the function body
and I'll say process.exit zero, like so.
Now talk about why we are passing
in the zero right there in just a moment.
But first, let's test this out
and make sure that we can actually get our server
to crash any time that we try to visit this root route.
So I'm gonna flip on over
to my terminal and I'll run docker compose up.
And since we just made a little change
to the code inside one of our images
I'm gonna add on the dash dash build flag
to make sure that we attempt to rebuild that container.
So I'll run that, we're going to rebuild that container
and then everything starts up
and I see my running radar server
and I'm also listening on Port 8081,
but you and I know
that we actually changed the listening port to 4001
inside of our docker composed file right here.
All right, so I'm going to now open up my web browser
and I'm going to navigate to local host colon 4001.
Once I navigate over there,
I'm gonna see an air message like this appear on the screen.
And if I flip back over to my terminal,
you'll notice that it says right here
we exited with code zero.
And so at this point, essentially our running container
and the software inside of it has crashed.
I now want you to try opening up a second tab
and running docker PS.
When we do so you'll notice
that we are now down to just one running container,
just the redis container
and our node server is no longer running whatsoever.
So clearly that thing has crashed
and we might want to automatically restart it
at some point in time.
So let's take a quick pause right here
and take a look at what our different options are
in the next section.
So quick break and I'll see you in just a minute.