parse_unix.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // +build !windows
  2. package runconfig
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. // ValidateNetMode ensures that the various combinations of requested
  8. // network settings are valid.
  9. func ValidateNetMode(c *Config, hc *HostConfig) error {
  10. // We may not be passed a host config, such as in the case of docker commit
  11. if hc == nil {
  12. return nil
  13. }
  14. parts := strings.Split(string(hc.NetworkMode), ":")
  15. switch mode := parts[0]; mode {
  16. case "default", "bridge", "none", "host":
  17. case "container":
  18. if len(parts) < 2 || parts[1] == "" {
  19. return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
  20. }
  21. default:
  22. return fmt.Errorf("invalid --net: %s", hc.NetworkMode)
  23. }
  24. if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
  25. return ErrConflictNetworkHostname
  26. }
  27. if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
  28. return ErrConflictHostNetworkAndLinks
  29. }
  30. if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
  31. return ErrConflictContainerNetworkAndLinks
  32. }
  33. if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
  34. return ErrConflictNetworkAndDNS
  35. }
  36. if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 {
  37. return ErrConflictNetworkHosts
  38. }
  39. if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
  40. return ErrConflictContainerNetworkAndMac
  41. }
  42. if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
  43. return ErrConflictNetworkPublishPorts
  44. }
  45. if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
  46. return ErrConflictNetworkExposePorts
  47. }
  48. return nil
  49. }