Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 13, 2025

What does this PR do?

Backports testcontainers-go PR #3485 to fix UDP port binding. When nat.ParsePortSpecs() processes exposed ports like "8080/udp", it creates PortBinding with empty HostPort: "". Docker interprets this as "don't bind" (port 0) rather than "bind to random port".

Modified mergePortBindings() to convert empty HostPort to "0" for automatic allocation:

// Fix: Ensure that ports with empty HostPort get "0" for automatic allocation
for k, v := range exposedPortMap {
    for i := range v {
        if v[i].HostPort == "" {
            v[i].HostPort = "0" // Tell Docker to allocate a random port
        }
    }
    exposedPortMap[k] = v
}

Before:

c, err := Run(ctx,
    WithImage("alpine/socat:latest"),
    WithExposedPorts("8080/udp"),
)
port, _ := c.MappedPort(ctx, nat.NewPort("udp", "8080"))
// port.Port() returns "0" - unusable

After:

c, err := Run(ctx,
    WithImage("alpine/socat:latest"),
    WithExposedPorts("8080/udp"),
)
port, _ := c.MappedPort(ctx, nat.NewPort("udp", "8080"))
// port.Port() returns "55051" - actual mapped port

Why is it important?

UDP services in containers were unusable - MappedPort() always returned "0" instead of the actual host port, preventing connection establishment.

Related issues

Original prompt

I want you to backport testcontainers/testcontainers-go#3485 to docker/go-sdk


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: mdelapenya <951580+mdelapenya@users.noreply.github.com>
Copilot AI changed the title [WIP] Backport changes from testcontainers-go PR 3485 fix(udp): expose UDP ports properly Nov 13, 2025
Copilot AI requested a review from mdelapenya November 13, 2025 16:24
@mdelapenya mdelapenya changed the title fix(udp): expose UDP ports properly fix(container): expose UDP ports properly Nov 13, 2025
@mdelapenya mdelapenya marked this pull request as ready for review November 13, 2025 16:48
Copilot AI review requested due to automatic review settings November 13, 2025 16:48
@mdelapenya
Copy link
Member

Credits to @blueprismo

Copy link
Contributor

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 backports testcontainers-go PR #3485 to fix UDP port binding. The fix addresses a bug where MappedPort() always returned "0" for UDP ports instead of the actual mapped host port, making UDP services in containers unusable.

Key changes:

  • Modified mergePortBindings() to convert empty HostPort values to "0" for automatic Docker port allocation
  • Updated existing test expectations in lifecycle.create_test.go to reflect the fix
  • Added comprehensive test coverage for UDP port binding behavior

Reviewed Changes

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

File Description
container/lifecycle.create.go Added logic to convert empty HostPort to "0" in mergePortBindings() to enable proper port allocation
container/lifecycle.create_test.go Updated test expectations to reflect HostPort "0" instead of empty string
container/udp_port_binding_test.go Added comprehensive tests for UDP port binding fix and regression tests for TCP

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mdelapenya mdelapenya added the bug Something isn't working label Nov 13, 2025
@mdelapenya mdelapenya merged commit 8f6fa02 into main Nov 13, 2025
38 of 42 checks passed
@mdelapenya mdelapenya deleted the copilot/backport-pr-3485-to-docker-go-sdk branch November 13, 2025 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants