github.com/ruphin/docker@v1.10.1/runconfig/hostconfig_unix.go (about)

     1  // +build !windows
     2  
     3  package runconfig
     4  
     5  import (
     6  	"fmt"
     7  	"runtime"
     8  	"strings"
     9  
    10  	"github.com/docker/engine-api/types/container"
    11  )
    12  
    13  // DefaultDaemonNetworkMode returns the default network stack the daemon should
    14  // use.
    15  func DefaultDaemonNetworkMode() container.NetworkMode {
    16  	return container.NetworkMode("bridge")
    17  }
    18  
    19  // IsPreDefinedNetwork indicates if a network is predefined by the daemon
    20  func IsPreDefinedNetwork(network string) bool {
    21  	n := container.NetworkMode(network)
    22  	return n.IsBridge() || n.IsHost() || n.IsNone() || n.IsDefault()
    23  }
    24  
    25  // ValidateNetMode ensures that the various combinations of requested
    26  // network settings are valid.
    27  func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
    28  	// We may not be passed a host config, such as in the case of docker commit
    29  	if hc == nil {
    30  		return nil
    31  	}
    32  	parts := strings.Split(string(hc.NetworkMode), ":")
    33  	if parts[0] == "container" {
    34  		if len(parts) < 2 || parts[1] == "" {
    35  			return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
    36  		}
    37  	}
    38  
    39  	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
    40  		return ErrConflictNetworkHostname
    41  	}
    42  
    43  	if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
    44  		return ErrConflictHostNetworkAndLinks
    45  	}
    46  
    47  	if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
    48  		return ErrConflictContainerNetworkAndLinks
    49  	}
    50  
    51  	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
    52  		return ErrConflictNetworkAndDNS
    53  	}
    54  
    55  	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 {
    56  		return ErrConflictNetworkHosts
    57  	}
    58  
    59  	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
    60  		return ErrConflictContainerNetworkAndMac
    61  	}
    62  
    63  	if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
    64  		return ErrConflictNetworkPublishPorts
    65  	}
    66  
    67  	if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
    68  		return ErrConflictNetworkExposePorts
    69  	}
    70  	return nil
    71  }
    72  
    73  // ValidateIsolationLevel performs platform specific validation of the
    74  // isolation level in the hostconfig structure. Linux only supports "default"
    75  // which is LXC container isolation
    76  func ValidateIsolationLevel(hc *container.HostConfig) error {
    77  	// We may not be passed a host config, such as in the case of docker commit
    78  	if hc == nil {
    79  		return nil
    80  	}
    81  	if !hc.Isolation.IsValid() {
    82  		return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
    83  	}
    84  	return nil
    85  }