container_restart.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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. // ContainerRestart stops and starts a container again.
  10. // It makes the daemon wait for the container to be up again for
  11. // a specific amount of time, given the timeout.
  12. func (cli *Client) ContainerRestart(ctx context.Context, containerID string, options container.StopOptions) error {
  13. query := url.Values{}
  14. if options.Timeout != nil {
  15. query.Set("t", strconv.Itoa(*options.Timeout))
  16. }
  17. if options.Signal != "" {
  18. // Make sure we negotiated (if the client is configured to do so),
  19. // as code below contains API-version specific handling of options.
  20. //
  21. // Normally, version-negotiation (if enabled) would not happen until
  22. // the API request is made.
  23. cli.checkVersion(ctx)
  24. if versions.GreaterThanOrEqualTo(cli.version, "1.42") {
  25. query.Set("signal", options.Signal)
  26. }
  27. }
  28. resp, err := cli.post(ctx, "/containers/"+containerID+"/restart", query, nil, nil)
  29. ensureReaderClosed(resp)
  30. return err
  31. }