Files
moby/client/container_list.go
Cory Snider 7ea066c8d1 client: add Filters type
Add a new type to use for building filter predicates for API requests,
replacing "./api/types/filters".Args in the client. Remove the now
unused api/types/filters package.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2025-10-08 12:06:31 -04:00

59 lines
1.1 KiB
Go

package client
import (
"context"
"encoding/json"
"net/url"
"strconv"
"github.com/moby/moby/api/types/container"
)
// ContainerListOptions holds parameters to list containers with.
type ContainerListOptions struct {
Size bool
All bool
Latest bool
Since string
Before string
Limit int
Filters Filters
}
// ContainerList returns the list of containers in the docker host.
func (cli *Client) ContainerList(ctx context.Context, options ContainerListOptions) ([]container.Summary, error) {
query := url.Values{}
if options.All {
query.Set("all", "1")
}
if options.Limit > 0 {
query.Set("limit", strconv.Itoa(options.Limit))
}
if options.Since != "" {
query.Set("since", options.Since)
}
if options.Before != "" {
query.Set("before", options.Before)
}
if options.Size {
query.Set("size", "1")
}
options.Filters.updateURLValues(query)
resp, err := cli.get(ctx, "/containers/json", query, nil)
defer ensureReaderClosed(resp)
if err != nil {
return nil, err
}
var containers []container.Summary
err = json.NewDecoder(resp.Body).Decode(&containers)
return containers, err
}