2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
2021-02-15 10:00:07 -05:00
|
|
|
"net"
|
2022-12-01 04:58:40 -05:00
|
|
|
"sort"
|
2022-12-01 04:33:07 -05:00
|
|
|
"strconv"
|
2016-09-08 13:11:39 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/go-connections/nat"
|
2022-12-01 04:58:40 -05:00
|
|
|
"github.com/fvbommel/sortorder"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type portOptions struct {
|
|
|
|
container string
|
|
|
|
|
|
|
|
port string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPortCommand creates a new cobra.Command for `docker port`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewPortCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -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",
|
|
|
|
Args: cli.RequiresRangeArgs(1, 2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.container = args[0]
|
|
|
|
if len(args) > 1 {
|
|
|
|
opts.port = args[1]
|
|
|
|
}
|
2023-09-09 18:27:44 -04:00
|
|
|
return runPort(cmd.Context(), dockerCli, &opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker container port, docker port",
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.ContainerNames(dockerCli, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:20:57 -05:00
|
|
|
// 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 port.
|
2023-09-09 18:27:44 -04:00
|
|
|
func runPort(ctx context.Context, dockerCli command.Cli, opts *portOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:58:40 -05:00
|
|
|
var out []string
|
2016-09-08 13:11:39 -04:00
|
|
|
if opts.port != "" {
|
2022-12-01 04:33:07 -05:00
|
|
|
port, proto, _ := strings.Cut(opts.port, "/")
|
|
|
|
if proto == "" {
|
|
|
|
proto = "tcp"
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2022-12-01 04:33:07 -05:00
|
|
|
if _, err = strconv.ParseUint(port, 10, 16); err != nil {
|
|
|
|
return errors.Wrapf(err, "Error: invalid port (%s)", port)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2022-12-01 04:33:07 -05:00
|
|
|
frontends, exists := c.NetworkSettings.Ports[nat.Port(port+"/"+proto)]
|
|
|
|
if !exists || frontends == nil {
|
|
|
|
return errors.Errorf("Error: No public port '%s' published for %s", opts.port, opts.container)
|
|
|
|
}
|
|
|
|
for _, frontend := range frontends {
|
2022-12-01 04:58:40 -05:00
|
|
|
out = append(out, net.JoinHostPort(frontend.HostIP, frontend.HostPort))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for from, frontends := range c.NetworkSettings.Ports {
|
|
|
|
for _, frontend := range frontends {
|
|
|
|
out = append(out, fmt.Sprintf("%s -> %s", from, net.JoinHostPort(frontend.HostIP, frontend.HostPort)))
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:58:40 -05:00
|
|
|
if len(out) > 0 {
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(out[i], out[j])
|
|
|
|
})
|
|
|
|
_, _ = fmt.Fprintln(dockerCli.Out(), strings.Join(out, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|