From c87db390350b7ea8b899d3bca26f7becb8b0d7b7 Mon Sep 17 00:00:00 2001 From: Antonio Aguilar Date: Thu, 30 May 2024 20:17:12 -0600 Subject: [PATCH] Make port arg optional in `docker compose port` This comand no longer needs a mandatory port. If no port is provided it will instead list all published ports. This behavior falls in line with the behavior provided by `docker port`. Closes #11859. Signed-off-by: Antonio Aguilar --- cmd/compose/port.go | 56 +++++++++++++++++++------ docs/reference/compose.md | 2 +- docs/reference/compose_port.md | 2 +- docs/reference/docker_compose_port.yaml | 4 +- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/cmd/compose/port.go b/cmd/compose/port.go index 59ea8ef1ce..02978e856c 100644 --- a/cmd/compose/port.go +++ b/cmd/compose/port.go @@ -30,6 +30,7 @@ import ( type portOptions struct { *ProjectOptions + list bool port uint16 protocol string index int @@ -40,10 +41,15 @@ func portCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) ProjectOptions: p, } cmd := &cobra.Command{ - Use: "port [OPTIONS] SERVICE PRIVATE_PORT", - Short: "Print the public port for a port binding", - Args: cobra.MinimumNArgs(2), + Use: "port [OPTIONS] SERVICE [PRIVATE_PORT]", + Short: "List port mappings or a specific mapping for the service", + Args: cobra.RangeArgs(1, 2), PreRunE: Adapt(func(ctx context.Context, args []string) error { + if len(args) == 1 { + opts.list = true + return nil + } + port, err := strconv.ParseUint(args[1], 10, 16) if err != nil { return err @@ -62,19 +68,45 @@ func portCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) return cmd } +func printPorts(dockerCli command.Cli, publishers api.PortPublishers, opts portOptions) { + for _, publisher := range publishers { + if publisher.Protocol != opts.protocol { + continue + } + + var format string + if strings.Contains(publisher.URL, ":") { + format = "%d/%s -> [%s]:%d\n" + } else { + format = "%d/%s -> %s:%d\n" + } + fmt.Fprintf(dockerCli.Out(), format, publisher.PublishedPort, publisher.Protocol, publisher.URL, publisher.TargetPort) + } +} + func runPort(ctx context.Context, dockerCli command.Cli, backend api.Service, opts portOptions, service string) error { projectName, err := opts.toProjectName(ctx, dockerCli) if err != nil { return err } - ip, port, err := backend.Port(ctx, projectName, service, opts.port, api.PortOptions{ - Protocol: opts.protocol, - Index: opts.index, - }) - if err != nil { - return err - } - fmt.Fprintf(dockerCli.Out(), "%s:%d\n", ip, port) - return nil + if opts.list { + summaries, err := backend.Ps(ctx, projectName, api.PsOptions{Services: []string{service}}) + if err != nil { + return err + } + printPorts(dockerCli, summaries[opts.index].Publishers, opts) + return nil + } else { + ip, port, err := backend.Port(ctx, projectName, service, opts.port, api.PortOptions{ + Protocol: opts.protocol, + Index: opts.index, + }) + if err != nil { + return err + } + + fmt.Fprintf(dockerCli.Out(), "%s:%d\n", ip, port) + return nil + } } diff --git a/docs/reference/compose.md b/docs/reference/compose.md index 7770f785a0..1cff698861 100644 --- a/docs/reference/compose.md +++ b/docs/reference/compose.md @@ -20,7 +20,7 @@ Define and run multi-container applications with Docker | [`logs`](compose_logs.md) | View output from containers | | [`ls`](compose_ls.md) | List running compose projects | | [`pause`](compose_pause.md) | Pause services | -| [`port`](compose_port.md) | Print the public port for a port binding | +| [`port`](compose_port.md) | List port mappings or a specific mapping for the service | | [`ps`](compose_ps.md) | List containers | | [`pull`](compose_pull.md) | Pull service images | | [`push`](compose_push.md) | Push service images | diff --git a/docs/reference/compose_port.md b/docs/reference/compose_port.md index 5e70b35329..7f1cd6faaa 100644 --- a/docs/reference/compose_port.md +++ b/docs/reference/compose_port.md @@ -1,7 +1,7 @@ # docker compose port -Print the public port for a port binding +List port mappings or a specific mapping for the service ### Options diff --git a/docs/reference/docker_compose_port.yaml b/docs/reference/docker_compose_port.yaml index 8a07f31ea5..6164069b5c 100644 --- a/docs/reference/docker_compose_port.yaml +++ b/docs/reference/docker_compose_port.yaml @@ -1,7 +1,7 @@ command: docker compose port -short: Print the public port for a port binding +short: List port mappings or a specific mapping for the service long: Prints the public port for a port binding -usage: docker compose port [OPTIONS] SERVICE PRIVATE_PORT +usage: docker compose port [OPTIONS] SERVICE [PRIVATE_PORT] pname: docker compose plink: docker_compose.yaml options: