github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/stop.go (about)

     1  package daemon // import "github.com/docker/docker/daemon"
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	containerpkg "github.com/docker/docker/container"
     8  	"github.com/docker/docker/errdefs"
     9  	"github.com/pkg/errors"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // ContainerStop looks for the given container and stops it.
    14  // In case the container fails to stop gracefully within a time duration
    15  // specified by the timeout argument, in seconds, it is forcefully
    16  // terminated (killed).
    17  //
    18  // If the timeout is nil, the container's StopTimeout value is used, if set,
    19  // otherwise the engine default. A negative timeout value can be specified,
    20  // meaning no timeout, i.e. no forceful termination is performed.
    21  func (daemon *Daemon) ContainerStop(name string, timeout *int) error {
    22  	container, err := daemon.GetContainer(name)
    23  	if err != nil {
    24  		return err
    25  	}
    26  	if !container.IsRunning() {
    27  		return containerNotModifiedError{running: false}
    28  	}
    29  	if timeout == nil {
    30  		stopTimeout := container.StopTimeout()
    31  		timeout = &stopTimeout
    32  	}
    33  	if err := daemon.containerStop(container, *timeout); err != nil {
    34  		return errdefs.System(errors.Wrapf(err, "cannot stop container: %s", name))
    35  	}
    36  	return nil
    37  }
    38  
    39  // containerStop sends a stop signal, waits, sends a kill signal.
    40  func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds int) error {
    41  	if !container.IsRunning() {
    42  		return nil
    43  	}
    44  
    45  	stopSignal := container.StopSignal()
    46  	// 1. Send a stop signal
    47  	if err := daemon.killPossiblyDeadProcess(container, stopSignal); err != nil {
    48  		// While normally we might "return err" here we're not going to
    49  		// because if we can't stop the container by this point then
    50  		// it's probably because it's already stopped. Meaning, between
    51  		// the time of the IsRunning() call above and now it stopped.
    52  		// Also, since the err return will be environment specific we can't
    53  		// look for any particular (common) error that would indicate
    54  		// that the process is already dead vs something else going wrong.
    55  		// So, instead we'll give it up to 2 more seconds to complete and if
    56  		// by that time the container is still running, then the error
    57  		// we got is probably valid and so we force kill it.
    58  		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
    59  		defer cancel()
    60  
    61  		if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
    62  			logrus.Infof("Container failed to stop after sending signal %d to the process, force killing", stopSignal)
    63  			if err := daemon.killPossiblyDeadProcess(container, 9); err != nil {
    64  				return err
    65  			}
    66  		}
    67  	}
    68  
    69  	// 2. Wait for the process to exit on its own
    70  	ctx := context.Background()
    71  	if seconds >= 0 {
    72  		var cancel context.CancelFunc
    73  		ctx, cancel = context.WithTimeout(ctx, time.Duration(seconds)*time.Second)
    74  		defer cancel()
    75  	}
    76  
    77  	if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
    78  		logrus.Infof("Container %v failed to exit within %d seconds of signal %d - using the force", container.ID, seconds, stopSignal)
    79  		// 3. If it doesn't, then send SIGKILL
    80  		if err := daemon.Kill(container); err != nil {
    81  			// Wait without a timeout, ignore result.
    82  			<-container.Wait(context.Background(), containerpkg.WaitConditionNotRunning)
    83  			logrus.Warn(err) // Don't return error because we only care that container is stopped, not what function stopped it
    84  		}
    85  	}
    86  
    87  	daemon.LogContainerEvent(container, "stop")
    88  	return nil
    89  }