parse_unix.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build !windows
  2. package runconfig
  3. import (
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. )
  8. // ValidateNetMode ensures that the various combinations of requested
  9. // network settings are valid.
  10. func ValidateNetMode(c *Config, hc *HostConfig) error {
  11. // We may not be passed a host config, such as in the case of docker commit
  12. if hc == nil {
  13. return nil
  14. }
  15. parts := strings.Split(string(hc.NetworkMode), ":")
  16. if parts[0] == "container" {
  17. if len(parts) < 2 || parts[1] == "" {
  18. return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
  19. }
  20. }
  21. if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
  22. return ErrConflictNetworkHostname
  23. }
  24. if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
  25. return ErrConflictHostNetworkAndLinks
  26. }
  27. if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
  28. return ErrConflictContainerNetworkAndLinks
  29. }
  30. if hc.NetworkMode.IsUserDefined() && len(hc.Links) > 0 {
  31. return ErrConflictUserDefinedNetworkAndLinks
  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. }
  50. // ValidateIsolationLevel performs platform specific validation of the
  51. // isolation level in the hostconfig structure. Linux only supports "default"
  52. // which is LXC container isolation
  53. func ValidateIsolationLevel(hc *HostConfig) error {
  54. // We may not be passed a host config, such as in the case of docker commit
  55. if hc == nil {
  56. return nil
  57. }
  58. if !hc.Isolation.IsValid() {
  59. return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
  60. }
  61. return nil
  62. }