stop.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package daemon
  2. import (
  3. "context"
  4. "time"
  5. containerpkg "github.com/docker/docker/container"
  6. "github.com/pkg/errors"
  7. "github.com/sirupsen/logrus"
  8. )
  9. // ContainerStop looks for the given container and terminates it,
  10. // waiting the given number of seconds before forcefully killing the
  11. // container. If a negative number of seconds is given, ContainerStop
  12. // will wait for a graceful termination. An error is returned if the
  13. // container is not found, is already stopped, or if there is a
  14. // problem stopping the container.
  15. func (daemon *Daemon) ContainerStop(name string, seconds *int) error {
  16. container, err := daemon.GetContainer(name)
  17. if err != nil {
  18. return err
  19. }
  20. if !container.IsRunning() {
  21. return containerNotModifiedError{running: false}
  22. }
  23. if seconds == nil {
  24. stopTimeout := container.StopTimeout()
  25. seconds = &stopTimeout
  26. }
  27. if err := daemon.containerStop(container, *seconds); err != nil {
  28. return errors.Wrapf(systemError{err}, "cannot stop container: %s", name)
  29. }
  30. return nil
  31. }
  32. // containerStop halts a container by sending a stop signal, waiting for the given
  33. // duration in seconds, and then calling SIGKILL and waiting for the
  34. // process to exit. If a negative duration is given, Stop will wait
  35. // for the initial signal forever. If the container is not running Stop returns
  36. // immediately.
  37. func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds int) error {
  38. if !container.IsRunning() {
  39. return nil
  40. }
  41. daemon.stopHealthchecks(container)
  42. stopSignal := container.StopSignal()
  43. // 1. Send a stop signal
  44. if err := daemon.killPossiblyDeadProcess(container, stopSignal); err != nil {
  45. // While normally we might "return err" here we're not going to
  46. // because if we can't stop the container by this point then
  47. // it's probably because it's already stopped. Meaning, between
  48. // the time of the IsRunning() call above and now it stopped.
  49. // Also, since the err return will be environment specific we can't
  50. // look for any particular (common) error that would indicate
  51. // that the process is already dead vs something else going wrong.
  52. // So, instead we'll give it up to 2 more seconds to complete and if
  53. // by that time the container is still running, then the error
  54. // we got is probably valid and so we force kill it.
  55. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  56. defer cancel()
  57. if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
  58. logrus.Infof("Container failed to stop after sending signal %d to the process, force killing", stopSignal)
  59. if err := daemon.killPossiblyDeadProcess(container, 9); err != nil {
  60. return err
  61. }
  62. }
  63. }
  64. // 2. Wait for the process to exit on its own
  65. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(seconds)*time.Second)
  66. defer cancel()
  67. if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
  68. logrus.Infof("Container %v failed to exit within %d seconds of signal %d - using the force", container.ID, seconds, stopSignal)
  69. // 3. If it doesn't, then send SIGKILL
  70. if err := daemon.Kill(container); err != nil {
  71. // Wait without a timeout, ignore result.
  72. <-container.Wait(context.Background(), containerpkg.WaitConditionNotRunning)
  73. logrus.Warn(err) // Don't return error because we only care that container is stopped, not what function stopped it
  74. }
  75. }
  76. daemon.LogContainerEvent(container, "stop")
  77. return nil
  78. }