github.com/rish1988/moby@v25.0.2+incompatible/client/container_stop.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"strconv"
     7  
     8  	"github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/api/types/versions"
    10  )
    11  
    12  // ContainerStop stops a container. In case the container fails to stop
    13  // gracefully within a time frame specified by the timeout argument,
    14  // it is forcefully terminated (killed).
    15  //
    16  // If the timeout is nil, the container's StopTimeout value is used, if set,
    17  // otherwise the engine default. A negative timeout value can be specified,
    18  // meaning no timeout, i.e. no forceful termination is performed.
    19  func (cli *Client) ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error {
    20  	query := url.Values{}
    21  	if options.Timeout != nil {
    22  		query.Set("t", strconv.Itoa(*options.Timeout))
    23  	}
    24  	if options.Signal != "" {
    25  		// Make sure we negotiated (if the client is configured to do so),
    26  		// as code below contains API-version specific handling of options.
    27  		//
    28  		// Normally, version-negotiation (if enabled) would not happen until
    29  		// the API request is made.
    30  		cli.checkVersion(ctx)
    31  		if versions.GreaterThanOrEqualTo(cli.version, "1.42") {
    32  			query.Set("signal", options.Signal)
    33  		}
    34  	}
    35  	resp, err := cli.post(ctx, "/containers/"+containerID+"/stop", query, nil, nil)
    36  	ensureReaderClosed(resp)
    37  	return err
    38  }