2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-03-16 19:19:22 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-12-18 17:58:48 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2015-03-25 03:57:23 +00:00
|
|
|
)
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdPort lists port mappings for a container.
|
|
|
|
// If a private port is specified, it also shows the public-facing port that is NATed to the private port.
|
|
|
|
//
|
|
|
|
// Usage: docker port CONTAINER [PRIVATE_PORT[/PROTO]]
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdPort(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("port", []string{"CONTAINER [PRIVATE_PORT[/PROTO]]"}, Cli.DockerCommands["port"].Description, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
cmd.Require(flag.Min, 1)
|
2015-07-03 09:26:09 +00:00
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2016-03-16 19:19:22 +00:00
|
|
|
c, err := cli.client.ContainerInspect(context.Background(), cmd.Arg(0))
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.NArg() == 2 {
|
|
|
|
var (
|
|
|
|
port = cmd.Arg(1)
|
|
|
|
proto = "tcp"
|
|
|
|
parts = strings.SplitN(port, "/", 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(parts) == 2 && len(parts[1]) != 0 {
|
|
|
|
port = parts[0]
|
|
|
|
proto = parts[1]
|
|
|
|
}
|
|
|
|
natPort := port + "/" + proto
|
2015-07-16 03:45:48 +00:00
|
|
|
newP, err := nat.NewPort(proto, port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if frontends, exists := c.NetworkSettings.Ports[newP]; exists && frontends != nil {
|
2015-03-25 03:57:23 +00:00
|
|
|
for _, frontend := range frontends {
|
2015-07-21 13:43:32 +00:00
|
|
|
fmt.Fprintf(cli.out, "%s:%s\n", frontend.HostIP, frontend.HostPort)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error: No public port '%s' published for %s", natPort, cmd.Arg(0))
|
|
|
|
}
|
|
|
|
|
2015-04-04 04:06:48 +00:00
|
|
|
for from, frontends := range c.NetworkSettings.Ports {
|
2015-03-25 03:57:23 +00:00
|
|
|
for _, frontend := range frontends {
|
2015-07-21 13:43:32 +00:00
|
|
|
fmt.Fprintf(cli.out, "%s -> %s:%s\n", from, frontend.HostIP, frontend.HostPort)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|