github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/port.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "strings" 6 7 Cli "github.com/docker/docker/cli" 8 flag "github.com/docker/docker/pkg/mflag" 9 "github.com/docker/go-connections/nat" 10 ) 11 12 // CmdPort lists port mappings for a container. 13 // If a private port is specified, it also shows the public-facing port that is NATed to the private port. 14 // 15 // Usage: docker port CONTAINER [PRIVATE_PORT[/PROTO]] 16 func (cli *DockerCli) CmdPort(args ...string) error { 17 cmd := Cli.Subcmd("port", []string{"CONTAINER [PRIVATE_PORT[/PROTO]]"}, Cli.DockerCommands["port"].Description, true) 18 cmd.Require(flag.Min, 1) 19 20 cmd.ParseFlags(args, true) 21 22 c, err := cli.client.ContainerInspect(cmd.Arg(0)) 23 if err != nil { 24 return err 25 } 26 27 if cmd.NArg() == 2 { 28 var ( 29 port = cmd.Arg(1) 30 proto = "tcp" 31 parts = strings.SplitN(port, "/", 2) 32 ) 33 34 if len(parts) == 2 && len(parts[1]) != 0 { 35 port = parts[0] 36 proto = parts[1] 37 } 38 natPort := port + "/" + proto 39 newP, err := nat.NewPort(proto, port) 40 if err != nil { 41 return err 42 } 43 if frontends, exists := c.NetworkSettings.Ports[newP]; exists && frontends != nil { 44 for _, frontend := range frontends { 45 fmt.Fprintf(cli.out, "%s:%s\n", frontend.HostIP, frontend.HostPort) 46 } 47 return nil 48 } 49 return fmt.Errorf("Error: No public port '%s' published for %s", natPort, cmd.Arg(0)) 50 } 51 52 for from, frontends := range c.NetworkSettings.Ports { 53 for _, frontend := range frontends { 54 fmt.Fprintf(cli.out, "%s -> %s:%s\n", from, frontend.HostIP, frontend.HostPort) 55 } 56 } 57 58 return nil 59 }