WaitForDaemon waits for a Docker daemon to startup. It waits a max of 5m before giving up.
(ctx context.Context, client Client)
| 10 | // WaitForDaemon waits for a Docker daemon to startup. It waits a max |
| 11 | // of 5m before giving up. |
| 12 | func 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 | } |