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