github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/client/container_wait.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "context" 5 "encoding/json" 6 "net/url" 7 8 "github.com/docker/docker/api/types/container" 9 "github.com/docker/docker/api/types/versions" 10 ) 11 12 // ContainerWait waits until the specified container is in a certain state 13 // indicated by the given condition, either "not-running" (default), 14 // "next-exit", or "removed". 15 // 16 // If this client's API version is before 1.30, condition is ignored and 17 // ContainerWait will return immediately with the two channels, as the server 18 // will wait as if the condition were "not-running". 19 // 20 // If this client's API version is at least 1.30, ContainerWait blocks until 21 // the request has been acknowledged by the server (with a response header), 22 // then returns two channels on which the caller can wait for the exit status 23 // of the container or an error if there was a problem either beginning the 24 // wait request or in getting the response. This allows the caller to 25 // synchronize ContainerWait with other calls, such as specifying a 26 // "next-exit" condition before issuing a ContainerStart request. 27 func (cli *Client) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) { 28 if versions.LessThan(cli.ClientVersion(), "1.30") { 29 return cli.legacyContainerWait(ctx, containerID) 30 } 31 32 resultC := make(chan container.ContainerWaitOKBody) 33 errC := make(chan error, 1) 34 35 query := url.Values{} 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 45 go func() { 46 defer ensureReaderClosed(resp) 47 var res container.ContainerWaitOKBody 48 if err := json.NewDecoder(resp.body).Decode(&res); err != nil { 49 errC <- err 50 return 51 } 52 53 resultC <- res 54 }() 55 56 return resultC, errC 57 } 58 59 // legacyContainerWait returns immediately and doesn't have an option to wait 60 // until the container is removed. 61 func (cli *Client) legacyContainerWait(ctx context.Context, containerID string) (<-chan container.ContainerWaitOKBody, <-chan error) { 62 resultC := make(chan container.ContainerWaitOKBody) 63 errC := make(chan error) 64 65 go func() { 66 resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", nil, nil, nil) 67 if err != nil { 68 errC <- err 69 return 70 } 71 defer ensureReaderClosed(resp) 72 73 var res container.ContainerWaitOKBody 74 if err := json.NewDecoder(resp.body).Decode(&res); err != nil { 75 errC <- err 76 return 77 } 78 79 resultC <- res 80 }() 81 82 return resultC, errC 83 }