mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
PublishAllPorts: don't crash with nil PortBindings
Introduced by commit 85b260f ("PublishAllPorts: create
port mappings for exposed ports").
Signed-off-by: Rob Murray <rob.murray@docker.com>
This commit is contained in:
@@ -42,6 +42,9 @@ func decodeCreateRequest(src io.Reader) (container.CreateRequest, error) {
|
||||
if w.HostConfig == nil {
|
||||
w.HostConfig = &container.HostConfig{}
|
||||
}
|
||||
if w.HostConfig.PortBindings == nil {
|
||||
w.HostConfig.PortBindings = make(network.PortMap)
|
||||
}
|
||||
// Make sure NetworkMode has an acceptable value. We do this to ensure
|
||||
// backwards compatible API behavior.
|
||||
//
|
||||
|
||||
@@ -72,6 +72,13 @@ func WithSysctls(sysctls map[string]string) func(*TestContainerConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
// WithPublishAllPorts sets PublishAllPorts.
|
||||
func WithPublishAllPorts(publishAll bool) func(*TestContainerConfig) {
|
||||
return func(c *TestContainerConfig) {
|
||||
c.HostConfig.PublishAllPorts = publishAll
|
||||
}
|
||||
}
|
||||
|
||||
// WithExposedPorts sets the exposed ports of the container
|
||||
func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
|
||||
return func(c *TestContainerConfig) {
|
||||
|
||||
@@ -22,12 +22,14 @@ import (
|
||||
"github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge"
|
||||
"github.com/moby/moby/v2/daemon/libnetwork/iptables"
|
||||
"github.com/moby/moby/v2/daemon/libnetwork/netlabel"
|
||||
"github.com/moby/moby/v2/integration/internal/build"
|
||||
"github.com/moby/moby/v2/integration/internal/container"
|
||||
"github.com/moby/moby/v2/integration/internal/network"
|
||||
"github.com/moby/moby/v2/integration/internal/testutils/networking"
|
||||
n "github.com/moby/moby/v2/integration/network"
|
||||
"github.com/moby/moby/v2/internal/testutil"
|
||||
"github.com/moby/moby/v2/internal/testutil/daemon"
|
||||
"github.com/moby/moby/v2/internal/testutil/fakecontext"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
"gotest.tools/v3/icmd"
|
||||
@@ -2139,3 +2141,18 @@ func TestGatewayErrorOnNetDisconnect(t *testing.T) {
|
||||
assert.Check(t, is.Contains(ctrInsp.NetworkSettings.Networks, "n1"))
|
||||
assert.Check(t, is.Contains(ctrInsp.NetworkSettings.Networks, "n2"))
|
||||
}
|
||||
|
||||
// Regression test for https://github.com/moby/moby/issues/51620
|
||||
func TestPublishAllWithNilPortBindings(t *testing.T) {
|
||||
ctx := setupTest(t)
|
||||
c := testEnv.APIClient()
|
||||
|
||||
imgWithExpose := container.WithImage(build.Do(ctx, t, c,
|
||||
fakecontext.New(t, "", fakecontext.WithDockerfile("FROM busybox\nEXPOSE 80/tcp\n"))))
|
||||
|
||||
_ = container.Run(ctx, t, c,
|
||||
container.WithAutoRemove,
|
||||
container.WithPublishAllPorts(true),
|
||||
imgWithExpose,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user