2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-07-31 20:40:15 +00:00
|
|
|
|
2015-09-16 18:56:26 +00:00
|
|
|
import (
|
2017-03-30 20:52:40 +00:00
|
|
|
"context"
|
2015-11-02 23:25:26 +00:00
|
|
|
"time"
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
containerpkg "github.com/docker/docker/container"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-07-19 14:20:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-09-16 18:56:26 +00:00
|
|
|
)
|
2015-03-25 07:44:12 +00:00
|
|
|
|
2018-04-16 23:58:42 +00:00
|
|
|
// ContainerStop looks for the given container and stops it.
|
|
|
|
// In case the container fails to stop gracefully within a time duration
|
|
|
|
// specified by the timeout argument, in seconds, it is forcefully
|
|
|
|
// terminated (killed).
|
|
|
|
//
|
|
|
|
// If the timeout is nil, the container's StopTimeout value is used, if set,
|
|
|
|
// otherwise the engine default. A negative timeout value can be specified,
|
|
|
|
// meaning no timeout, i.e. no forceful termination is performed.
|
|
|
|
func (daemon *Daemon) ContainerStop(name string, timeout *int) error {
|
2015-12-11 17:39:28 +00:00
|
|
|
container, err := daemon.GetContainer(name)
|
2014-12-16 23:06:35 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-07-31 20:40:15 +00:00
|
|
|
}
|
2014-12-16 23:06:35 +00:00
|
|
|
if !container.IsRunning() {
|
2017-07-19 14:20:13 +00:00
|
|
|
return containerNotModifiedError{running: false}
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2018-04-16 23:58:42 +00:00
|
|
|
if timeout == nil {
|
2016-06-07 03:29:05 +00:00
|
|
|
stopTimeout := container.StopTimeout()
|
2018-04-16 23:58:42 +00:00
|
|
|
timeout = &stopTimeout
|
2016-06-07 03:29:05 +00:00
|
|
|
}
|
2018-04-16 23:58:42 +00:00
|
|
|
if err := daemon.containerStop(container, *timeout); err != nil {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.System(errors.Wrapf(err, "cannot stop container: %s", name))
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-07-31 20:40:15 +00:00
|
|
|
}
|
2015-11-02 23:25:26 +00:00
|
|
|
|
2018-04-16 23:58:42 +00:00
|
|
|
// containerStop sends a stop signal, waits, sends a kill signal.
|
2017-03-31 03:01:41 +00:00
|
|
|
func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds int) error {
|
2020-10-24 20:15:58 +00:00
|
|
|
// TODO propagate a context down to this function
|
|
|
|
ctx := context.TODO()
|
2015-11-02 23:25:26 +00:00
|
|
|
if !container.IsRunning() {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-24 20:15:58 +00:00
|
|
|
var wait time.Duration
|
|
|
|
if seconds >= 0 {
|
|
|
|
wait = time.Duration(seconds) * time.Second
|
|
|
|
}
|
|
|
|
success := func() error {
|
|
|
|
daemon.LogContainerEvent(container, "stop")
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 08:06:45 +00:00
|
|
|
stopSignal := container.StopSignal()
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2020-10-24 20:15:58 +00:00
|
|
|
// 1. Send a stop signal
|
|
|
|
err := daemon.killPossiblyDeadProcess(container, stopSignal)
|
|
|
|
if err != nil {
|
|
|
|
wait = 2 * time.Second
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:15:58 +00:00
|
|
|
var subCtx context.Context
|
|
|
|
var cancel context.CancelFunc
|
2018-04-16 23:58:42 +00:00
|
|
|
if seconds >= 0 {
|
2020-10-24 20:15:58 +00:00
|
|
|
subCtx, cancel = context.WithTimeout(ctx, wait)
|
|
|
|
} else {
|
|
|
|
subCtx, cancel = context.WithCancel(ctx)
|
2018-04-16 23:58:42 +00:00
|
|
|
}
|
2020-10-24 20:15:58 +00:00
|
|
|
defer cancel()
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2020-10-24 20:15:58 +00:00
|
|
|
if status := <-container.Wait(subCtx, containerpkg.WaitConditionNotRunning); status.Err() == nil {
|
|
|
|
// container did exit, so ignore any previous errors and return
|
|
|
|
return success()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// the container has still not exited, and the kill function errored, so log the error here:
|
|
|
|
logrus.WithError(err).WithField("container", container.ID).Errorf("Error sending stop (signal %d) to container", stopSignal)
|
|
|
|
}
|
|
|
|
if seconds < 0 {
|
|
|
|
// if the client requested that we never kill / wait forever, but container.Wait was still
|
|
|
|
// interrupted (parent context cancelled, for example), we should propagate the signal failure
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-08 20:29:57 +00:00
|
|
|
logrus.WithField("container", container.ID).Infof("Container failed to exit within %s of signal %d - using the force", wait, stopSignal)
|
2020-10-24 20:15:58 +00:00
|
|
|
// Stop either failed or container didnt exit, so fallback to kill.
|
|
|
|
if err := daemon.Kill(container); err != nil {
|
|
|
|
// got a kill error, but give container 2 more seconds to exit just in case
|
|
|
|
subCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if status := <-container.Wait(subCtx, containerpkg.WaitConditionNotRunning); status.Err() == nil {
|
|
|
|
// container did exit, so ignore error and return
|
|
|
|
return success()
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|
2020-10-24 20:15:58 +00:00
|
|
|
logrus.WithError(err).WithField("container", container.ID).Error("Error killing the container")
|
|
|
|
return err
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:15:58 +00:00
|
|
|
return success()
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|