images.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "text/tabwriter"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/opts"
  10. flag "github.com/docker/docker/pkg/mflag"
  11. "github.com/docker/docker/pkg/parsers"
  12. "github.com/docker/docker/pkg/parsers/filters"
  13. "github.com/docker/docker/pkg/stringid"
  14. "github.com/docker/docker/pkg/units"
  15. "github.com/docker/docker/utils"
  16. )
  17. // CmdImages lists the images in a specified repository, or all top-level images if no repository is specified.
  18. //
  19. // Usage: docker images [OPTIONS] [REPOSITORY]
  20. func (cli *DockerCli) CmdImages(args ...string) error {
  21. cmd := cli.Subcmd("images", "[REPOSITORY]", "List images", true)
  22. quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
  23. all := cmd.Bool([]string{"a", "-all"}, false, "Show all images (default hides intermediate images)")
  24. noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  25. showDigests := cmd.Bool([]string{"-digests"}, false, "Show digests")
  26. flFilter := opts.NewListOpts(nil)
  27. cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
  28. cmd.Require(flag.Max, 1)
  29. cmd.ParseFlags(args, true)
  30. // Consolidate all filter flags, and sanity check them early.
  31. // They'll get process in the daemon/server.
  32. imageFilterArgs := filters.Args{}
  33. for _, f := range flFilter.GetAll() {
  34. var err error
  35. imageFilterArgs, err = filters.ParseFlag(f, imageFilterArgs)
  36. if err != nil {
  37. return err
  38. }
  39. }
  40. matchName := cmd.Arg(0)
  41. v := url.Values{}
  42. if len(imageFilterArgs) > 0 {
  43. filterJSON, err := filters.ToParam(imageFilterArgs)
  44. if err != nil {
  45. return err
  46. }
  47. v.Set("filters", filterJSON)
  48. }
  49. if cmd.NArg() == 1 {
  50. // FIXME rename this parameter, to not be confused with the filters flag
  51. v.Set("filter", matchName)
  52. }
  53. if *all {
  54. v.Set("all", "1")
  55. }
  56. rdr, _, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
  57. if err != nil {
  58. return err
  59. }
  60. images := []types.Image{}
  61. if err := json.NewDecoder(rdr).Decode(&images); err != nil {
  62. return err
  63. }
  64. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  65. if !*quiet {
  66. if *showDigests {
  67. fmt.Fprintln(w, "REPOSITORY\tTAG\tDIGEST\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
  68. } else {
  69. fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
  70. }
  71. }
  72. for _, image := range images {
  73. ID := image.ID
  74. if !*noTrunc {
  75. ID = stringid.TruncateID(ID)
  76. }
  77. repoTags := image.RepoTags
  78. repoDigests := image.RepoDigests
  79. if len(repoTags) == 1 && repoTags[0] == "<none>:<none>" && len(repoDigests) == 1 && repoDigests[0] == "<none>@<none>" {
  80. // dangling image - clear out either repoTags or repoDigsts so we only show it once below
  81. repoDigests = []string{}
  82. }
  83. // combine the tags and digests lists
  84. tagsAndDigests := append(repoTags, repoDigests...)
  85. for _, repoAndRef := range tagsAndDigests {
  86. repo, ref := parsers.ParseRepositoryTag(repoAndRef)
  87. // default tag and digest to none - if there's a value, it'll be set below
  88. tag := "<none>"
  89. digest := "<none>"
  90. if utils.DigestReference(ref) {
  91. digest = ref
  92. } else {
  93. tag = ref
  94. }
  95. if !*quiet {
  96. if *showDigests {
  97. fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, tag, digest, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
  98. } else {
  99. fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
  100. }
  101. } else {
  102. fmt.Fprintln(w, ID)
  103. }
  104. }
  105. }
  106. if !*quiet {
  107. w.Flush()
  108. }
  109. return nil
  110. }