github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/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  		if err := cli.checkVersion(ctx); err != nil {
    31  			return err
    32  		}
    33  		if versions.GreaterThanOrEqualTo(cli.version, "1.42") {
    34  			query.Set("signal", options.Signal)
    35  		}
    36  	}
    37  	resp, err := cli.post(ctx, "/containers/"+containerID+"/stop", query, nil, nil)
    38  	ensureReaderClosed(resp)
    39  	return err
    40  }