Browse Source

'docker ps' shows port mappings

Solomon Hykes 12 years ago
parent
commit
931ca464a7
2 changed files with 14 additions and 1 deletions
  1. 2 1
      commands.go
  2. 12 0
      container.go

+ 2 - 1
commands.go

@@ -677,7 +677,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
 	}
 	w := tabwriter.NewWriter(stdout, 12, 1, 3, ' ', 0)
 	if !*quiet {
-		fmt.Fprintln(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tCOMMENT")
+		fmt.Fprintln(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tCOMMENT\tPORTS")
 	}
 	for i, container := range srv.runtime.List() {
 		if !container.State.Running && !*flAll && *nLast == -1 {
@@ -698,6 +698,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
 				/* CREATED */ HumanDuration(time.Now().Sub(container.Created)) + " ago",
 				/* STATUS */ container.State.String(),
 				/* COMMENT */ "",
+				/* PORTS */ container.NetworkSettings.PortMappingHuman(),
 			} {
 				if idx == 0 {
 					w.Write([]byte(field))

+ 12 - 0
container.go

@@ -11,7 +11,9 @@ import (
 	"os"
 	"os/exec"
 	"path"
+	"sort"
 	"strconv"
+	"strings"
 	"syscall"
 	"time"
 )
@@ -150,6 +152,16 @@ type NetworkSettings struct {
 	PortMapping map[string]string
 }
 
+// String returns a human-readable description of the port mapping defined in the settings
+func (settings *NetworkSettings) PortMappingHuman() string {
+	var mapping []string
+	for private, public := range settings.PortMapping {
+		mapping = append(mapping, fmt.Sprintf("%s->%s", public, private))
+	}
+	sort.Strings(mapping)
+	return strings.Join(mapping, ", ")
+}
+
 func (container *Container) Cmd() *exec.Cmd {
 	return container.cmd
 }