Skip to content

Commit 597fafb

Browse files
committed
provider/docker: locate container via ID not name
This reapplies the patch mentioned in hashicorp#3364 - for an unknown reason the diff there was incorrect.
1 parent d6ae527 commit 597fafb

1 file changed

Lines changed: 4 additions & 17 deletions

File tree

builtin/providers/docker/resource_docker_container_funcs.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"strconv"
7-
"strings"
87
"time"
98

109
dc "github.com/fsouza/go-dockerclient"
@@ -160,7 +159,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
160159
func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error {
161160
client := meta.(*dc.Client)
162161

163-
apiContainer, err := fetchDockerContainer(d.Get("name").(string), client)
162+
apiContainer, err := fetchDockerContainer(d.Id(), client)
164163
if err != nil {
165164
return err
166165
}
@@ -268,28 +267,16 @@ func mapTypeMapValsToString(typeMap map[string]interface{}) map[string]string {
268267
return mapped
269268
}
270269

271-
func fetchDockerContainer(name string, client *dc.Client) (*dc.APIContainers, error) {
270+
func fetchDockerContainer(ID string, client *dc.Client) (*dc.APIContainers, error) {
272271
apiContainers, err := client.ListContainers(dc.ListContainersOptions{All: true})
273272

274273
if err != nil {
275274
return nil, fmt.Errorf("Error fetching container information from Docker: %s\n", err)
276275
}
277276

278277
for _, apiContainer := range apiContainers {
279-
// Sometimes the Docker API prefixes container names with /
280-
// like it does in these commands. But if there's no
281-
// set name, it just uses the ID without a /...ugh.
282-
switch len(apiContainer.Names) {
283-
case 0:
284-
if apiContainer.ID == name {
285-
return &apiContainer, nil
286-
}
287-
default:
288-
for _, containerName := range apiContainer.Names {
289-
if strings.TrimLeft(containerName, "/") == name {
290-
return &apiContainer, nil
291-
}
292-
}
278+
if apiContainer.ID == ID {
279+
return &apiContainer, nil
293280
}
294281
}
295282

0 commit comments

Comments
 (0)