In all these commands we've been running with Docker create and start.
There's kind of one oddity that I want to show you really quickly.
Let's do a container creation and then a start and you'll see some kind of weird behavior.
I'm gonna run a docker create Busy Box and then we're gonna setup that ping command again.
So I'll say ping Google dot com that's going to create the container.
I can then copy the idea right here and do Docker start and paste that idea and you'll notice that I
did not put in the dash a.
So I have not attached this container when I run that command.
I see the idea of the container echoed back to me.
And then I can do a docker logs and paste that idea in and I'll see that.
Yep it is in fact running the pin command.
And if I do the docker P.S. command I can see up that container is running and it's continuing to execute
the pin command.
Now there's just one kind of odd thing here.
How do we actually stop this container.
Remember the pin command is going to essentially go on forever and we have to actually send it a signal
until it Hey ping command please stop.
Like you're all done.
No need to reach out to Google anymore.
So we need to figure out a way to stop this running container in the past we've just been hitting control
C on our keyboard or even allowed the container to automatically stop itself as was the case with the
Echo command.
So in order to stop a container that seems to be running amuck on its own we can issue either the docker
stop command or the docker kill command.
Both these are going to stop the running container but they look like they kind of do the same thing
here so what's the difference between them.
Well here's what happens behind the scenes when you issue a docker stop command a hardware signal is
sent to the process.
The primary process inside that container in the case of Docker stop.
We send a sig term message which is short for terminate signal.
It's a message that's going to be received by the process telling it essentially to shut down on its
own time sic term is used.
Any time that you want to stop a process inside of your container and shut the container down and you
want to give that process inside there a little bit of time to shut itself down and do a little bit
of cleanup.
A lot of different programming languages have the ability for you to listen for these signals inside
of your codebase.
And as soon as you get that signal you could attempt to do a little bit of cleanup or maybe save some
file or emit some message or something like that.
On the other hand the docker kill command issue is a sig kill or kills signal to the primary running
process inside the container so kill it essentially means you have to shut down right now and you do
not get to do any additional work so ideally we always stop a container with the docker stop command
in order to get the running process inside of it a little bit of time to shut itself down.
Otherwise if it feels like the container has locked up and it's not responding to the docker stop command
then we could issue Docker kill instead.
Now one kind of little oddity or interesting thing about Docker stop when you issue Docker stop to a
container.
If the container does not automatically stop in 10 seconds then Docker is going to automatically fall
back to issuing the docker kill command.
So essentially at Docker stop is us being nice but it's only got 10 seconds to actually shut down.
So let's now try using these commands on that kind of runaway container that we have right now.
So again do Docker P.S. And I'll see that the pinging google doc command hears me pin Koogle dot com
container is still running.
So going to take the idea right there and I'll issue Docker stop and then I'll paste that idea in and
then we're going to wait and notice how we're kind of waiting here.
We're going to end up waiting just about ten seconds.
We're waiting 10 seconds because it turns out that the ping command does not properly respond to a sig
term message.
The ping command doesn't really have the ability to say oh yeah I understand you want me to shut down
the ping command is rather an analyst term for it.
The ping command wants to do its own thing and just wants to run forever.
And so after we waited those 10 seconds eventually the kill signal was sent to it telling it Hey ping
you're done.
Get out of here.
Shut yourself down.
And so that's why we had to wait a couple of seconds.
Let's now take that container idea again.
We're going to start the container back up and try killing it this time with using Docker kill Sol first.
Start the container back up with Docker start.
I'll pace the idea in and then I'll issue a docker kill and paste the idea again and this time you're
going to see that's it instantly dead.
No Grace Period.
Just get out of here.
All right.
So that's it.
That's the two commands that we're going to use for closing down containers that are running.
Remember you can always get the idea the container ahead of time with Docker P.S. you can copy the I.D.
and then run either dock or stop or Docker kill.
And in general I do recommend use Docker stopped to give the processes inside there a chance to shut
down and if they don't shut down nice nicely nicely as we saw with the pin command.
Eventually Docker is going to fall back to issuing a kill on the container instead.
All right.
So that's pretty much it.
Let's take another brief pause and continue in the next section.