Skip to content

Commit e932173

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 1791d68 commit e932173

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
@@ -5,6 +5,7 @@ import (
55
"math/big"
66
"strings"
77

8+
"github.com/docker/docker/client"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -136,6 +137,14 @@ func containsIgnorableErrorMessage(errorMsg string, ignorableErrorMessages ...st
136137
return false
137138
}
138139

140+
func alreadyDeleted(err error) bool {
141+
if strings.Contains(err.Error(), "is already in progress") {
142+
return true
143+
}
144+
145+
return client.IsErrNotFound(err)
146+
}
147+
139148
func interfaceArrayToStringArray(interfaceArray []interface{}) []string {
140149
stringArray := make([]string, len(interfaceArray))
141150
for i, v := range interfaceArray {

internal/provider/resource_docker_container_funcs.go

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

972972
log.Printf("[INFO] Removing Container '%s'", d.Id())
973973
if err := client.ContainerRemove(ctx, d.Id(), removeOpts); err != nil {
974-
if !containsIgnorableErrorMessage(err.Error(), "No such container", "is already in progress") {
974+
if !alreadyDeleted(err) {
975975
return diag.Errorf("Error deleting container %s: %s", d.Id(), err)
976976
}
977977
}
@@ -987,7 +987,7 @@ func resourceDockerContainerDelete(ctx context.Context, d *schema.ResourceData,
987987
case waitOk := <-waitOkC:
988988
log.Printf("[INFO] Container exited with code [%v]: '%s'", waitOk.StatusCode, d.Id())
989989
case err := <-errorC:
990-
if !containsIgnorableErrorMessage(err.Error(), "No such container", "is already in progress") {
990+
if !alreadyDeleted(err) {
991991
return diag.Errorf("Error waiting for container removal '%s': %s", d.Id(), err)
992992
}
993993
log.Printf("[INFO] Waiting for Container '%s' errord: '%s'", d.Id(), err.Error())

0 commit comments

Comments
 (0)