2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-04 00:39:06 +00:00
|
|
|
"encoding/json"
|
2015-03-25 03:57:23 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2015-04-04 00:39:06 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 03:57:23 +00: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 17:34:41 +00: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 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdImages(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("images", []string{"[REPOSITORY[:TAG]]"}, Cli.DockerCommands["images"].Description, true)
|
2015-03-25 03:57:23 +00: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 14:37:24 +00:00
|
|
|
noTrunc := cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
|
2015-03-25 03:57:23 +00: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 09:26:09 +00:00
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00: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 14:33:52 +00:00
|
|
|
v := url.Values{}
|
|
|
|
if len(imageFilterArgs) > 0 {
|
|
|
|
filterJSON, err := filters.ToParam(imageFilterArgs)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-13 14:33:52 +00:00
|
|
|
v.Set("filters", filterJSON)
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-04-13 14:33:52 +00: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 03:57:23 +00:00
|
|
|
|
2015-07-10 02:05:50 +00:00
|
|
|
serverResp, err := cli.call("GET", "/images/json?"+v.Encode(), nil, nil)
|
2015-04-13 14:33:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-07-10 02:05:50 +00:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 06:19:23 +00:00
|
|
|
|
2015-04-13 14:33:52 +00:00
|
|
|
images := []types.Image{}
|
2015-07-10 02:05:50 +00:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&images); err != nil {
|
2015-04-13 14:33:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-04-13 14:33:52 +00: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-25 03:57:23 +00:00
|
|
|
} else {
|
2015-04-13 14:33:52 +00:00
|
|
|
fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-04-13 14:33:52 +00:00
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-04-13 14:33:52 +00:00
|
|
|
for _, image := range images {
|
|
|
|
ID := image.ID
|
|
|
|
if !*noTrunc {
|
|
|
|
ID = stringid.TruncateID(ID)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:33:52 +00:00
|
|
|
repoTags := image.RepoTags
|
|
|
|
repoDigests := image.RepoDigests
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-04-13 14:33:52 +00: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 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:33:52 +00: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-25 03:57:23 +00:00
|
|
|
} else {
|
2015-04-13 14:33:52 +00:00
|
|
|
tag = ref
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:33:52 +00:00
|
|
|
if !*quiet {
|
|
|
|
if *showDigests {
|
2015-05-15 00:31:34 +00: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-25 03:57:23 +00:00
|
|
|
} else {
|
2015-05-15 00:31:34 +00: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-25 03:57:23 +00:00
|
|
|
}
|
2015-04-13 14:33:52 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, ID)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 14:33:52 +00:00
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-04-13 14:33:52 +00:00
|
|
|
if !*quiet {
|
|
|
|
w.Flush()
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|