Skip to content

Commit

Permalink
Make port arg optional in docker compose port
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
crazybolillo committed Jun 4, 2024
1 parent 250c311 commit 357c13b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
41 changes: 38 additions & 3 deletions cmd/compose/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

type portOptions struct {
*ProjectOptions
list bool
port uint16
protocol string
index int
Expand All @@ -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 print the public port of 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
Expand All @@ -62,11 +68,40 @@ 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
}

if opts.list {
summaries, err := backend.Ps(ctx, projectName, api.PsOptions{Services: []string{service}})
if err != nil {
return err
}
if len(summaries) <= opts.index {
return fmt.Errorf("no container with the requested index (%d) is available", opts.index)
}
printPorts(dockerCli, summaries[opts.index].Publishers, opts)
return nil
}

ip, port, err := backend.Port(ctx, projectName, service, opts.port, api.PortOptions{
Protocol: opts.protocol,
Index: opts.index,
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/compose_port.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# docker compose port

<!---MARKER_GEN_START-->
Print the public port for a port binding
List port mappings or a specific mapping for the service

### Options

Expand Down
4 changes: 2 additions & 2 deletions docs/reference/docker_compose_port.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 357c13b

Please sign in to comment.