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"
|
2015-11-18 17:20:54 -05:00
|
|
|
"strings"
|
2015-03-24 23:57:23 -04:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-04-03 20:39:06 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
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/filters"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/pkg/units"
|
|
|
|
)
|
|
|
|
|
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 {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("images", []string{"[REPOSITORY[:TAG]]"}, Cli.DockerCommands["images"].Description, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
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)")
|
2015-11-09 09:37:24 -05:00
|
|
|
noTrunc := cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
|
2015-03-24 23:57:23 -04:00
|
|
|
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-07-03 05:26:09 -04:00
|
|
|
|
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-07-09 22:05:50 -04:00
|
|
|
serverResp, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
|
2015-04-13 10:33:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
images := []types.Image{}
|
2015-07-09 22:05:50 -04:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&images); err != nil {
|
2015-04-13 10:33:52 -04:00
|
|
|
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 {
|
2015-11-18 17:20:54 -05:00
|
|
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tDIGEST\tIMAGE ID\tCREATED\tSIZE")
|
2015-03-24 23:57:23 -04:00
|
|
|
} else {
|
2015-11-18 17:20:54 -05:00
|
|
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tSIZE")
|
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 {
|
2015-11-18 17:20:54 -05:00
|
|
|
// default repo, tag, and digest to none - if there's a value, it'll be set below
|
|
|
|
repo := "<none>"
|
2015-04-13 10:33:52 -04:00
|
|
|
tag := "<none>"
|
|
|
|
digest := "<none>"
|
2015-11-18 17:20:54 -05:00
|
|
|
|
|
|
|
if !strings.HasPrefix(repoAndRef, "<none>") {
|
|
|
|
ref, err := reference.ParseNamed(repoAndRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
repo = ref.Name()
|
|
|
|
|
|
|
|
switch x := ref.(type) {
|
|
|
|
case reference.Digested:
|
|
|
|
digest = x.Digest().String()
|
|
|
|
case reference.Tagged:
|
|
|
|
tag = x.Tag()
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-04-13 10:33:52 -04:00
|
|
|
if !*quiet {
|
|
|
|
if *showDigests {
|
2015-11-18 17:20:54 -05: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.Size)))
|
2015-03-24 23:57:23 -04:00
|
|
|
} else {
|
2015-11-18 17:20:54 -05: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.Size)))
|
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
|
|
|
|
}
|