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"
|
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2021-08-20 22:23:26 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2023-08-26 13:24:46 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2021-08-21 22:46:07 +00:00
|
|
|
"github.com/docker/docker/container"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2023-10-20 15:44:30 +00:00
|
|
|
"github.com/docker/docker/internal/compatcontext"
|
2021-08-22 10:46:43 +00:00
|
|
|
"github.com/moby/sys/signal"
|
2017-07-19 14:20:13 +00:00
|
|
|
"github.com/pkg/errors"
|
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.
|
2021-08-20 22:23:26 +00:00
|
|
|
func (daemon *Daemon) ContainerStop(ctx context.Context, name string, options containertypes.StopOptions) error {
|
2021-08-21 22:46:07 +00:00
|
|
|
ctr, 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
|
|
|
}
|
2021-08-21 22:46:07 +00:00
|
|
|
if !ctr.IsRunning() {
|
2023-08-11 19:07:19 +00:00
|
|
|
// This is not an actual error, but produces a 304 "not modified"
|
|
|
|
// when returned through the API to indicates the container is
|
|
|
|
// already in the desired state. It's implemented as an error
|
|
|
|
// to make the code calling this function terminate early (as
|
|
|
|
// no further processing is needed).
|
|
|
|
return errdefs.NotModified(errors.New("container is already stopped"))
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2021-08-20 22:23:26 +00:00
|
|
|
err = daemon.containerStop(ctx, ctr, options)
|
2021-08-20 20:43:13 +00:00
|
|
|
if 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
|
|
|
|
2023-10-20 15:44:30 +00:00
|
|
|
// containerStop sends a stop signal, waits, sends a kill signal. It uses
|
|
|
|
// a [context.WithoutCancel], so cancelling the context does not cancel
|
|
|
|
// the request to stop the container.
|
|
|
|
func (daemon *Daemon) containerStop(ctx context.Context, ctr *container.Container, options containertypes.StopOptions) (retErr error) {
|
|
|
|
// Cancelling the request should not cancel the stop.
|
|
|
|
ctx = compatcontext.WithoutCancel(ctx)
|
2023-06-13 21:18:04 +00:00
|
|
|
|
2021-08-21 22:46:07 +00:00
|
|
|
if !ctr.IsRunning() {
|
2015-11-02 23:25:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-20 20:43:13 +00:00
|
|
|
|
|
|
|
var (
|
2022-05-01 23:16:56 +00:00
|
|
|
stopSignal = ctr.StopSignal()
|
2021-08-20 20:43:13 +00:00
|
|
|
stopTimeout = ctr.StopTimeout()
|
|
|
|
)
|
2021-08-22 10:46:43 +00:00
|
|
|
if options.Signal != "" {
|
|
|
|
sig, err := signal.ParseSignal(options.Signal)
|
|
|
|
if err != nil {
|
|
|
|
return errdefs.InvalidParameter(err)
|
|
|
|
}
|
2022-05-01 22:28:17 +00:00
|
|
|
stopSignal = sig
|
2021-08-22 10:46:43 +00:00
|
|
|
}
|
2021-08-20 22:23:26 +00:00
|
|
|
if options.Timeout != nil {
|
|
|
|
stopTimeout = *options.Timeout
|
2021-08-20 20:43:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:15:58 +00:00
|
|
|
var wait time.Duration
|
2021-08-20 20:43:13 +00:00
|
|
|
if stopTimeout >= 0 {
|
|
|
|
wait = time.Duration(stopTimeout) * time.Second
|
2020-10-24 20:15:58 +00:00
|
|
|
}
|
2021-08-20 20:57:09 +00:00
|
|
|
defer func() {
|
|
|
|
if retErr == nil {
|
2023-08-26 13:24:46 +00:00
|
|
|
daemon.LogContainerEvent(ctr, events.ActionStop)
|
2021-08-20 20:57:09 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2020-10-24 20:15:58 +00:00
|
|
|
// 1. Send a stop signal
|
2021-08-21 22:46:07 +00:00
|
|
|
err := daemon.killPossiblyDeadProcess(ctr, stopSignal)
|
2020-10-24 20:15:58 +00:00
|
|
|
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
|
2021-08-20 20:43:13 +00:00
|
|
|
if stopTimeout >= 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
|
|
|
|
2021-08-21 22:46:07 +00:00
|
|
|
if status := <-ctr.Wait(subCtx, container.WaitConditionNotRunning); status.Err() == nil {
|
2020-10-24 20:15:58 +00:00
|
|
|
// container did exit, so ignore any previous errors and return
|
2021-08-20 20:57:09 +00:00
|
|
|
return nil
|
2020-10-24 20:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// the container has still not exited, and the kill function errored, so log the error here:
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).WithError(err).WithField("container", ctr.ID).Errorf("Error sending stop (signal %d) to container", stopSignal)
|
2020-10-24 20:15:58 +00:00
|
|
|
}
|
2021-08-20 20:43:13 +00:00
|
|
|
if stopTimeout < 0 {
|
2020-10-24 20:15:58 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).WithField("container", ctr.ID).Infof("Container failed to exit within %s of signal %d - using the force", wait, stopSignal)
|
2021-08-20 20:57:09 +00:00
|
|
|
|
|
|
|
// Stop either failed or container didn't exit, so fallback to kill.
|
2021-08-21 22:46:07 +00:00
|
|
|
if err := daemon.Kill(ctr); err != nil {
|
2020-10-24 20:15:58 +00:00
|
|
|
// 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()
|
2021-08-20 20:57:09 +00:00
|
|
|
status := <-ctr.Wait(subCtx, container.WaitConditionNotRunning)
|
|
|
|
if status.Err() != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).WithError(err).WithField("container", ctr.ID).Errorf("error killing container: %v", status.Err())
|
2021-08-20 20:57:09 +00:00
|
|
|
return err
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|
2021-08-20 20:57:09 +00:00
|
|
|
// container did exit, so ignore previous errors and continue
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 20:57:09 +00:00
|
|
|
return nil
|
2015-11-02 23:25:26 +00:00
|
|
|
}
|