From 0099b5e68cc76080f01e9150d3a3b86e0cb2b741 Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Fri, 10 Oct 2014 08:12:12 +0200 Subject: [PATCH 1/3] reflect changes introduced in d2ffcd9 in docs Docker-DCO-1.1-Signed-off-by: Brice Jaglin (github: bjaglin) --- docs/sources/userguide/dockerlinks.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/sources/userguide/dockerlinks.md b/docs/sources/userguide/dockerlinks.md index 847475cebba0d..717ab43409ee3 100644 --- a/docs/sources/userguide/dockerlinks.md +++ b/docs/sources/userguide/dockerlinks.md @@ -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 From 4a5cefa1731ae99ee592e1898c6007904fb5943a Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Wed, 8 Oct 2014 22:33:01 +0200 Subject: [PATCH 2/3] on truncated output, show canonical name rather than first name Docker-DCO-1.1-Signed-off-by: Brice Jaglin (github: bjaglin) --- api/client/commands.go | 23 ++++++++----- integration-cli/docker_cli_ps_test.go | 47 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/api/client/commands.go b/api/client/commands.go index 03e4293d791ab..b106dc4ba16d4 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -1558,22 +1558,27 @@ func (cli *DockerCli) CmdPs(args ...string) error { outID = utils.TruncateID(outID) } - // Remove the leading / from the names - for i := 0; i < len(outNames); i++ { - outNames[i] = outNames[i][1:] - } - if !*quiet { var ( - outCommand = out.Get("Command") - ports = engine.NewTable("", 0) - outNamesList = strings.Join(outNames, ",") + outCommand = out.Get("Command") + ports = engine.NewTable("", 0) ) outCommand = strconv.Quote(outCommand) if !*noTrunc { outCommand = utils.Trunc(outCommand, 20) - outNamesList = outNames[0] + // Keep only the canonical name + for i := 0; i < len(outNames); i++ { + if !strings.Contains(outNames[i][1:], "/") { + outNames = outNames[i : i+1] + break + } + } + } + // Remove the leading / from the names + for i := 0; i < len(outNames); i++ { + outNames[i] = outNames[i][1:] } + outNamesList := strings.Join(outNames, ",") 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 { diff --git a/integration-cli/docker_cli_ps_test.go b/integration-cli/docker_cli_ps_test.go index 4bcf95bf7901b..39dd4d680a064 100644 --- a/integration-cli/docker_cli_ps_test.go +++ b/integration-cli/docker_cli_ps_test.go @@ -282,3 +282,50 @@ func TestPsListContainersFilterStatus(t *testing.T) { logDone("ps - test ps filter status") } + +func TestPsListContainersNames(t *testing.T) { + runCmd := exec.Command(dockerBinary, "run", "--name", "link_target", "-d", "busybox", "top") + out, _, err := runCommandWithOutput(runCmd) + errorOut(err, t, out) + + runCmd = exec.Command(dockerBinary, "run", "--name", "link_source", "--link", "link_target:link_alias", "-d", "busybox", "top") + out, _, err = runCommandWithOutput(runCmd) + errorOut(err, t, out) + + // non-truncated ps should return all names + runCmd = exec.Command(dockerBinary, "ps", "--no-trunc") + out, _, err = runCommandWithOutput(runCmd) + errorOut(err, t, out) + lines := strings.Split(strings.Trim(out, "\n "), "\n") + namesIndex := strings.Index(lines[0], "NAMES") + expectedNames := "link_source" + foundNames := strings.TrimSpace(lines[1][namesIndex:]) + if foundNames != expectedNames { + t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) + } + expectedNames = "link_source/link_alias,link_target" + foundNames = strings.TrimSpace(lines[2][namesIndex:]) + if foundNames != expectedNames { + t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) + } + + // truncated ps should return canonical names only + runCmd = exec.Command(dockerBinary, "ps") + out, _, err = runCommandWithOutput(runCmd) + errorOut(err, t, out) + lines = strings.Split(strings.Trim(out, "\n "), "\n") + namesIndex = strings.Index(lines[0], "NAMES") + expectedNames = "link_source" + foundNames = strings.TrimSpace(lines[1][namesIndex:]) + if foundNames != expectedNames { + t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) + } + expectedNames = "link_target" + foundNames = strings.TrimSpace(lines[2][namesIndex:]) + if foundNames != expectedNames { + t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) + } + + deleteAllContainers() + logDone("ps - test ps names") +} From b10dfb7de3c24cd6b4184d4228925517803f7ec7 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 10 Oct 2014 19:02:22 +0000 Subject: [PATCH 3/3] Import ps name parsing for default name Signed-off-by: Michael Crosby --- api/client/commands.go | 124 ++++++++++++++++---------- integration-cli/docker_cli_ps_test.go | 47 ---------- 2 files changed, 75 insertions(+), 96 deletions(-) diff --git a/api/client/commands.go b/api/client/commands.go index b106dc4ba16d4..6c4e5c55fe42f 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -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= - containers with exit code of \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,56 +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) } - if !*quiet { - var ( - outCommand = out.Get("Command") - ports = engine.NewTable("", 0) - ) - outCommand = strconv.Quote(outCommand) - if !*noTrunc { - outCommand = utils.Trunc(outCommand, 20) - // Keep only the canonical name - for i := 0; i < len(outNames); i++ { - if !strings.Contains(outNames[i][1:], "/") { - outNames = outNames[i : i+1] - break - } + if *quiet { + fmt.Fprintln(w, outID) + + continue + } + + 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 } } - // Remove the leading / from the names - for i := 0; i < len(outNames); i++ { - outNames[i] = outNames[i][1:] - } - outNamesList := strings.Join(outNames, ",") - 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"))) - } + } + + 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.Fprint(w, "\n") + fmt.Fprintf(w, "%s\n", units.HumanSize(out.GetInt64("SizeRw"))) } - } else { - fmt.Fprintln(w, outID) + + continue } + + fmt.Fprint(w, "\n") } if !*quiet { w.Flush() } + return nil } diff --git a/integration-cli/docker_cli_ps_test.go b/integration-cli/docker_cli_ps_test.go index 39dd4d680a064..4bcf95bf7901b 100644 --- a/integration-cli/docker_cli_ps_test.go +++ b/integration-cli/docker_cli_ps_test.go @@ -282,50 +282,3 @@ func TestPsListContainersFilterStatus(t *testing.T) { logDone("ps - test ps filter status") } - -func TestPsListContainersNames(t *testing.T) { - runCmd := exec.Command(dockerBinary, "run", "--name", "link_target", "-d", "busybox", "top") - out, _, err := runCommandWithOutput(runCmd) - errorOut(err, t, out) - - runCmd = exec.Command(dockerBinary, "run", "--name", "link_source", "--link", "link_target:link_alias", "-d", "busybox", "top") - out, _, err = runCommandWithOutput(runCmd) - errorOut(err, t, out) - - // non-truncated ps should return all names - runCmd = exec.Command(dockerBinary, "ps", "--no-trunc") - out, _, err = runCommandWithOutput(runCmd) - errorOut(err, t, out) - lines := strings.Split(strings.Trim(out, "\n "), "\n") - namesIndex := strings.Index(lines[0], "NAMES") - expectedNames := "link_source" - foundNames := strings.TrimSpace(lines[1][namesIndex:]) - if foundNames != expectedNames { - t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) - } - expectedNames = "link_source/link_alias,link_target" - foundNames = strings.TrimSpace(lines[2][namesIndex:]) - if foundNames != expectedNames { - t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) - } - - // truncated ps should return canonical names only - runCmd = exec.Command(dockerBinary, "ps") - out, _, err = runCommandWithOutput(runCmd) - errorOut(err, t, out) - lines = strings.Split(strings.Trim(out, "\n "), "\n") - namesIndex = strings.Index(lines[0], "NAMES") - expectedNames = "link_source" - foundNames = strings.TrimSpace(lines[1][namesIndex:]) - if foundNames != expectedNames { - t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) - } - expectedNames = "link_target" - foundNames = strings.TrimSpace(lines[2][namesIndex:]) - if foundNames != expectedNames { - t.Fatalf("Expected names %q, got %q", expectedNames, foundNames) - } - - deleteAllContainers() - logDone("ps - test ps names") -}