github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/daemon/stop.go (about)

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