2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-03 20:39:06 -04:00
|
|
|
"encoding/json"
|
2015-03-24 23:57:23 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/parsers"
|
|
|
|
"github.com/docker/docker/pkg/parsers/filters"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/pkg/units"
|
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdImages lists the images in a specified repository, or all top-level images if no repository is specified.
|
|
|
|
//
|
|
|
|
// Usage: docker images [OPTIONS] [REPOSITORY]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdImages(args ...string) error {
|
|
|
|
cmd := cli.Subcmd("images", "[REPOSITORY]", "List images", true)
|
|
|
|
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
|
|
|
|
all := cmd.Bool([]string{"a", "-all"}, false, "Show all images (default hides intermediate images)")
|
|
|
|
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
|
|
|
showDigests := cmd.Bool([]string{"-digests"}, false, "Show digests")
|
|
|
|
|
|
|
|
flFilter := opts.NewListOpts(nil)
|
|
|
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
|
|
|
cmd.Require(flag.Max, 1)
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
// Consolidate all filter flags, and sanity check them early.
|
|
|
|
// They'll get process in the daemon/server.
|
|
|
|
imageFilterArgs := filters.Args{}
|
|
|
|
for _, f := range flFilter.GetAll() {
|
|
|
|
var err error
|
|
|
|
imageFilterArgs, err = filters.ParseFlag(f, imageFilterArgs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matchName := cmd.Arg(0)
|
2015-04-13 10:33:52 -04:00
|
|
|
v := url.Values{}
|
|
|
|
if len(imageFilterArgs) > 0 {
|
|
|
|
filterJSON, err := filters.ToParam(imageFilterArgs)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-13 10:33:52 -04:00
|
|
|
v.Set("filters", filterJSON)
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
if cmd.NArg() == 1 {
|
|
|
|
// FIXME rename this parameter, to not be confused with the filters flag
|
|
|
|
v.Set("filter", matchName)
|
|
|
|
}
|
|
|
|
if *all {
|
|
|
|
v.Set("all", "1")
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
rdr, _, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
images := []types.Image{}
|
|
|
|
if err := json.NewDecoder(rdr).Decode(&images); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
if !*quiet {
|
|
|
|
if *showDigests {
|
|
|
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tDIGEST\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
|
2015-03-24 23:57:23 -04:00
|
|
|
} else {
|
2015-04-13 10:33:52 -04:00
|
|
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-04-13 10:33:52 -04:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
for _, image := range images {
|
|
|
|
ID := image.ID
|
|
|
|
if !*noTrunc {
|
|
|
|
ID = stringid.TruncateID(ID)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
repoTags := image.RepoTags
|
|
|
|
repoDigests := image.RepoDigests
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
if len(repoTags) == 1 && repoTags[0] == "<none>:<none>" && len(repoDigests) == 1 && repoDigests[0] == "<none>@<none>" {
|
|
|
|
// dangling image - clear out either repoTags or repoDigsts so we only show it once below
|
|
|
|
repoDigests = []string{}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
// combine the tags and digests lists
|
|
|
|
tagsAndDigests := append(repoTags, repoDigests...)
|
|
|
|
for _, repoAndRef := range tagsAndDigests {
|
|
|
|
repo, ref := parsers.ParseRepositoryTag(repoAndRef)
|
|
|
|
// default tag and digest to none - if there's a value, it'll be set below
|
|
|
|
tag := "<none>"
|
|
|
|
digest := "<none>"
|
|
|
|
if utils.DigestReference(ref) {
|
|
|
|
digest = ref
|
2015-03-24 23:57:23 -04:00
|
|
|
} else {
|
2015-04-13 10:33:52 -04:00
|
|
|
tag = ref
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
if !*quiet {
|
|
|
|
if *showDigests {
|
2015-05-14 20:31:34 -04:00
|
|
|
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)))
|
2015-03-24 23:57:23 -04:00
|
|
|
} else {
|
2015-05-14 20:31:34 -04:00
|
|
|
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)))
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-04-13 10:33:52 -04:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, ID)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 10:33:52 -04:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
if !*quiet {
|
|
|
|
w.Flush()
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|