Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make port arg optional in docker compose port #11874

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

crazybolillo
Copy link

What I did

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.

Related issue
Closes #11859.

@crazybolillo crazybolillo force-pushed the 11859-crazybolillo branch 2 times, most recently from 9670fd8 to c87db39 Compare June 1, 2024 19:05
Copy link
Contributor

@glours glours left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution, I have some small suggestions to improve your changes

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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Short: "List port mappings or a specific mapping for the service",
Short: "List port mappings or print the public port for a specific mapping for the service",

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just the same help used in docker/cli

cmd/compose/port.go Outdated Show resolved Hide resolved
cmd/compose/port.go Outdated Show resolved Hide resolved
@crazybolillo crazybolillo force-pushed the 11859-crazybolillo branch 2 times, most recently from 357c13b to 21c0a70 Compare June 4, 2024 02:03
@crazybolillo
Copy link
Author

Thanks for the review, I have taken care of your observations.

@crazybolillo crazybolillo requested a review from glours June 5, 2024 04:45
Copy link
Contributor

@glours glours left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@glours glours requested a review from ndeloof June 5, 2024 07:30
PreRunE: Adapt(func(ctx context.Context, args []string) error {
if len(args) == 1 {
opts.list = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incompatibility with --index should be tested

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should --index be prohibited if no specific port was provided?

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}})
Copy link
Contributor

@ndeloof ndeloof Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better update backend.Port so it can manage multiple services. Could return PortPublishers

if err != nil {
return err
}
if len(summaries) <= opts.index {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[]ContainerSummary index is unrelated to replica index in service set by --index.

}

var format string
if strings.Contains(publisher.URL, ":") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would make sense to me PortPublisher has a String() method to handle this, aligned with docker/cli

@ndeloof
Copy link
Contributor

ndeloof commented Jun 11, 2024

while not strictly required, I think we should use this opportunity while revisiting this command to add support for app_protocol and port name (https://github.com/compose-spec/compose-spec/blob/master/05-services.md#long-syntax-3)

@crazybolillo
Copy link
Author

while not strictly required, I think we should use this opportunity while revisiting this command to add support for app_protocol and port name (https://github.com/compose-spec/compose-spec/blob/master/05-services.md#long-syntax-3)

Wow, did not even know those existed. How do you envision it? Allow filtering? Or just displaying the name and app_protocol if they exist for the given 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 docker#11859.

Signed-off-by: Antonio Aguilar <[email protected]>
@crazybolillo
Copy link
Author

Went ahead and changed backend.Port so it returns a slice of publishers. That should fix other issues like dealing with --index. Since I was modifying PortPublisher I went ahead and also changed its port members to be uint16 so it falls in line with types.Port

I think the only thing left is the question about --index incompatibility, and support for app_protocol and port name,

@@ -26,18 +26,40 @@ import (
moby "github.com/docker/docker/api/types"
)

func (s *composeService) Port(ctx context.Context, projectName string, service string, port uint16, options api.PortOptions) (string, int, error) {
func (s *composeService) Port(ctx context.Context, projectName string, service string, port uint16, options api.PortOptions) (api.PortPublishers, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as signature changes, and breaks compatibility, makes sense to also rename Ports(plural)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to denote information about more than one port can be returned. However wouldn't it be weird since it breaks the pattern of backend methods having the same name as their CLI command? Also if we were to rename the method, I think we should also rename the file to ports.go?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof Sorry to bother but do you have any thoughts on my comments? Should we still perform the renaming?

@ndeloof
Copy link
Contributor

ndeloof commented Jun 12, 2024

--index is used to select service replica, already implemented in backend.Port calling getSpecifiedContainer
Adding support for app_protocol would not be that simple, as such metadata only exists in compose model, not in actual resources. Let's keep this for a future improvement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make PRIVATE_PORT optional for docker compose port
3 participants