@@ -19,6 +19,8 @@ package compose
1919import (
2020 "context"
2121 "fmt"
22+ "net"
23+ "sort"
2224 "strconv"
2325 "strings"
2426
@@ -41,16 +43,20 @@ func portCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Backe
4143 ProjectOptions : p ,
4244 }
4345 cmd := & cobra.Command {
44- Use : "port [OPTIONS] SERVICE PRIVATE_PORT" ,
45- Short : "Print the public port for a port binding " ,
46- Args : cobra .MinimumNArgs ( 2 ),
46+ Use : "port [OPTIONS] SERVICE [ PRIVATE_PORT] " ,
47+ Short : "List port mappings or print the public port of a specific mapping for the service " ,
48+ Args : cobra .RangeArgs ( 1 , 2 ),
4749 PreRunE : Adapt (func (ctx context.Context , args []string ) error {
48- port , err := strconv .ParseUint (args [1 ], 10 , 16 )
49- if err != nil {
50- return err
51- }
52- opts .port = uint16 (port )
5350 opts .protocol = strings .ToLower (opts .protocol )
51+ if len (args ) > 1 {
52+ port , err := strconv .ParseUint (args [1 ], 10 , 16 )
53+ if err != nil {
54+ return err
55+ }
56+ opts .port = uint16 (port )
57+ } else {
58+ opts .protocol = ""
59+ }
5460 return nil
5561 }),
5662 RunE : Adapt (func (ctx context.Context , args []string ) error {
@@ -73,14 +79,22 @@ func runPort(ctx context.Context, dockerCli command.Cli, backendOptions *Backend
7379 if err != nil {
7480 return err
7581 }
76- ip , port , err := backend .Port (ctx , projectName , service , opts .port , api.PortOptions {
82+ publishers , err := backend .Ports (ctx , projectName , service , opts .port , api.PortOptions {
7783 Protocol : opts .protocol ,
7884 Index : opts .index ,
7985 })
8086 if err != nil {
8187 return err
8288 }
8389
84- _ , _ = fmt .Fprintf (dockerCli .Out (), "%s:%d\n " , ip , port )
90+ if opts .port != 0 && len (publishers ) > 0 {
91+ p := publishers [0 ]
92+ _ , _ = fmt .Fprintf (dockerCli .Out (), "%s\n " , net .JoinHostPort (p .URL , strconv .Itoa (p .PublishedPort )))
93+ return nil
94+ }
95+ sort .Sort (publishers )
96+ for _ , p := range publishers {
97+ _ , _ = fmt .Fprintln (dockerCli .Out (), p .String ())
98+ }
8599 return nil
86100}
0 commit comments