FindContainerByName takes a container name and returns the container If container is not found, returns nil with no error
(name string)
| 150 | // FindContainerByName takes a container name and returns the container |
| 151 | // If container is not found, returns nil with no error |
| 152 | func FindContainerByName(name string) (*container.Summary, error) { |
| 153 | ctx, apiClient, err := GetDockerClient() |
| 154 | if err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | |
| 158 | containers, err := apiClient.ContainerList(ctx, client.ContainerListOptions{ |
| 159 | All: true, |
| 160 | Filters: client.Filters{}.Add("name", name), |
| 161 | }) |
| 162 | if err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | if len(containers.Items) == 0 { |
| 166 | return nil, nil |
| 167 | } |
| 168 | |
| 169 | // ListContainers can return partial matches. Make sure we only match the exact one |
| 170 | // we're after. |
| 171 | for _, c := range containers.Items { |
| 172 | if len(c.Names) > 0 && c.Names[0] == "/"+name { |
| 173 | return &c, nil |
| 174 | } |
| 175 | } |
| 176 | return nil, nil |
| 177 | } |
| 178 | |
| 179 | // GetContainerStateByName returns container state for the named container |
| 180 | func GetContainerStateByName(name string) (container.ContainerState, error) { |