MCPcopy Index your code
hub / github.com/coder/envbox / WaitForDaemon

Function WaitForDaemon

dockerutil/daemon.go:12–56  ·  view source on GitHub ↗

WaitForDaemon waits for a Docker daemon to startup. It waits a max of 5m before giving up.

(ctx context.Context, client Client)

Source from the content-addressed store, hash-verified

10// WaitForDaemon waits for a Docker daemon to startup. It waits a max
11// of 5m before giving up.
12func WaitForDaemon(ctx context.Context, client Client) error {
13 ticker := time.NewTicker(time.Millisecond * 250)
14 defer ticker.Stop()
15
16 ctx, cancel := context.WithTimeout(ctx, time.Minute*5)
17 defer cancel()
18
19 pingCtx, cancel := context.WithTimeout(ctx, time.Second)
20 defer cancel()
21
22 _, err := client.Ping(pingCtx)
23 if err == nil {
24 // We pinged successfully!
25 return nil
26 }
27
28 for {
29 select {
30 case <-ctx.Done():
31 return ctx.Err()
32 case <-ticker.C:
33 }
34
35 err := func() error {
36 pingCtx, cancel := context.WithTimeout(ctx, time.Second)
37 defer cancel()
38
39 _, err := client.Ping(pingCtx)
40 return err
41 }()
42 if err == nil {
43 // We pinged successfully!
44 return nil
45 }
46
47 // If its a connection failed error we can ignore and continue polling.
48 // It's likely that dockerd just isn't fully setup yet.
49 if dockerclient.IsErrConnectionFailed(err) || pingCtx.Err() != nil {
50 continue
51 }
52
53 // If its something else, we return it.
54 return err
55 }
56}

Callers 1

dockerCmdFunction · 0.92

Calls 2

StopMethod · 0.80
PingMethod · 0.80

Tested by

no test coverage detected