mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #11516 from coolljt0725/add_show_error_set_some_flag_in_container_netmode
add support --net=container with --mac-address, --add-host error out
This commit is contained in:
commit
6b6a26c769
5 changed files with 63 additions and 23 deletions
|
@ -1116,7 +1116,7 @@ func (container *Container) setupContainerDns() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.NetworkMode != "host" {
|
if config.NetworkMode.IsBridge() || config.NetworkMode.IsNone() {
|
||||||
// check configurations for any container/daemon dns settings
|
// check configurations for any container/daemon dns settings
|
||||||
if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
|
if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -282,7 +282,8 @@ With the networking mode set to `host` a container will share the host's
|
||||||
network stack and all interfaces from the host will be available to the
|
network stack and all interfaces from the host will be available to the
|
||||||
container. The container's hostname will match the hostname on the host
|
container. The container's hostname will match the hostname on the host
|
||||||
system. Publishing ports and linking to other containers will not work
|
system. Publishing ports and linking to other containers will not work
|
||||||
when sharing the host's network stack.
|
when sharing the host's network stack. Note that `--add-host` `--hostname`
|
||||||
|
`--dns` `--dns-search` and `--mac-address` is invalid in `host` netmode.
|
||||||
|
|
||||||
Compared to the default `bridge` mode, the `host` mode gives *significantly*
|
Compared to the default `bridge` mode, the `host` mode gives *significantly*
|
||||||
better networking performance since it uses the host's native networking stack
|
better networking performance since it uses the host's native networking stack
|
||||||
|
@ -298,7 +299,9 @@ or a High Performance Web Server.
|
||||||
|
|
||||||
With the networking mode set to `container` a container will share the
|
With the networking mode set to `container` a container will share the
|
||||||
network stack of another container. The other container's name must be
|
network stack of another container. The other container's name must be
|
||||||
provided in the format of `--net container:<name|id>`.
|
provided in the format of `--net container:<name|id>`. Note that `--add-host`
|
||||||
|
`--hostname` `--dns` `--dns-search` and `--mac-address` is invalid
|
||||||
|
in `container` netmode.
|
||||||
|
|
||||||
Example running a Redis container with Redis binding to `localhost` then
|
Example running a Redis container with Redis binding to `localhost` then
|
||||||
running the `redis-cli` command and connecting to the Redis server over the
|
running the `redis-cli` command and connecting to the Redis server over the
|
||||||
|
|
|
@ -371,6 +371,33 @@ func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) {
|
||||||
|
cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
|
||||||
|
out, _, err := runCommandWithOutput(cmd)
|
||||||
|
if err != nil {
|
||||||
|
c.Fatalf("failed to run container: %v, output: %q", err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = exec.Command(dockerBinary, "run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
|
||||||
|
out, _, err = runCommandWithOutput(cmd)
|
||||||
|
if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") {
|
||||||
|
c.Fatalf("run --net=container with --dns should error out")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
|
||||||
|
out, _, err = runCommandWithOutput(cmd)
|
||||||
|
if err == nil || !strings.Contains(out, "--mac-address and the network mode") {
|
||||||
|
c.Fatalf("run --net=container with --mac-address should error out")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = exec.Command(dockerBinary, "run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
|
||||||
|
out, _, err = runCommandWithOutput(cmd)
|
||||||
|
if err == nil || !strings.Contains(out, "--add-host and the network mode") {
|
||||||
|
c.Fatalf("run --net=container with --add-host should error out")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
|
func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
|
||||||
testRequires(c, ExecSupport)
|
testRequires(c, ExecSupport)
|
||||||
cmd := exec.Command(dockerBinary, "run", "-i", "-d", "--name", "parent", "busybox", "top")
|
cmd := exec.Command(dockerBinary, "run", "-i", "-d", "--name", "parent", "busybox", "top")
|
||||||
|
|
|
@ -21,6 +21,10 @@ func (n NetworkMode) IsPrivate() bool {
|
||||||
return !(n.IsHost() || n.IsContainer() || n.IsNone())
|
return !(n.IsHost() || n.IsContainer() || n.IsNone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n NetworkMode) IsBridge() bool {
|
||||||
|
return n == "bridge"
|
||||||
|
}
|
||||||
|
|
||||||
func (n NetworkMode) IsHost() bool {
|
func (n NetworkMode) IsHost() bool {
|
||||||
return n == "host"
|
return n == "host"
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,11 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrConflictContainerNetworkAndLinks = fmt.Errorf("Conflicting options: --net=container can't be used with links. This would result in undefined behavior.")
|
ErrConflictContainerNetworkAndLinks = fmt.Errorf("Conflicting options: --net=container can't be used with links. This would result in undefined behavior.")
|
||||||
ErrConflictContainerNetworkAndDns = fmt.Errorf("Conflicting options: --net=container can't be used with --dns. This configuration is invalid.")
|
ErrConflictNetworkAndDns = fmt.Errorf("Conflicting options: --dns and the network mode (--net).")
|
||||||
ErrConflictNetworkHostname = fmt.Errorf("Conflicting options: -h and the network mode (--net)")
|
ErrConflictNetworkHostname = fmt.Errorf("Conflicting options: -h and the network mode (--net)")
|
||||||
ErrConflictHostNetworkAndDns = fmt.Errorf("Conflicting options: --net=host can't be used with --dns. This configuration is invalid.")
|
|
||||||
ErrConflictHostNetworkAndLinks = fmt.Errorf("Conflicting options: --net=host can't be used with links. This would result in undefined behavior.")
|
ErrConflictHostNetworkAndLinks = fmt.Errorf("Conflicting options: --net=host can't be used with links. This would result in undefined behavior.")
|
||||||
|
ErrConflictContainerNetworkAndMac = fmt.Errorf("Conflicting options: --mac-address and the network mode (--net).")
|
||||||
|
ErrConflictNetworkHosts = fmt.Errorf("Conflicting options: --add-host and the network mode (--net).")
|
||||||
)
|
)
|
||||||
|
|
||||||
func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSet, error) {
|
func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSet, error) {
|
||||||
|
@ -101,36 +102,46 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
|
||||||
return nil, nil, cmd, err
|
return nil, nil, cmd, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate input params starting with the input mac address
|
|
||||||
if *flMacAddress != "" {
|
|
||||||
if _, err := opts.ValidateMACAddress(*flMacAddress); err != nil {
|
|
||||||
return nil, nil, cmd, fmt.Errorf("%s is not a valid mac address", *flMacAddress)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var (
|
var (
|
||||||
attachStdin = flAttach.Get("stdin")
|
attachStdin = flAttach.Get("stdin")
|
||||||
attachStdout = flAttach.Get("stdout")
|
attachStdout = flAttach.Get("stdout")
|
||||||
attachStderr = flAttach.Get("stderr")
|
attachStderr = flAttach.Get("stderr")
|
||||||
)
|
)
|
||||||
|
|
||||||
if *flNetMode != "bridge" && *flNetMode != "none" && *flHostname != "" {
|
netMode, err := parseNetMode(*flNetMode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, cmd, fmt.Errorf("--net: invalid net mode: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (netMode.IsHost() || netMode.IsContainer()) && *flHostname != "" {
|
||||||
return nil, nil, cmd, ErrConflictNetworkHostname
|
return nil, nil, cmd, ErrConflictNetworkHostname
|
||||||
}
|
}
|
||||||
|
|
||||||
if *flNetMode == "host" && flLinks.Len() > 0 {
|
if netMode.IsHost() && flLinks.Len() > 0 {
|
||||||
return nil, nil, cmd, ErrConflictHostNetworkAndLinks
|
return nil, nil, cmd, ErrConflictHostNetworkAndLinks
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(*flNetMode, "container") && flLinks.Len() > 0 {
|
if netMode.IsContainer() && flLinks.Len() > 0 {
|
||||||
return nil, nil, cmd, ErrConflictContainerNetworkAndLinks
|
return nil, nil, cmd, ErrConflictContainerNetworkAndLinks
|
||||||
}
|
}
|
||||||
|
|
||||||
if *flNetMode == "host" && flDns.Len() > 0 {
|
if (netMode.IsHost() || netMode.IsContainer()) && flDns.Len() > 0 {
|
||||||
return nil, nil, cmd, ErrConflictHostNetworkAndDns
|
return nil, nil, cmd, ErrConflictNetworkAndDns
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(*flNetMode, "container") && flDns.Len() > 0 {
|
if (netMode.IsContainer() || netMode.IsHost()) && flExtraHosts.Len() > 0 {
|
||||||
return nil, nil, cmd, ErrConflictContainerNetworkAndDns
|
return nil, nil, cmd, ErrConflictNetworkHosts
|
||||||
|
}
|
||||||
|
|
||||||
|
if (netMode.IsContainer() || netMode.IsHost()) && *flMacAddress != "" {
|
||||||
|
return nil, nil, cmd, ErrConflictContainerNetworkAndMac
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the input mac address
|
||||||
|
if *flMacAddress != "" {
|
||||||
|
if _, err := opts.ValidateMACAddress(*flMacAddress); err != nil {
|
||||||
|
return nil, nil, cmd, fmt.Errorf("%s is not a valid mac address", *flMacAddress)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If neither -d or -a are set, attach to everything by default
|
// If neither -d or -a are set, attach to everything by default
|
||||||
|
@ -267,11 +278,6 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
|
||||||
return nil, nil, cmd, fmt.Errorf("--pid: invalid PID mode")
|
return nil, nil, cmd, fmt.Errorf("--pid: invalid PID mode")
|
||||||
}
|
}
|
||||||
|
|
||||||
netMode, err := parseNetMode(*flNetMode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, cmd, fmt.Errorf("--net: invalid net mode: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
restartPolicy, err := ParseRestartPolicy(*flRestartPolicy)
|
restartPolicy, err := ParseRestartPolicy(*flRestartPolicy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, cmd, err
|
return nil, nil, cmd, err
|
||||||
|
|
Loading…
Reference in a new issue