-
Notifications
You must be signed in to change notification settings - Fork 1
/
startcontainer.go
56 lines (49 loc) · 1.18 KB
/
startcontainer.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 execcmd
import (
"context"
"fmt"
"time"
"github.com/docker/docker/api/types"
dockerclient "github.com/docker/docker/client"
)
// StartContainer starts a container and waits for it to be ready.
func StartContainer(
ctx context.Context,
dockerClient dockerclient.APIClient,
containerID string,
pollDur time.Duration,
) error {
if pdmin := time.Duration(100) * time.Millisecond; pollDur < pdmin {
pollDur = pdmin
}
err := dockerClient.ContainerStart(ctx, containerID, types.ContainerStartOptions{})
if err != nil {
return err
}
// wait for the container to start
for {
select {
case <-ctx.Done():
return context.Canceled
case <-time.After(pollDur):
}
ctr, err := dockerClient.ContainerInspect(ctx, containerID)
if err != nil {
return err
}
if ctr.State == nil {
continue
}
if ctr.State.Dead ||
(!ctr.State.Running && !ctr.State.Restarting && ctr.State.Status == "exited") {
return fmt.Errorf("Container failed to start with exit code: %d", ctr.State.ExitCode)
}
health := ctr.State.Health
if health != nil && (health.Status != "none" && health.Status != "healthy") {
continue
}
if ctr.State.Running {
return nil
}
}
}