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. if parts[0] == "container" {
  16. if len(parts) < 2 || parts[1] == "" {
  17. return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
  18. }
  19. }
  20. if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
  21. return ErrConflictNetworkHostname
  22. }
  23. if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
  24. return ErrConflictHostNetworkAndLinks
  25. }
  26. if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
  27. return ErrConflictContainerNetworkAndLinks
  28. }
  29. if hc.NetworkMode.IsUserDefined() && len(hc.Links) > 0 {
  30. return ErrConflictUserDefinedNetworkAndLinks
  31. }
  32. if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
  33. return ErrConflictNetworkAndDNS
  34. }
  35. if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 {
  36. return ErrConflictNetworkHosts
  37. }
  38. if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
  39. return ErrConflictContainerNetworkAndMac
  40. }
  41. if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
  42. return ErrConflictNetworkPublishPorts
  43. }
  44. if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
  45. return ErrConflictNetworkExposePorts
  46. }
  47. return nil
  48. }