From 56cfba2509bbfe574d4c569e96e4201673ac2253 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Fri, 12 Jun 2015 21:01:36 +0000 Subject: [PATCH] Fix a serious problem when using links. Links cause there to be more than one name for a container to be returned. As a result, only looking at the first element of the container names could cause a container to not be found, leading Terraform to remove it from state and attempt to recreate it. --- .../docker/resource_docker_container_funcs.go | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index d355b898c..779ce1d17 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -211,15 +211,17 @@ func fetchDockerContainer(name string, client *dc.Client) (*dc.APIContainers, er // Sometimes the Docker API prefixes container names with / // like it does in these commands. But if there's no // set name, it just uses the ID without a /...ugh. - var dockerContainerName string - if len(apiContainer.Names) > 0 { - dockerContainerName = strings.TrimLeft(apiContainer.Names[0], "/") - } else { - dockerContainerName = apiContainer.ID - } - - if dockerContainerName == name { - return &apiContainer, nil + switch len(apiContainer.Names) { + case 0: + if apiContainer.ID == name { + return &apiContainer, nil + } + default: + for _, containerName := range apiContainer.Names { + if strings.TrimLeft(containerName, "/") == name { + return &apiContainer, nil + } + } } }