Browse code

'docker ps' shows port mappings

Solomon Hykes authored on 2013/04/20 11:29:13
Showing 2 changed files
... ...
@@ -677,7 +677,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
677 677
 	}
678 678
 	w := tabwriter.NewWriter(stdout, 12, 1, 3, ' ', 0)
679 679
 	if !*quiet {
680
-		fmt.Fprintln(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tCOMMENT")
680
+		fmt.Fprintln(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tCOMMENT\tPORTS")
681 681
 	}
682 682
 	for i, container := range srv.runtime.List() {
683 683
 		if !container.State.Running && !*flAll && *nLast == -1 {
... ...
@@ -698,6 +698,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
698 698
 				/* CREATED */ HumanDuration(time.Now().Sub(container.Created)) + " ago",
699 699
 				/* STATUS */ container.State.String(),
700 700
 				/* COMMENT */ "",
701
+				/* PORTS */ container.NetworkSettings.PortMappingHuman(),
701 702
 			} {
702 703
 				if idx == 0 {
703 704
 					w.Write([]byte(field))
... ...
@@ -11,7 +11,9 @@ import (
11 11
 	"os"
12 12
 	"os/exec"
13 13
 	"path"
14
+	"sort"
14 15
 	"strconv"
16
+	"strings"
15 17
 	"syscall"
16 18
 	"time"
17 19
 )
... ...
@@ -150,6 +152,16 @@ type NetworkSettings struct {
150 150
 	PortMapping map[string]string
151 151
 }
152 152
 
153
+// String returns a human-readable description of the port mapping defined in the settings
154
+func (settings *NetworkSettings) PortMappingHuman() string {
155
+	var mapping []string
156
+	for private, public := range settings.PortMapping {
157
+		mapping = append(mapping, fmt.Sprintf("%s->%s", public, private))
158
+	}
159
+	sort.Strings(mapping)
160
+	return strings.Join(mapping, ", ")
161
+}
162
+
153 163
 func (container *Container) Cmd() *exec.Cmd {
154 164
 	return container.cmd
155 165
 }