Skip to content

Fix/reserve port #114

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

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions internal/local_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,36 @@ func (d *LocalRunner) Stop() error {
// reservePort finds the first available port from the startPort and reserves it
// Note that we have to keep track of the port in 'reservedPorts' because
// the port allocation happens before the services uses it and binds to it.
func (d *LocalRunner) reservePort(startPort int) int {
func (d *LocalRunner) reservePort(startPort int, protocol string) int {
for i := startPort; i < startPort+1000; i++ {
if _, ok := d.reservedPorts[i]; ok {
continue
}
// make a net.Listen on the port to see if it is aavailable
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", i))
if err != nil {
continue

bindAddr := "0.0.0.0"
if d.bindHostPortsLocally {
bindAddr = "127.0.0.1"
}
listener.Close()

if protocol == ProtocolUDP {
listener, err := net.ListenUDP("udp", &net.UDPAddr{
Port: i,
IP: net.ParseIP(bindAddr),
})
if err != nil {
continue
}
listener.Close()
} else if protocol == ProtocolTCP {
listener, err := net.Listen(protocol, fmt.Sprintf("%s:%d", bindAddr, i))
if err != nil {
continue
}
listener.Close()
} else {
panic(fmt.Sprintf("invalid protocol: %s", protocol))
}

d.reservedPorts[i] = true
return i
}
Expand Down Expand Up @@ -642,7 +661,7 @@ func (d *LocalRunner) generateDockerCompose() ([]byte, error) {
// between services running inside docker and the ones running on the host machine.
for _, svc := range d.manifest.services {
for _, port := range svc.ports {
port.HostPort = d.reservePort(port.Port)
port.HostPort = d.reservePort(port.Port, port.Protocol)
}
}

Expand Down