-: In the last section, we started taking a look
at how we can get Docker Compose to automatically maintain
and restart our containers.
We added in a little bit of code
to our node server to make sure
that anytime we visited our route
the server would automatically exit.
When we visited that route
we were then able to flip on over
to our terminal and see that our container stopped.
We were also able to run Docker ps
and verify that, yep
we only have one container now running.
So in this section we're gonna figure out
exactly how we can get Docker Compose
to automatically restart that crashed or stopped container.
Now the first thing I wanna tell you about
is that little line of code that we added inside
of our index.js file inside of here.
We added in process.exit
and passed in the number zero right there.
The zero that we're adding in right there
is what we refer to as a exit status code.
By adding in a zero, we are indicating
that we just exited our node server
and we exited because we wanted to.
Everything is okay.
We didn't run into any errors.
We stopped that running process
because we meant to.
if we added in a status code of anything besides zero
so 1, 2, 3, 300, 400, 500,
5,000, any number besides zero
that means that our running process exited
because an error occurred or something went wrong.
Now that's a very important little tidbit to be aware of
because it's going to affect exactly how Docker decides
whether or not to restart our containers.
So just keep that in your mind for just a moment.
Okay, so here's how we're going to get Docker Compose
to automatically restart a container
we're going to specify something called a restart policy
inside of our Docker Compose file
there are four different restart policies.
So we have access to.
By default
we have the no restart policy assigned
to all of our containers.
The no restart policy means if this thing crashes
for any reason, do not attempt to restart the container.
We also get the always restart policy
which as you might guess means
if the container stops for absolutely any reason whatsoever
automatically attempt to restart it.
We get two others.
So we'll talk about in just a moment,
but before we do
let's try adding in the always restart policy
to our node server
and then try visiting it again
and trying to get it to crash.
So let's get to it.
I'm gonna flip back on my code editor.
I'm gonna open up my Docker Compose file
and then I'm going to add a new line right
after our node app specification right here.
I'm gonna say restart always.
So as you might guess,
this specifies the always restart policy
for our node app container.
Notice how we specified a restart policy
for one specific service
or one specific container.
If we wanted to add a restart policy to the writer's server
we would have to add it onto that one specifically as well.
But right now we'll go with just node app.
Now I'm gonna save this file
and we're gonna try bringing
up our Docker Compose containers again
and seeing what happens when our node application crashes.
Okay, so I'm gonna flip back on my terminal.
I'm going to hit control C to stop that
Docker Compose process
and then I will start them back up with
Docker Compose up.
We'll now flip open our browser again.
I'll open a new tab.
I do encourage you to make a new tab here
as opposed to just refreshing the existing one you have.
Sometimes Chrome does not quite refresh the server
as you would expect if you reuse your tab.
So I'm going to visit local host :4,001.
Again, I get the error message here
which is okay if I flip backward my terminal
after a second and two, you'll notice
that we have gotten that exit message right there.
But then very quickly after that
we got this new color here that essentially means
that we just restarted that running container.
So we've restarted the container
and we again have a copy of node js listing
on port 81 inside the container.
One thing that's kind of interesting here is
so you'll notice that we are seeing multiple listening on
Port 81 messages here.
That's totally fine.
Technically, when you and I are stopping our container
with that process.exit line
we're not actually deleting the container or stopping it
or anything like or killing it or anything like that.
Technically the container just stopping
and when Docker Compose decides to restart the container
it's reusing the one that we had previously created.
And so when we reuse that container
we attach to the standard out log
which has all the previous messages appended on it as well.
And that's why we're seeing multiple listing
on port 81 right here.
All right, so back over to our diagrams.
So as you might guess, the always restart policy says,
'Hey if this container stops for any reason
just go ahead and attempt to restart it.'
And we just saw that in action when we crashed our server.
Now we also get access to the on failure restart policy.
As you might guess,
the on failure policy
is only going to attempt to restart the running container
if we get a error code.
So a status code
from that process.exit line other than on zero.
Let's try testing that
out right now and just seeing what happens.
Now, I'm going to leave process.exit inside
of index.js at zero right here.
Okay, so we're still going to have an exit code of zero
which means we exited because we wanted to.
But inside of my Docker Compose file
I'm gonna change the restart policy to be
on - failure
like so.
Now I'll go back over to my terminal,
I'll stop that running process with control C.
I'll then start my Docker Compose group back up
with Docker Compose up.
Now if I open up my browser again
I'll again create a new tab and visit local host 4001.
I don't get any feedback here.
If I go to my terminal,
I'll see that I have an exit with status code Zero
and now in this case we say that,
'Hey we exited because we wanted to'
No error whatsoever
but we specified a restart policy that says,
'Only attempt to restart it if we had a failure.'
So in this case
we're never going to see the container restart.
We would have to change our exit code
in order to see it restarted when using the
on failure policy.
Now, if you want to test that out yourself
feel free to do so.
You could change the exit of zero right here
to one or 10 or 100
or essentially any non-zero number.
Just remember that if you do that,
you will have to rebuild your image
by using Docker Compose up dash dash built like so.
So again, if you wanna try it yourself
just don't forget the dash dash built.
Now, the last policy that I wanna tell you
about is the unless stopped policy.
So unless stopped means
always attempt to restart this container
unless you or I at the command line
forcibly tell that container to stop
by running Docker stop.
So unless stopped right here
basically means just always restart it.
But if you and I really want it to stop,
then it will stop.
Now, one quick note here about the 'no' restart policy
you'll notice it in this case
we put quotes around,
'No' that's actually on purpose.
So with all the other policies
like always on failure and unless stopped,
you can just type in the raw policy name
into your Docker Compose file.
So on failure like that right there.
But if you're going to make use of the 'no' restart policy
you specifically have to put it in quotes
either double or single.
The reason for that is that in a YAML file,
the value no
and you can see when I type it in
it appears as orange right here.
So in a YAML file,
the value no gets interpreted as false.
So a false restart policy is
different than a string of no.
So that's why 'no' specifically needs to be added in quotes
so that it understands this is not a Boolean false
it's specifically the string, 'no.'
Okay, so that's pretty much it on restart policies.
Now, the last thing you might be a little bit curious about
is why would we ever want to use always
versus on failure?
Well, it really comes down
to the purpose of your Docker container.
In some cases
you might have a container that you always, always
always want to make sure is running.
A good example of this would be a web server.
If you are running some public web application
chances are a hundred percent
of the time you want that server to be available.
And so if you are running some type
of web application, well
I would kind of expect you to use the always restart policy
so that you are
at least always attempting to keep your server running.
On the other hand
if you are running some type of worker process
like some container that is meant to do some amount
of processing on some file and then naturally exit
that would probably be a good use case
for the on failure policy
because that worker container might eventually
finish its job,
it might finish processing a file
and when it's completed processing that file
Well, you probably don't wanna start it back
up because it finished its job
and you should probably just let it die
and let it close out and not get restarted.
And so if you have a worker process of some sort
well that's probably where I would expect you to use the
on failure policy.
Okay, so that's pretty much it.
That's how we get Docker Compose to automatically
maintain our containers.
Now, there's one last little note
that I wanna share around Docker Compose.
So let's take a quick break right here
and continue in the next section.