github.com/moby/docker@v26.1.3+incompatible/client/container_list.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/url"
     7  	"strconv"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/container"
    11  	"github.com/docker/docker/api/types/filters"
    12  )
    13  
    14  // ContainerList returns the list of containers in the docker host.
    15  func (cli *Client) ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error) {
    16  	query := url.Values{}
    17  
    18  	if options.All {
    19  		query.Set("all", "1")
    20  	}
    21  
    22  	if options.Limit > 0 {
    23  		query.Set("limit", strconv.Itoa(options.Limit))
    24  	}
    25  
    26  	if options.Since != "" {
    27  		query.Set("since", options.Since)
    28  	}
    29  
    30  	if options.Before != "" {
    31  		query.Set("before", options.Before)
    32  	}
    33  
    34  	if options.Size {
    35  		query.Set("size", "1")
    36  	}
    37  
    38  	if options.Filters.Len() > 0 {
    39  		//nolint:staticcheck // ignore SA1019 for old code
    40  		filterJSON, err := filters.ToParamWithVersion(cli.version, options.Filters)
    41  		if err != nil {
    42  			return nil, err
    43  		}
    44  
    45  		query.Set("filters", filterJSON)
    46  	}
    47  
    48  	resp, err := cli.get(ctx, "/containers/json", query, nil)
    49  	defer ensureReaderClosed(resp)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	var containers []types.Container
    55  	err = json.NewDecoder(resp.body).Decode(&containers)
    56  	return containers, err
    57  }