Browse code

Add name and alias to docker ps output

Michael Crosby authored on 2013/09/26 06:01:16
Showing 4 changed files
... ...
@@ -42,6 +42,7 @@ type APIRmi struct {
42 42
 }
43 43
 
44 44
 type APIContainers struct {
45
+	Name       string
45 46
 	ID         string `json:"Id"`
46 47
 	Image      string
47 48
 	Command    string
... ...
@@ -1074,7 +1074,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
1074 1074
 	}
1075 1075
 	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
1076 1076
 	if !*quiet {
1077
-		fmt.Fprint(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS")
1077
+		fmt.Fprint(w, "NAME\tID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS")
1078 1078
 		if *size {
1079 1079
 			fmt.Fprintln(w, "\tSIZE")
1080 1080
 		} else {
... ...
@@ -1085,9 +1085,9 @@ func (cli *DockerCli) CmdPs(args ...string) error {
1085 1085
 	for _, out := range outs {
1086 1086
 		if !*quiet {
1087 1087
 			if *noTrunc {
1088
-				fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t", out.ID, out.Image, out.Command, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, displayablePorts(out.Ports))
1088
+				fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\t%s\t", out.Name, out.ID, out.Image, out.Command, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, displayablePorts(out.Ports))
1089 1089
 			} else {
1090
-				fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t", utils.TruncateID(out.ID), out.Image, utils.Trunc(out.Command, 20), utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, displayablePorts(out.Ports))
1090
+				fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\t%s\t", out.Name, utils.TruncateID(out.ID), out.Image, utils.Trunc(out.Command, 20), utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, displayablePorts(out.Ports))
1091 1091
 			}
1092 1092
 			if *size {
1093 1093
 				if out.SizeRootFs > 0 {
... ...
@@ -42,7 +42,7 @@ func (r *LinkRepository) NewLink(to, from *Container, bridgeInterface string, al
42 42
 		FromID:          utils.TruncateID(from.ID),
43 43
 		ToID:            utils.TruncateID(to.ID),
44 44
 		BridgeInterface: bridgeInterface,
45
-		Alias:           strings.ToUpper(alias),
45
+		Alias:           alias,
46 46
 		FromIP:          from.NetworkSettings.IPAddress,
47 47
 		ToIP:            to.NetworkSettings.IPAddress,
48 48
 		FromEnvironment: from.Config.Env,
... ...
@@ -67,7 +67,7 @@ func (l *Link) ToEnv() []string {
67 67
 
68 68
 	// Load exposed ports into the environment
69 69
 	for _, p := range l.Ports {
70
-		env = append(env, fmt.Sprintf("%s_PORT_%s_%s=%s://%s:%s", l.Alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto(), l.FromIP, p.Port()))
70
+		env = append(env, fmt.Sprintf("%s_PORT_%s_%s=%s://%s:%s", l.Alias, p.Port(), p.Proto(), p.Proto(), l.FromIP, p.Port()))
71 71
 	}
72 72
 
73 73
 	// Load the linked container's ID into the environment
... ...
@@ -100,7 +100,7 @@ func (l *Link) getDefaultPort() *Port {
100 100
 		sortPorts(l.Ports, func(ip, jp Port) bool {
101 101
 			// If the two ports have the same number, tcp takes priority
102 102
 			// Sort in desc order
103
-			return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && ip.Proto() == "tcp")
103
+			return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && strings.ToLower(ip.Proto()) == "tcp")
104 104
 		})
105 105
 	}
106 106
 	p = l.Ports[0]
... ...
@@ -357,7 +357,7 @@ func (srv *Server) ContainerChanges(name string) ([]Change, error) {
357 357
 func (srv *Server) Containers(all, size bool, n int, since, before string) []APIContainers {
358 358
 	var foundBefore bool
359 359
 	var displayed int
360
-	retContainers := []APIContainers{}
360
+	retContainers := make(map[string]APIContainers)
361 361
 
362 362
 	for _, container := range srv.runtime.List() {
363 363
 		if !container.State.Running && !all && n == -1 && since == "" && before == "" {
... ...
@@ -383,6 +383,8 @@ func (srv *Server) Containers(all, size bool, n int, since, before string) []API
383 383
 		c := APIContainers{
384 384
 			ID: container.ID,
385 385
 		}
386
+		c.Name = utils.TruncateID(container.ID)
387
+
386 388
 		c.Image = srv.runtime.repositories.ImageName(container.Image)
387 389
 		c.Command = fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
388 390
 		c.Created = container.Created.Unix()
... ...
@@ -391,9 +393,23 @@ func (srv *Server) Containers(all, size bool, n int, since, before string) []API
391 391
 		if size {
392 392
 			c.SizeRw, c.SizeRootFs = container.GetSize()
393 393
 		}
394
-		retContainers = append(retContainers, c)
394
+		retContainers[utils.TruncateID(c.ID)] = c
395
+	}
396
+	out := make([]APIContainers, len(retContainers))
397
+	var i int
398
+	for _, v := range retContainers {
399
+		out[i] = v
400
+		i++
401
+	}
402
+
403
+	// Add links to result
404
+	for _, link := range srv.runtime.links.GetAll() {
405
+		cp := retContainers[link.ToID]
406
+		c := cp
407
+		c.Name = fmt.Sprintf("%s/%s", link.FromID, link.Alias)
408
+		out = append(out, c)
395 409
 	}
396
-	return retContainers
410
+	return out
397 411
 }
398 412
 
399 413
 func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {