container_list.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package client
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "strconv"
  6. "github.com/docker/engine-api/types"
  7. "github.com/docker/engine-api/types/filters"
  8. "golang.org/x/net/context"
  9. )
  10. // ContainerList returns the list of containers in the docker host.
  11. func (cli *Client) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
  12. query := url.Values{}
  13. if options.All {
  14. query.Set("all", "1")
  15. }
  16. if options.Limit != -1 {
  17. query.Set("limit", strconv.Itoa(options.Limit))
  18. }
  19. if options.Since != "" {
  20. query.Set("since", options.Since)
  21. }
  22. if options.Before != "" {
  23. query.Set("before", options.Before)
  24. }
  25. if options.Size {
  26. query.Set("size", "1")
  27. }
  28. if options.Filter.Len() > 0 {
  29. filterJSON, err := filters.ToParam(options.Filter)
  30. if err != nil {
  31. return nil, err
  32. }
  33. query.Set("filters", filterJSON)
  34. }
  35. resp, err := cli.get(ctx, "/containers/json", query, nil)
  36. if err != nil {
  37. return nil, err
  38. }
  39. var containers []types.Container
  40. err = json.NewDecoder(resp.body).Decode(&containers)
  41. ensureReaderClosed(resp)
  42. return containers, err
  43. }