runPort shows the port mapping for a given container. Optionally, it allows showing the mapping for a specific (container)port and proto. TODO(thaJeztah): currently this defaults to show the TCP port if no proto is specified. We should consider changing this to "any" protocol for the given private
(ctx context.Context, dockerCli command.Cli, opts *portOptions)
| 53 | // proto is specified. We should consider changing this to "any" protocol |
| 54 | // for the given private port. |
| 55 | func runPort(ctx context.Context, dockerCli command.Cli, opts *portOptions) error { |
| 56 | c, err := dockerCli.Client().ContainerInspect(ctx, opts.container, client.ContainerInspectOptions{}) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | var out []string |
| 62 | if opts.port != "" { |
| 63 | port, err := network.ParsePort(opts.port) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | frontends, exists := c.Container.NetworkSettings.Ports[port] |
| 68 | if !exists || len(frontends) == 0 { |
| 69 | return fmt.Errorf("no public port '%s' published for %s", opts.port, opts.container) |
| 70 | } |
| 71 | for _, frontend := range frontends { |
| 72 | out = append(out, net.JoinHostPort(frontend.HostIP.String(), frontend.HostPort)) |
| 73 | } |
| 74 | } else { |
| 75 | for from, frontends := range c.Container.NetworkSettings.Ports { |
| 76 | for _, frontend := range frontends { |
| 77 | out = append(out, fmt.Sprintf("%s -> %s", from, net.JoinHostPort(frontend.HostIP.String(), frontend.HostPort))) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if len(out) > 0 { |
| 83 | sort.Slice(out, func(i, j int) bool { |
| 84 | return sortorder.NaturalLess(out[i], out[j]) |
| 85 | }) |
| 86 | _, _ = fmt.Fprintln(dockerCli.Out(), strings.Join(out, "\n")) |
| 87 | } |
| 88 | |
| 89 | return nil |
| 90 | } |
no test coverage detected
searching dependent graphs…