stop.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package daemon
  2. import (
  3. "context"
  4. "time"
  5. containerpkg "github.com/docker/docker/container"
  6. "github.com/docker/docker/errdefs"
  7. "github.com/pkg/errors"
  8. "github.com/sirupsen/logrus"
  9. )
  10. // ContainerStop looks for the given container and terminates it,
  11. // waiting the given number of seconds before forcefully killing the
  12. // container. If a negative number of seconds is given, ContainerStop
  13. // will wait for a graceful termination. An error is returned if the
  14. // container is not found, is already stopped, or if there is a
  15. // problem stopping the container.
  16. func (daemon *Daemon) ContainerStop(name string, seconds *int) error {
  17. container, err := daemon.GetContainer(name)
  18. if err != nil {
  19. return err
  20. }
  21. if !container.IsRunning() {
  22. return containerNotModifiedError{running: false}
  23. }
  24. if seconds == nil {
  25. stopTimeout := container.StopTimeout()
  26. seconds = &stopTimeout
  27. }
  28. if err := daemon.containerStop(container, *seconds); err != nil {
  29. return errdefs.System(errors.Wrapf(err, "cannot stop container: %s", name))
  30. }
  31. return nil
  32. }
  33. // containerStop halts a container by sending a stop signal, waiting for the given
  34. // duration in seconds, and then calling SIGKILL and waiting for the
  35. // process to exit. If a negative duration is given, Stop will wait
  36. // for the initial signal forever. If the container is not running Stop returns
  37. // immediately.
  38. func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds int) error {
  39. if !container.IsRunning() {
  40. return nil
  41. }
  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. }