container_stop.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 != "" {
  22. // Make sure we negotiated (if the client is configured to do so),
  23. // as code below contains API-version specific handling of options.
  24. //
  25. // Normally, version-negotiation (if enabled) would not happen until
  26. // the API request is made.
  27. cli.checkVersion(ctx)
  28. if versions.GreaterThanOrEqualTo(cli.version, "1.42") {
  29. query.Set("signal", options.Signal)
  30. }
  31. }
  32. resp, err := cli.post(ctx, "/containers/"+containerID+"/stop", query, nil, nil)
  33. ensureReaderClosed(resp)
  34. return err
  35. }