Skip to content

Commit

Permalink
fix(docker_container): only wait when healthcheck defined
Browse files Browse the repository at this point in the history
  • Loading branch information
FalcoSuessgott committed Aug 16, 2024
1 parent 7155ab0 commit b0a85d7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/provider/resource_docker_container_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,28 +496,40 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("Unable to start container: %s", err)
}

if d.Get("wait").(bool) {

_ , hasHealthCheck := d.GetOk("healthcheck")

if d.Get("wait").(bool) && hasHealthCheck {
waitForHealthyState := func(result chan<- error) {
for {
infos, err := client.ContainerInspect(ctx, retContainer.ID)
if err != nil {
result <- fmt.Errorf("error inspecting container state: %s", err)
break
}

if infos.ContainerJSONBase == nil || infos.ContainerJSONBase.State == nil || infos.ContainerJSONBase.State.Health == nil {
result <- fmt.Errorf("invalid container state: missing required fields")
break
}
//infos.ContainerJSONBase.State.Health is only set when there is a healthcheck defined on the container resource

if infos.ContainerJSONBase.State.Health.Status == types.Healthy {
log.Printf("[DEBUG] container state is healthy")
break
result <- nil
return
}

log.Printf("[DEBUG] waiting for container healthy state")
time.Sleep(time.Second)
}
result <- nil
}

ctx, cancel := context.WithTimeout(ctx, time.Duration(d.Get("wait_timeout").(int))*time.Second)
defer cancel()

result := make(chan error, 1)
go waitForHealthyState(result)

select {
case <-ctx.Done():
log.Printf("[ERROR] Container %s failed to be in healthy state in time", retContainer.ID)
Expand Down

0 comments on commit b0a85d7

Please sign in to comment.