container_stop.go 1.0 KB

123456789101112131415161718192021222324252627282930
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/api/types/versions"
  8. )
  9. // ContainerStop stops a container. In case the container fails to stop
  10. // gracefully within a time frame specified by the timeout argument,
  11. // it is forcefully terminated (killed).
  12. //
  13. // If the timeout is nil, the container's StopTimeout value is used, if set,
  14. // otherwise the engine default. A negative timeout value can be specified,
  15. // meaning no timeout, i.e. no forceful termination is performed.
  16. func (cli *Client) ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error {
  17. query := url.Values{}
  18. if options.Timeout != nil {
  19. query.Set("t", strconv.Itoa(*options.Timeout))
  20. }
  21. if options.Signal != "" && versions.GreaterThanOrEqualTo(cli.version, "1.42") {
  22. query.Set("signal", options.Signal)
  23. }
  24. resp, err := cli.post(ctx, "/containers/"+containerID+"/stop", query, nil, nil)
  25. ensureReaderClosed(resp)
  26. return err
  27. }