2015-03-25 06:57:23 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-04 03:39:06 +03:00
|
|
|
"encoding/json"
|
2015-03-25 06:57:23 +03:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2015-11-19 01:20:54 +03:00
|
|
|
"strings"
|
2015-03-25 06:57:23 +03:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2015-11-19 01:20:54 +03:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-04-04 03:39:06 +03:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 07:18:28 +03:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 06:57:23 +03: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 20:34:41 +03: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-25 06:57:23 +03:00
|
|
|
func (cli *DockerCli) CmdImages(args ...string) error {
|
2015-10-08 15:46:21 +03:00
|
|
|
cmd := Cli.Subcmd("images", []string{"[REPOSITORY[:TAG]]"}, Cli.DockerCommands["images"].Description, true)
|
2015-03-25 06:57:23 +03: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 17:37:24 +03:00
|
|
|
noTrunc := cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
|
2015-03-25 06:57:23 +03: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 12:26:09 +03:00
|
|
|
|
2015-03-29 04:22:46 +03:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 06:57:23 +03:00
|
|
|
|
|
|
|
// Consolidate all filter flags, and sanity check them early.
|
|
|
|
// They'll get process in the daemon/server.
|
2015-11-26 04:27:11 +03:00
|
|
|
imageFilterArgs := filters.NewArgs()
|
2015-03-25 06:57:23 +03:00
|
|
|
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 17:33:52 +03:00
|
|
|
v := url.Values{}
|
2015-11-26 04:27:11 +03:00
|
|
|
if imageFilterArgs.Len() > 0 {
|
2015-04-13 17:33:52 +03:00
|
|
|
filterJSON, err := filters.ToParam(imageFilterArgs)
|
2015-03-25 06:57:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-13 17:33:52 +03:00
|
|
|
v.Set("filters", filterJSON)
|
|
|
|
}
|
2015-03-25 06:57:23 +03:00
|
|
|
|
2015-04-13 17:33:52 +03: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-25 06:57:23 +03:00
|
|
|
|
2015-07-10 05:05:50 +03:00
|
|
|
serverResp, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
|
2015-04-13 17:33:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-25 06:57:23 +03:00
|
|
|
|
2015-07-10 05:05:50 +03:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 09:19:23 +03:00
|
|
|
|
2015-04-13 17:33:52 +03:00
|
|
|
images := []types.Image{}
|
2015-07-10 05:05:50 +03:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&images); err != nil {
|
2015-04-13 17:33:52 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-03-25 06:57:23 +03:00
|
|
|
|
2015-04-13 17:33:52 +03:00
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
if !*quiet {
|
|
|
|
if *showDigests {
|
2015-11-19 01:20:54 +03:00
|
|
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tDIGEST\tIMAGE ID\tCREATED\tSIZE")
|
2015-03-25 06:57:23 +03:00
|
|
|
} else {
|
2015-11-19 01:20:54 +03:00
|
|
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tSIZE")
|
2015-03-25 06:57:23 +03:00
|
|
|
}
|
2015-04-13 17:33:52 +03:00
|
|
|
}
|
2015-03-25 06:57:23 +03:00
|
|
|
|
2015-04-13 17:33:52 +03:00
|
|
|
for _, image := range images {
|
|
|
|
ID := image.ID
|
|
|
|
if !*noTrunc {
|
|
|
|
ID = stringid.TruncateID(ID)
|
2015-03-25 06:57:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-13 17:33:52 +03:00
|
|
|
repoTags := image.RepoTags
|
|
|
|
repoDigests := image.RepoDigests
|
2015-03-25 06:57:23 +03:00
|
|
|
|
2015-04-13 17:33:52 +03: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-25 06:57:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-13 17:33:52 +03:00
|
|
|
// combine the tags and digests lists
|
|
|
|
tagsAndDigests := append(repoTags, repoDigests...)
|
|
|
|
for _, repoAndRef := range tagsAndDigests {
|
2015-11-19 01:20:54 +03:00
|
|
|
// default repo, tag, and digest to none - if there's a value, it'll be set below
|
|
|
|
repo := "<none>"
|
2015-04-13 17:33:52 +03:00
|
|
|
tag := "<none>"
|
|
|
|
digest := "<none>"
|
2015-11-19 01:20:54 +03: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-25 06:57:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-13 17:33:52 +03:00
|
|
|
if !*quiet {
|
|
|
|
if *showDigests {
|
2015-11-19 01:20:54 +03: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-25 06:57:23 +03:00
|
|
|
} else {
|
2015-11-19 01:20:54 +03: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-25 06:57:23 +03:00
|
|
|
}
|
2015-04-13 17:33:52 +03:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, ID)
|
2015-03-25 06:57:23 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 17:33:52 +03:00
|
|
|
}
|
2015-03-25 06:57:23 +03:00
|
|
|
|
2015-04-13 17:33:52 +03:00
|
|
|
if !*quiet {
|
|
|
|
w.Flush()
|
2015-03-25 06:57:23 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|