gopkg.in/docker/docker.v20@v20.10.27/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/filters"
    11  )
    12  
    13  // ContainerList returns the list of containers in the docker host.
    14  func (cli *Client) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
    15  	query := url.Values{}
    16  
    17  	if options.All {
    18  		query.Set("all", "1")
    19  	}
    20  
    21  	if options.Limit != -1 {
    22  		query.Set("limit", strconv.Itoa(options.Limit))
    23  	}
    24  
    25  	if options.Since != "" {
    26  		query.Set("since", options.Since)
    27  	}
    28  
    29  	if options.Before != "" {
    30  		query.Set("before", options.Before)
    31  	}
    32  
    33  	if options.Size {
    34  		query.Set("size", "1")
    35  	}
    36  
    37  	if options.Filters.Len() > 0 {
    38  		//nolint:staticcheck // ignore SA1019 for old code
    39  		filterJSON, err := filters.ToParamWithVersion(cli.version, options.Filters)
    40  
    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  }