本文整理汇总了Golang中github.com/docker/engine-api/client.APIClient.ContainerList方法的典型用法代码示例。如果您正苦于以下问题:Golang APIClient.ContainerList方法的具体用法?Golang APIClient.ContainerList怎么用?Golang APIClient.ContainerList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/docker/engine-api/client.APIClient
的用法示例。
在下文中一共展示了APIClient.ContainerList方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetContainersByFilter
// GetContainersByFilter looks up the hosts containers with the specified filters and
// returns a list of container matching it, or an error.
func GetContainersByFilter(ctx context.Context, clientInstance client.APIClient, containerFilters ...map[string][]string) ([]types.Container, error) {
filterArgs := filters.NewArgs()
// FIXME(vdemeester) I don't like 3 for loops >_<
for _, filter := range containerFilters {
for key, filterValue := range filter {
for _, value := range filterValue {
filterArgs.Add(key, value)
}
}
}
return clientInstance.ContainerList(ctx, types.ContainerListOptions{
All: true,
Filter: filterArgs,
})
}
示例2: listContainers
func listContainers(dockerClient client.APIClient) ([]dockertypes.ContainerJSON, error) {
containerList, err := dockerClient.ContainerList(context.Background(), dockertypes.ContainerListOptions{})
if err != nil {
return []dockertypes.ContainerJSON{}, err
}
containersInspected := []dockertypes.ContainerJSON{}
// get inspect containers
for _, container := range containerList {
containerInspected, err := dockerClient.ContainerInspect(context.Background(), container.ID)
if err != nil {
log.Warnf("Failed to inpsect container %s, error: %s", container.ID, err)
}
containersInspected = append(containersInspected, containerInspected)
}
return containersInspected, nil
}
示例3: GetContainerByID
// GetContainerByID looks up the hosts containers with the specified Id and
// returns it, or an error.
func GetContainerByID(client client.APIClient, id string) (*types.Container, error) {
filterArgs := filters.NewArgs()
filterArgs.Add("id", id)
containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
Filter: filterArgs,
})
if err != nil {
return nil, err
}
if len(containers) == 0 {
return nil, nil
}
return &containers[0], nil
}
示例4: GetContainerByName
// GetContainerByName looks up the hosts containers with the specified name and
// returns it, or an error.
func GetContainerByName(client client.APIClient, name string) (*types.Container, error) {
filterArgs := filters.NewArgs()
filterArgs.Add("label", fmt.Sprintf("%s=%s", NAME, name))
containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
Filter: filterArgs,
})
if err != nil {
return nil, err
}
if len(containers) == 0 {
return nil, nil
}
return &containers[0], nil
}
示例5: NewNamer
// NewNamer returns a namer that returns names based on the specified project and
// service name and an inner counter, e.g. project_service_1, project_service_2…
func NewNamer(ctx context.Context, client client.APIClient, project, service string, oneOff bool) (Namer, error) {
namer := &defaultNamer{
project: project,
service: service,
oneOff: oneOff,
}
filter := filters.NewArgs()
filter.Add("label", fmt.Sprintf("%s=%s", labels.PROJECT.Str(), project))
filter.Add("label", fmt.Sprintf("%s=%s", labels.SERVICE.Str(), service))
if oneOff {
filter.Add("label", fmt.Sprintf("%s=%s", labels.ONEOFF.Str(), "True"))
} else {
filter.Add("label", fmt.Sprintf("%s=%s", labels.ONEOFF.Str(), "False"))
}
containers, err := client.ContainerList(ctx, types.ContainerListOptions{
All: true,
Filter: filter,
})
if err != nil {
return nil, err
}
maxNumber := 0
for _, container := range containers {
number, err := strconv.Atoi(container.Labels[labels.NUMBER.Str()])
if err != nil {
return nil, err
}
if number > maxNumber {
maxNumber = number
}
}
namer.currentNumber = maxNumber + 1
return namer, nil
}