github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/container_list.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"strconv"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/filters"
    10  	"golang.org/x/net/context"
    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  		filterJSON, err := filters.ToParamWithVersion(cli.version, options.Filters)
    39  
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  
    44  		query.Set("filters", filterJSON)
    45  	}
    46  
    47  	resp, err := cli.get(ctx, "/containers/json", query, nil)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	var containers []types.Container
    53  	err = json.NewDecoder(resp.body).Decode(&containers)
    54  	ensureReaderClosed(resp)
    55  	return containers, err
    56  }