container_list.go 1.0 KB

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