2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-12-18 13:03:41 +00:00
|
|
|
"github.com/docker/docker/api/client/formatter"
|
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"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
2015-03-25 03:57:23 +00:00
|
|
|
)
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdPs outputs a list of Docker containers.
|
|
|
|
//
|
|
|
|
// Usage: docker ps [OPTIONS]
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdPs(args ...string) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
|
2015-11-26 01:27:11 +00:00
|
|
|
psFilterArgs = filters.NewArgs()
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
quiet = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
|
|
|
|
size = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
|
|
|
|
all = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
|
2015-11-09 14:37:24 +00:00
|
|
|
noTrunc = cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
|
2015-11-05 07:08:00 +00:00
|
|
|
nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container (includes all states)")
|
|
|
|
since = cmd.String([]string{"#-since"}, "", "Show containers created since Id or Name (includes all states)")
|
|
|
|
before = cmd.String([]string{"#-before"}, "", "Only show containers created before Id or Name")
|
|
|
|
last = cmd.Int([]string{"n"}, -1, "Show n last created containers (includes all states)")
|
2015-07-17 04:03:16 +00:00
|
|
|
format = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template")
|
2015-03-25 03:57:23 +00:00
|
|
|
flFilter = opts.NewListOpts(nil)
|
|
|
|
)
|
|
|
|
cmd.Require(flag.Exact, 0)
|
|
|
|
|
|
|
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
if *last == -1 && *nLatest {
|
|
|
|
*last = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consolidate all filter flags, and sanity check them.
|
|
|
|
// They'll get processed in the daemon/server.
|
|
|
|
for _, f := range flFilter.GetAll() {
|
|
|
|
if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 22:02:06 +00:00
|
|
|
options := types.ContainerListOptions{
|
2015-12-04 16:59:44 +00:00
|
|
|
All: *all,
|
|
|
|
Limit: *last,
|
|
|
|
Since: *since,
|
|
|
|
Before: *before,
|
|
|
|
Size: *size,
|
|
|
|
Filter: psFilterArgs,
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 16:59:44 +00:00
|
|
|
containers, err := cli.client.ContainerList(options)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:23:27 +00:00
|
|
|
f := *format
|
|
|
|
if len(f) == 0 {
|
2015-08-14 22:00:48 +00:00
|
|
|
if len(cli.PsFormat()) > 0 && !*quiet {
|
2015-05-01 21:23:27 +00:00
|
|
|
f = cli.PsFormat()
|
|
|
|
} else {
|
|
|
|
f = "table"
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-01-21 23:52:22 +00:00
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-12-18 13:03:41 +00:00
|
|
|
psCtx := formatter.ContainerContext{
|
|
|
|
Context: formatter.Context{
|
|
|
|
Output: cli.out,
|
|
|
|
Format: f,
|
|
|
|
Quiet: *quiet,
|
|
|
|
Trunc: !*noTrunc,
|
|
|
|
},
|
|
|
|
Size: *size,
|
|
|
|
Containers: containers,
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 13:03:41 +00:00
|
|
|
psCtx.Write()
|
2015-03-25 03:57:23 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|