Skip to content

Commit fb2f75c

Browse files
author
Decaf Code
committed
fix: make container deletion more robust
Use the `IsErrNotFound` function exported from the Docker client library to detect HTTP 404 errors returned by the Docker API more reliably. This improves compatibility with Podman's implementation of the Docker API, because Podman uses a different capitalization for the error message that is returned when a container has already been deleted, causing the existing code to fail to recognize this condition.
1 parent 7155ab0 commit fb2f75c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

internal/provider/helpers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/docker/docker/client"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -134,3 +135,11 @@ func containsIgnorableErrorMessage(errorMsg string, ignorableErrorMessages ...st
134135

135136
return false
136137
}
138+
139+
func alreadyDeleted(err error) bool {
140+
if strings.Contains(err.Error(), "is already in progress") {
141+
return true
142+
}
143+
144+
return client.IsErrNotFound(err)
145+
}

internal/provider/resource_docker_container_funcs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ func resourceDockerContainerDelete(ctx context.Context, d *schema.ResourceData,
886886

887887
log.Printf("[INFO] Removing Container '%s'", d.Id())
888888
if err := client.ContainerRemove(ctx, d.Id(), removeOpts); err != nil {
889-
if !containsIgnorableErrorMessage(err.Error(), "No such container", "is already in progress") {
889+
if !alreadyDeleted(err) {
890890
return diag.Errorf("Error deleting container %s: %s", d.Id(), err)
891891
}
892892
}
@@ -902,7 +902,7 @@ func resourceDockerContainerDelete(ctx context.Context, d *schema.ResourceData,
902902
case waitOk := <-waitOkC:
903903
log.Printf("[INFO] Container exited with code [%v]: '%s'", waitOk.StatusCode, d.Id())
904904
case err := <-errorC:
905-
if !containsIgnorableErrorMessage(err.Error(), "No such container", "is already in progress") {
905+
if !alreadyDeleted(err) {
906906
return diag.Errorf("Error waiting for container removal '%s': %s", d.Id(), err)
907907
}
908908
log.Printf("[INFO] Waiting for Container '%s' errord: '%s'", d.Id(), err.Error())

0 commit comments

Comments
 (0)