Skip to content

Conversation

thaJeztah
Copy link
Member

This forks the parsing-code from github.com/docker/go-connections/nat to remove the dependency on github.com/docker/go-connections.

Some follow-ups may be needed to also address;


frontend/dockerfile: fork nat.ParsePortSpecs

Fork the nat.ParsePortSpecs and related functions.

frontend/dockerfile: parsePortSpecs: simplify

BuildKit doesn't use the port-bindings, as EXPOSE only
allows for the ports (and protos) to expose. Simplify
the function to return just that.

frontend/dockerfile: parsePortSpec: remove unused code

The EXPOSE instruction in the Dockerfile does not use mapping,
only specifies a range of ports to expose, so we can skip
creating nat.PortMapping for each.

frontend/dockerfile: inline validateProto

@github-actions github-actions bot added area/dockerfile area/dependencies Pull requests that update a dependency file area/frontend labels Aug 11, 2025
@thaJeztah thaJeztah marked this pull request as draft August 11, 2025 09:07
@thaJeztah thaJeztah force-pushed the remove_go_connections branch from fc8c253 to 37aecb6 Compare August 11, 2025 09:12
@thaJeztah thaJeztah marked this pull request as ready for review August 11, 2025 09:12
@thaJeztah thaJeztah force-pushed the remove_go_connections branch from 37aecb6 to 1287add Compare August 11, 2025 09:46
@thaJeztah thaJeztah requested a review from Copilot August 11, 2025 09:46
Copilot

This comment was marked as outdated.

@thaJeztah thaJeztah force-pushed the remove_go_connections branch from 1287add to f07ded2 Compare August 11, 2025 09:54
return start, end, err
}

parts := strings.Split(ports, "-")
Copy link
Member Author

Choose a reason for hiding this comment

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

I think I had some changes stashed in go-connections to use strings.Cut here as well, but kept the implementation here to be like it was in the current version (may be opening follow-ups).

Copy link
Member Author

Choose a reason for hiding this comment

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

I can apply similar changes here in a follow-up.

@thaJeztah
Copy link
Member Author

/cc @crazy-max @tonistiigi PTAL

Copy link
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

Can we have unit tests backported as well?

switch proto {
case "":
return "tcp", port, nil
case "tcp", "udp", "sctp":
Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine to keep sctp as long as host kernel and network stack support SCTP.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, also went looking what's defined in the OCI spec, but it leaves it open as to what protocols must be supported. Not even sure if BuildKit should know about protocols, or just "anything goes" - at most, it should probably be defined in the OCI what format the proto takes (a-z (A-Z?), max length?);
https://github.com/opencontainers/image-spec/blob/2daaaaf0e7c16a6a147be91fde277f38573be672/config.md#properties

Comment on lines +98 to +99
// TODO(thaJeztah): mapping IP-addresses should not be allowed for EXPOSE; see https://github.com/moby/buildkit/issues/2173
if ip != "" && ip[0] == '[' {
Copy link
Member

Choose a reason for hiding this comment

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

Yeah ip doesn't make sense in a Dockerfile. Maybe we can keep it for now to avoid breaking people and implement a build check to warn users about it first?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, wasn't sure how the build-checks worked, but that was also in the back of my head (warn, not fail at start).

Leaving that for a follow-up for sure, not for this PR.

}

func validateProto(proto string) error {
proto = strings.ToLower(proto)
Copy link
Member

Choose a reason for hiding this comment

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

It might be good to have a build check for this one as well to warn if proto is not lower case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I hate that we have way too many places where we were a bit too permissive; it means we need to check multiple variations everywhere (and while perhaps TCP or tcp would be fine, I'm much less a fan of TcP or tCp being considered "valid").

Copy link
Member

Choose a reason for hiding this comment

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

Yes will look to have a build check for this

@thaJeztah
Copy link
Member Author

Can we have unit tests backported as well?

Let me have a look!

Fork the nat.ParsePortSpecs and related functions.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
BuildKit doesn't use the port-bindings, as EXPOSE only
allows for the ports (and protos) to expose. Simplify
the function to return just that.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Member Author

Updated; also applied the changes from docker/go-connections#143

The EXPOSE instruction in the Dockerfile does not use mapping,
only specifies a range of ports to expose, so we can skip
creating nat.PortMapping for each.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the remove_go_connections branch from 656d524 to 9962429 Compare August 12, 2025 16:05
@thaJeztah thaJeztah requested review from Copilot and crazy-max August 12, 2025 19:14
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes the dependency on github.com/docker/go-connections/nat by forking and simplifying the port parsing functionality specifically for the EXPOSE instruction in Dockerfiles. The changes maintain the same functionality while reducing external dependencies and tailoring the code to BuildKit's specific needs.

  • Forked nat.ParsePortSpecs and related functions into the BuildKit codebase
  • Simplified the parsing logic by removing port-binding functionality (EXPOSE only needs port/protocol info)
  • Added comprehensive test coverage for the new parsing functions

Reviewed Changes

Copilot reviewed 4 out of 10 changed files in this pull request and generated 1 comment.

File Description
go.mod Removes the dependency on github.com/docker/go-connections
frontend/dockerfile/dockerfile2llb/convert_expose.go Implements forked and simplified port parsing functions for EXPOSE instruction
frontend/dockerfile/dockerfile2llb/convert_expose_test.go Adds comprehensive test coverage for the new port parsing functionality
frontend/dockerfile/dockerfile2llb/convert.go Removes the old dispatchExpose function and import of nat package

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

LGTM thanks!

@crazy-max crazy-max merged commit 77850e6 into moby:master Aug 13, 2025
139 checks passed
@thaJeztah thaJeztah deleted the remove_go_connections branch August 13, 2025 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants