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.
This commit is contained in:
Jeff Mitchell 2015-06-12 21:01:36 +00:00
parent 2e01e0635b
commit 56cfba2509
1 changed files with 11 additions and 9 deletions

View File

@ -211,16 +211,18 @@ 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 {
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
}
}
}
}
return nil, nil