Skip to content

Fix port forwarding not working for non-127.0.0.1 localhost in rootless #3831

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 1 commit into from
Jan 28, 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
43 changes: 43 additions & 0 deletions cmd/nerdctl/container/container_run_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/containerd/nerdctl/v2/pkg/strutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
)

Expand Down Expand Up @@ -567,3 +568,45 @@ func TestIssue3568(t *testing.T) {

testCase.Run(t)
}

// TestPortBindingWithCustomHost tests https://github.com/containerd/nerdctl/issues/3539
func TestPortBindingWithCustomHost(t *testing.T) {
testCase := nerdtest.Setup()

const (
host = "127.0.0.2"
hostPort = 8080
)
address := fmt.Sprintf("%s:%d", host, hostPort)

testCase.SubTests = []*test.Case{
{
Description: "Issue #3539 - Access to a container running when 127.0.0.2 is specified in -p in rootless mode.",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier(), "-p", fmt.Sprintf("%s:80", address), testutil.NginxAlpineImage)
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Errors: []error{},
Output: test.All(
func(stdout string, info string, t *testing.T) {
resp, err := nettestutil.HTTPGet(address, 30, false)
assert.NilError(t, err)

respBody, err := io.ReadAll(resp.Body)
assert.NilError(t, err)
assert.Assert(t, strings.Contains(string(respBody), testutil.NginxAlpineIndexHTMLSnippet))
},
),
}
},
},
}

testCase.Run(t)
}