瀏覽代碼

Support completely disabling network configuration with docker -d -b none

Stefan Praszalowicz 12 年之前
父節點
當前提交
49673fc45c
共有 2 個文件被更改,包括 33 次插入2 次删除
  1. 6 2
      container.go
  2. 27 0
      network.go

+ 6 - 2
container.go

@@ -514,8 +514,12 @@ func (container *Container) Start(hostConfig *HostConfig) error {
 	if err := container.EnsureMounted(); err != nil {
 	if err := container.EnsureMounted(); err != nil {
 		return err
 		return err
 	}
 	}
-	if err := container.allocateNetwork(); err != nil {
-		return err
+	if container.runtime.networkManager.disabled {
+		container.Config.NetworkEnabled = false
+	} else {
+		if err := container.allocateNetwork(); err != nil {
+			return err
+		}
 	}
 	}
 
 
 	// Make sure the config is compatible with the current kernel
 	// Make sure the config is compatible with the current kernel

+ 27 - 0
network.go

@@ -17,6 +17,7 @@ var NetworkBridgeIface string
 
 
 const (
 const (
 	DefaultNetworkBridge = "docker0"
 	DefaultNetworkBridge = "docker0"
+	DisableNetworkBridge = "none"
 	portRangeStart       = 49153
 	portRangeStart       = 49153
 	portRangeEnd         = 65535
 	portRangeEnd         = 65535
 )
 )
@@ -453,10 +454,16 @@ type NetworkInterface struct {
 
 
 	manager  *NetworkManager
 	manager  *NetworkManager
 	extPorts []*Nat
 	extPorts []*Nat
+	disabled bool
 }
 }
 
 
 // Allocate an external TCP port and map it to the interface
 // Allocate an external TCP port and map it to the interface
 func (iface *NetworkInterface) AllocatePort(spec string) (*Nat, error) {
 func (iface *NetworkInterface) AllocatePort(spec string) (*Nat, error) {
+
+	if iface.disabled {
+		return nil, fmt.Errorf("Trying to allocate port for interface %v, which is disabled", iface) // FIXME
+	}
+
 	nat, err := parseNat(spec)
 	nat, err := parseNat(spec)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
@@ -552,6 +559,11 @@ func parseNat(spec string) (*Nat, error) {
 
 
 // Release: Network cleanup - release all resources
 // Release: Network cleanup - release all resources
 func (iface *NetworkInterface) Release() {
 func (iface *NetworkInterface) Release() {
+
+	if iface.disabled {
+		return
+	}
+
 	for _, nat := range iface.extPorts {
 	for _, nat := range iface.extPorts {
 		utils.Debugf("Unmaping %v/%v", nat.Proto, nat.Frontend)
 		utils.Debugf("Unmaping %v/%v", nat.Proto, nat.Frontend)
 		if err := iface.manager.portMapper.Unmap(nat.Frontend, nat.Proto); err != nil {
 		if err := iface.manager.portMapper.Unmap(nat.Frontend, nat.Proto); err != nil {
@@ -579,10 +591,17 @@ type NetworkManager struct {
 	tcpPortAllocator *PortAllocator
 	tcpPortAllocator *PortAllocator
 	udpPortAllocator *PortAllocator
 	udpPortAllocator *PortAllocator
 	portMapper       *PortMapper
 	portMapper       *PortMapper
+
+	disabled bool
 }
 }
 
 
 // Allocate a network interface
 // Allocate a network interface
 func (manager *NetworkManager) Allocate() (*NetworkInterface, error) {
 func (manager *NetworkManager) Allocate() (*NetworkInterface, error) {
+
+	if manager.disabled {
+		return &NetworkInterface{disabled: true}, nil
+	}
+
 	ip, err := manager.ipAllocator.Acquire()
 	ip, err := manager.ipAllocator.Acquire()
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
@@ -596,6 +615,14 @@ func (manager *NetworkManager) Allocate() (*NetworkInterface, error) {
 }
 }
 
 
 func newNetworkManager(bridgeIface string) (*NetworkManager, error) {
 func newNetworkManager(bridgeIface string) (*NetworkManager, error) {
+
+	if bridgeIface == DisableNetworkBridge {
+		manager := &NetworkManager{
+			disabled: true,
+		}
+		return manager, nil
+	}
+
 	addr, err := getIfaceAddr(bridgeIface)
 	addr, err := getIfaceAddr(bridgeIface)
 	if err != nil {
 	if err != nil {
 		// If the iface is not found, try to create it
 		// If the iface is not found, try to create it