2016-06-05 19:48:26 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-05 19:48:26 -04:00
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type portOptions struct {
|
|
|
|
container string
|
|
|
|
|
|
|
|
port string
|
|
|
|
}
|
|
|
|
|
2016-07-16 05:55:18 -04:00
|
|
|
// NewPortCommand creates a new cobra.Command for `docker port`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewPortCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-05 19:48:26 -04:00
|
|
|
var opts portOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "port CONTAINER [PRIVATE_PORT[/PROTO]]",
|
|
|
|
Short: "List port mappings or a specific mapping for the container",
|
2016-06-06 10:57:44 -04:00
|
|
|
Args: cli.RequiresRangeArgs(1, 2),
|
2016-06-05 19:48:26 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.container = args[0]
|
|
|
|
if len(args) > 1 {
|
|
|
|
opts.port = args[1]
|
|
|
|
}
|
|
|
|
return runPort(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runPort(dockerCli *command.DockerCli, opts *portOptions) error {
|
2016-06-05 19:48:26 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.port != "" {
|
|
|
|
port := opts.port
|
|
|
|
proto := "tcp"
|
|
|
|
parts := strings.SplitN(port, "/", 2)
|
|
|
|
|
|
|
|
if len(parts) == 2 && len(parts[1]) != 0 {
|
|
|
|
port = parts[0]
|
|
|
|
proto = parts[1]
|
|
|
|
}
|
|
|
|
natPort := port + "/" + proto
|
|
|
|
newP, err := nat.NewPort(proto, port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if frontends, exists := c.NetworkSettings.Ports[newP]; exists && frontends != nil {
|
|
|
|
for _, frontend := range frontends {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s:%s\n", frontend.HostIP, frontend.HostPort)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error: No public port '%s' published for %s", natPort, opts.container)
|
|
|
|
}
|
|
|
|
|
|
|
|
for from, frontends := range c.NetworkSettings.Ports {
|
|
|
|
for _, frontend := range frontends {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s -> %s:%s\n", from, frontend.HostIP, frontend.HostPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|