container_wait.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "net/url"
  9. "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/api/types/versions"
  11. )
  12. const containerWaitErrorMsgLimit = 2 * 1024 /* Max: 2KiB */
  13. // ContainerWait waits until the specified container is in a certain state
  14. // indicated by the given condition, either "not-running" (default),
  15. // "next-exit", or "removed".
  16. //
  17. // If this client's API version is before 1.30, condition is ignored and
  18. // ContainerWait will return immediately with the two channels, as the server
  19. // will wait as if the condition were "not-running".
  20. //
  21. // If this client's API version is at least 1.30, ContainerWait blocks until
  22. // the request has been acknowledged by the server (with a response header),
  23. // then returns two channels on which the caller can wait for the exit status
  24. // of the container or an error if there was a problem either beginning the
  25. // wait request or in getting the response. This allows the caller to
  26. // synchronize ContainerWait with other calls, such as specifying a
  27. // "next-exit" condition before issuing a ContainerStart request.
  28. func (cli *Client) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.WaitResponse, <-chan error) {
  29. // Make sure we negotiated (if the client is configured to do so),
  30. // as code below contains API-version specific handling of options.
  31. //
  32. // Normally, version-negotiation (if enabled) would not happen until
  33. // the API request is made.
  34. cli.checkVersion(ctx)
  35. if versions.LessThan(cli.ClientVersion(), "1.30") {
  36. return cli.legacyContainerWait(ctx, containerID)
  37. }
  38. resultC := make(chan container.WaitResponse)
  39. errC := make(chan error, 1)
  40. query := url.Values{}
  41. if condition != "" {
  42. query.Set("condition", string(condition))
  43. }
  44. resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", query, nil, nil)
  45. if err != nil {
  46. defer ensureReaderClosed(resp)
  47. errC <- err
  48. return resultC, errC
  49. }
  50. go func() {
  51. defer ensureReaderClosed(resp)
  52. body := resp.body
  53. responseText := bytes.NewBuffer(nil)
  54. stream := io.TeeReader(body, responseText)
  55. var res container.WaitResponse
  56. if err := json.NewDecoder(stream).Decode(&res); err != nil {
  57. // NOTE(nicks): The /wait API does not work well with HTTP proxies.
  58. // At any time, the proxy could cut off the response stream.
  59. //
  60. // But because the HTTP status has already been written, the proxy's
  61. // only option is to write a plaintext error message.
  62. //
  63. // If there's a JSON parsing error, read the real error message
  64. // off the body and send it to the client.
  65. if errors.As(err, new(*json.SyntaxError)) {
  66. _, _ = io.ReadAll(io.LimitReader(stream, containerWaitErrorMsgLimit))
  67. errC <- errors.New(responseText.String())
  68. } else {
  69. errC <- err
  70. }
  71. return
  72. }
  73. resultC <- res
  74. }()
  75. return resultC, errC
  76. }
  77. // legacyContainerWait returns immediately and doesn't have an option to wait
  78. // until the container is removed.
  79. func (cli *Client) legacyContainerWait(ctx context.Context, containerID string) (<-chan container.WaitResponse, <-chan error) {
  80. resultC := make(chan container.WaitResponse)
  81. errC := make(chan error)
  82. go func() {
  83. resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", nil, nil, nil)
  84. if err != nil {
  85. errC <- err
  86. return
  87. }
  88. defer ensureReaderClosed(resp)
  89. var res container.WaitResponse
  90. if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
  91. errC <- err
  92. return
  93. }
  94. resultC <- res
  95. }()
  96. return resultC, errC
  97. }