mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #35510 from ripcurld0/fix_35500
Display a warn message when there is binding ports and net mode is host
This commit is contained in:
commit
35d69f10a9
2 changed files with 55 additions and 1 deletions
|
|
@ -343,6 +343,16 @@ func (daemon *Daemon) verifyContainerSettings(platform string, hostConfig *conta
|
||||||
return nil, errors.Errorf("invalid isolation '%s' on %s", hostConfig.Isolation, runtime.GOOS)
|
return nil, errors.Errorf("invalid isolation '%s' on %s", hostConfig.Isolation, runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
warnings []string
|
||||||
|
)
|
||||||
// Now do platform-specific verification
|
// Now do platform-specific verification
|
||||||
return verifyPlatformContainerSettings(daemon, hostConfig, config, update)
|
if warnings, err = verifyPlatformContainerSettings(daemon, hostConfig, config, update); err != nil {
|
||||||
|
return warnings, err
|
||||||
|
}
|
||||||
|
if hostConfig.NetworkMode.IsHost() && len(hostConfig.PortBindings) > 0 {
|
||||||
|
warnings = append(warnings, "Published ports are discarded when using host network mode")
|
||||||
|
}
|
||||||
|
return warnings, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
44
daemon/container_unix_test.go
Normal file
44
daemon/container_unix_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
// +build linux freebsd
|
||||||
|
|
||||||
|
package daemon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
containertypes "github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/docker/docker/daemon/config"
|
||||||
|
"github.com/docker/go-connections/nat"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestContainerWarningHostAndPublishPorts that a warning is returned when setting network mode to host and specifying published ports.
|
||||||
|
// This should not be tested on Windows because Windows doesn't support "host" network mode.
|
||||||
|
func TestContainerWarningHostAndPublishPorts(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
ports nat.PortMap
|
||||||
|
warnings []string
|
||||||
|
}{
|
||||||
|
{ports: nat.PortMap{}},
|
||||||
|
{ports: nat.PortMap{
|
||||||
|
"8080": []nat.PortBinding{{HostPort: "8989"}},
|
||||||
|
}, warnings: []string{"Published ports are discarded when using host network mode"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
hostConfig := &containertypes.HostConfig{
|
||||||
|
Runtime: "runc",
|
||||||
|
NetworkMode: "host",
|
||||||
|
PortBindings: tc.ports,
|
||||||
|
}
|
||||||
|
cs := &config.Config{
|
||||||
|
CommonUnixConfig: config.CommonUnixConfig{
|
||||||
|
Runtimes: map[string]types.Runtime{"runc": {}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
d := &Daemon{configStore: cs}
|
||||||
|
wrns, err := d.verifyContainerSettings("", hostConfig, &containertypes.Config{}, false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tc.warnings, wrns)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue