parse_unix.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. func ValidateIsolationLevel(hc *HostConfig) error {
  53. // We may not be passed a host config, such as in the case of docker commit
  54. if hc == nil {
  55. return nil
  56. }
  57. if !hc.Isolation.IsValid() {
  58. return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
  59. }
  60. return nil
  61. }