github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/daemon/stop.go (about)

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