container_wait.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if versions.LessThan(cli.ClientVersion(), "1.30") {
  30. return cli.legacyContainerWait(ctx, containerID)
  31. }
  32. resultC := make(chan container.WaitResponse)
  33. errC := make(chan error, 1)
  34. query := url.Values{}
  35. if condition != "" {
  36. query.Set("condition", string(condition))
  37. }
  38. resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", query, nil, nil)
  39. if err != nil {
  40. defer ensureReaderClosed(resp)
  41. errC <- err
  42. return resultC, errC
  43. }
  44. go func() {
  45. defer ensureReaderClosed(resp)
  46. body := resp.body
  47. responseText := bytes.NewBuffer(nil)
  48. stream := io.TeeReader(body, responseText)
  49. var res container.WaitResponse
  50. if err := json.NewDecoder(stream).Decode(&res); err != nil {
  51. // NOTE(nicks): The /wait API does not work well with HTTP proxies.
  52. // At any time, the proxy could cut off the response stream.
  53. //
  54. // But because the HTTP status has already been written, the proxy's
  55. // only option is to write a plaintext error message.
  56. //
  57. // If there's a JSON parsing error, read the real error message
  58. // off the body and send it to the client.
  59. _, _ = io.ReadAll(io.LimitReader(stream, containerWaitErrorMsgLimit))
  60. errC <- errors.New(responseText.String())
  61. return
  62. }
  63. resultC <- res
  64. }()
  65. return resultC, errC
  66. }
  67. // legacyContainerWait returns immediately and doesn't have an option to wait
  68. // until the container is removed.
  69. func (cli *Client) legacyContainerWait(ctx context.Context, containerID string) (<-chan container.WaitResponse, <-chan error) {
  70. resultC := make(chan container.WaitResponse)
  71. errC := make(chan error)
  72. go func() {
  73. resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", nil, nil, nil)
  74. if err != nil {
  75. errC <- err
  76. return
  77. }
  78. defer ensureReaderClosed(resp)
  79. var res container.WaitResponse
  80. if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
  81. errC <- err
  82. return
  83. }
  84. resultC <- res
  85. }()
  86. return resultC, errC
  87. }