Merge pull request #8505 from crosbymichael/ps-name
Improve ps name parsing
This commit is contained in:
commit
246ec5dd06
2 changed files with 81 additions and 49 deletions
|
@ -1475,57 +1475,68 @@ func (cli *DockerCli) printTreeNode(noTrunc bool, image *engine.Env, prefix stri
|
|||
}
|
||||
|
||||
func (cli *DockerCli) CmdPs(args ...string) error {
|
||||
cmd := cli.Subcmd("ps", "", "List containers")
|
||||
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
|
||||
size := cmd.Bool([]string{"s", "-size"}, false, "Display sizes")
|
||||
all := cmd.Bool([]string{"a", "-all"}, false, "Show all containers. Only running containers are shown by default.")
|
||||
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
||||
nLatest := cmd.Bool([]string{"l", "-latest"}, false, "Show only the latest created container, include non-running ones.")
|
||||
since := cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show only containers created since Id or Name, include non-running ones.")
|
||||
before := cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
|
||||
last := cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
|
||||
var (
|
||||
err error
|
||||
|
||||
psFilterArgs = filters.Args{}
|
||||
v = url.Values{}
|
||||
|
||||
cmd = cli.Subcmd("ps", "", "List containers")
|
||||
quiet = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
|
||||
size = cmd.Bool([]string{"s", "-size"}, false, "Display sizes")
|
||||
all = cmd.Bool([]string{"a", "-all"}, false, "Show all containers. Only running containers are shown by default.")
|
||||
noTrunc = cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
||||
nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show only the latest created container, include non-running ones.")
|
||||
since = cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show only containers created since Id or Name, include non-running ones.")
|
||||
before = cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
|
||||
last = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
|
||||
flFilter = opts.NewListOpts(nil)
|
||||
)
|
||||
|
||||
flFilter := opts.NewListOpts(nil)
|
||||
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>\nstatus=(restarting|running|paused|exited)")
|
||||
|
||||
if err := cmd.Parse(args); err != nil {
|
||||
return nil
|
||||
}
|
||||
v := url.Values{}
|
||||
|
||||
if *last == -1 && *nLatest {
|
||||
*last = 1
|
||||
}
|
||||
|
||||
if *all {
|
||||
v.Set("all", "1")
|
||||
}
|
||||
|
||||
if *last != -1 {
|
||||
v.Set("limit", strconv.Itoa(*last))
|
||||
}
|
||||
|
||||
if *since != "" {
|
||||
v.Set("since", *since)
|
||||
}
|
||||
|
||||
if *before != "" {
|
||||
v.Set("before", *before)
|
||||
}
|
||||
|
||||
if *size {
|
||||
v.Set("size", "1")
|
||||
}
|
||||
|
||||
// Consolidate all filter flags, and sanity check them.
|
||||
// They'll get processed in the daemon/server.
|
||||
psFilterArgs := filters.Args{}
|
||||
for _, f := range flFilter.GetAll() {
|
||||
var err error
|
||||
psFilterArgs, err = filters.ParseFlag(f, psFilterArgs)
|
||||
if err != nil {
|
||||
if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(psFilterArgs) > 0 {
|
||||
filterJson, err := filters.ToParam(psFilterArgs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v.Set("filters", filterJson)
|
||||
}
|
||||
|
||||
|
@ -1538,9 +1549,11 @@ func (cli *DockerCli) CmdPs(args ...string) error {
|
|||
if _, err := outs.ReadListFrom(body); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
||||
if !*quiet {
|
||||
fmt.Fprint(w, "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES")
|
||||
|
||||
if *size {
|
||||
fmt.Fprintln(w, "\tSIZE")
|
||||
} else {
|
||||
|
@ -1548,51 +1561,69 @@ func (cli *DockerCli) CmdPs(args ...string) error {
|
|||
}
|
||||
}
|
||||
|
||||
stripNamePrefix := func(ss []string) []string {
|
||||
for i, s := range ss {
|
||||
ss[i] = s[1:]
|
||||
}
|
||||
|
||||
return ss
|
||||
}
|
||||
|
||||
for _, out := range outs.Data {
|
||||
var (
|
||||
outID = out.Get("Id")
|
||||
outNames = out.GetList("Names")
|
||||
)
|
||||
outID := out.Get("Id")
|
||||
|
||||
if !*noTrunc {
|
||||
outID = utils.TruncateID(outID)
|
||||
}
|
||||
|
||||
// Remove the leading / from the names
|
||||
for i := 0; i < len(outNames); i++ {
|
||||
outNames[i] = outNames[i][1:]
|
||||
if *quiet {
|
||||
fmt.Fprintln(w, outID)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if !*quiet {
|
||||
var (
|
||||
outCommand = out.Get("Command")
|
||||
ports = engine.NewTable("", 0)
|
||||
outNamesList = strings.Join(outNames, ",")
|
||||
)
|
||||
outCommand = strconv.Quote(outCommand)
|
||||
if !*noTrunc {
|
||||
outCommand = utils.Trunc(outCommand, 20)
|
||||
outNamesList = outNames[0]
|
||||
}
|
||||
ports.ReadListFrom([]byte(out.Get("Ports")))
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", outID, out.Get("Image"), outCommand, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), out.Get("Status"), api.DisplayablePorts(ports), outNamesList)
|
||||
if *size {
|
||||
if out.GetInt("SizeRootFs") > 0 {
|
||||
fmt.Fprintf(w, "%s (virtual %s)\n", units.HumanSize(out.GetInt64("SizeRw")), units.HumanSize(out.GetInt64("SizeRootFs")))
|
||||
} else {
|
||||
fmt.Fprintf(w, "%s\n", units.HumanSize(out.GetInt64("SizeRw")))
|
||||
var (
|
||||
outNames = stripNamePrefix(out.GetList("Names"))
|
||||
outCommand = strconv.Quote(out.Get("Command"))
|
||||
ports = engine.NewTable("", 0)
|
||||
)
|
||||
|
||||
if !*noTrunc {
|
||||
outCommand = utils.Trunc(outCommand, 20)
|
||||
|
||||
// only display the default name for the container with notrunc is passed
|
||||
for _, name := range outNames {
|
||||
if len(strings.Split(name, "/")) == 1 {
|
||||
outNames = []string{name}
|
||||
|
||||
break
|
||||
}
|
||||
} else {
|
||||
fmt.Fprint(w, "\n")
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintln(w, outID)
|
||||
}
|
||||
|
||||
ports.ReadListFrom([]byte(out.Get("Ports")))
|
||||
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", outID, out.Get("Image"), outCommand,
|
||||
units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))),
|
||||
out.Get("Status"), api.DisplayablePorts(ports), strings.Join(outNames, ","))
|
||||
|
||||
if *size {
|
||||
if out.GetInt("SizeRootFs") > 0 {
|
||||
fmt.Fprintf(w, "%s (virtual %s)\n", units.HumanSize(out.GetInt64("SizeRw")), units.HumanSize(out.GetInt64("SizeRootFs")))
|
||||
} else {
|
||||
fmt.Fprintf(w, "%s\n", units.HumanSize(out.GetInt64("SizeRw")))
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "\n")
|
||||
}
|
||||
|
||||
if !*quiet {
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -151,12 +151,13 @@ earlier. The `--link` flag takes the form:
|
|||
Where `name` is the name of the container we're linking to and `alias` is an
|
||||
alias for the link name. You'll see how that alias gets used shortly.
|
||||
|
||||
Next, look at your linked containers using `docker ps`.
|
||||
Next, look at the names of your linked containers by filtering the full output of
|
||||
`docker ps` to the last column (NAMES) using `docker ps --no-trunc | awk '{print $NF}'`.
|
||||
|
||||
$ sudo docker ps
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
349169744e49 training/postgres:latest su postgres -c '/usr About a minute ago Up About a minute 5432/tcp db, web/db
|
||||
aed84ee21bde training/webapp:latest python app.py 16 hours ago Up 2 minutes 0.0.0.0:49154->5000/tcp web
|
||||
$ sudo docker ps --no-trunc | awk '{print $NF}'
|
||||
NAMES
|
||||
db, web/db
|
||||
web
|
||||
|
||||
You can see your named containers, `db` and `web`, and you can see that the `db`
|
||||
container also shows `web/db` in the `NAMES` column. This tells you that the
|
||||
|
|
Loading…
Reference in a new issue