2015-06-23 13:13:42 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
// ValidateNetMode ensures that the various combinations of requested
|
|
|
|
// network settings are valid.
|
|
|
|
func ValidateNetMode(c *Config, hc *HostConfig) error {
|
|
|
|
// We may not be passed a host config, such as in the case of docker commit
|
|
|
|
if hc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(string(hc.NetworkMode), ":")
|
2015-06-23 13:13:42 -04:00
|
|
|
switch mode := parts[0]; mode {
|
|
|
|
case "default", "bridge", "none", "host":
|
|
|
|
case "container":
|
|
|
|
if len(parts) < 2 || parts[1] == "" {
|
2015-07-09 18:12:36 -04:00
|
|
|
return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
|
2015-06-23 13:13:42 -04:00
|
|
|
}
|
|
|
|
default:
|
2015-07-09 18:12:36 -04:00
|
|
|
return fmt.Errorf("invalid --net: %s", hc.NetworkMode)
|
2015-06-23 13:13:42 -04:00
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
|
2015-06-23 13:13:42 -04:00
|
|
|
return ErrConflictNetworkHostname
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
|
2015-06-23 13:13:42 -04:00
|
|
|
return ErrConflictHostNetworkAndLinks
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
|
2015-06-23 13:13:42 -04:00
|
|
|
return ErrConflictContainerNetworkAndLinks
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
|
2015-07-25 05:11:45 -04:00
|
|
|
return ErrConflictNetworkAndDNS
|
2015-06-23 13:13:42 -04:00
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 {
|
2015-06-23 13:13:42 -04:00
|
|
|
return ErrConflictNetworkHosts
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
|
2015-06-23 13:13:42 -04:00
|
|
|
return ErrConflictContainerNetworkAndMac
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
|
2015-06-23 13:13:42 -04:00
|
|
|
return ErrConflictNetworkPublishPorts
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
|
2015-06-23 13:13:42 -04:00
|
|
|
return ErrConflictNetworkExposePorts
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|