Browse code

docker port: lookup public-facing tcp ports for a container

Solomon Hykes authored on 2013/03/06 17:39:03
Showing 1 changed files
... ...
@@ -44,6 +44,7 @@ func (srv *Server) Help() string {
44 44
 		{"ps", "Display a list of containers"},
45 45
 		{"pull", "Download a tarball and create a container from it"},
46 46
 		{"put", "Upload a tarball and create a container from it"},
47
+		{"port", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"},
47 48
 		{"rm", "Remove containers"},
48 49
 		{"kill", "Kill a running container"},
49 50
 		{"wait", "Wait for the state of a container to change"},
... ...
@@ -311,6 +312,30 @@ func (srv *Server) CmdInspect(stdin io.ReadCloser, stdout io.Writer, args ...str
311 311
 	return nil
312 312
 }
313 313
 
314
+func (srv *Server) CmdPort(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
315
+	cmd := rcli.Subcmd(stdout, "port", "[OPTIONS] CONTAINER PRIVATE_PORT", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT")
316
+	if err := cmd.Parse(args); err != nil {
317
+		cmd.Usage()
318
+		return nil
319
+	}
320
+	if cmd.NArg() != 2 {
321
+		cmd.Usage()
322
+		return nil
323
+	}
324
+	name := cmd.Arg(0)
325
+	privatePort := cmd.Arg(1)
326
+	if container := srv.containers.Get(name); container == nil {
327
+		return errors.New("No such container: " + name)
328
+	} else {
329
+		if frontend, exists := container.NetworkSettings.PortMapping[privatePort]; !exists {
330
+			return fmt.Errorf("No private port '%s' allocated on %s", privatePort, name)
331
+		} else {
332
+			fmt.Fprintln(stdout, frontend)
333
+		}
334
+	}
335
+	return nil
336
+}
337
+
314 338
 // 'docker rmi NAME' removes all images with the name NAME
315 339
 func (srv *Server) CmdRmi(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
316 340
 	cmd := rcli.Subcmd(stdout, "rmimage", "[OPTIONS] IMAGE", "Remove an image")