Browse Source

Setup host networking for lxc and native
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby 11 years ago
parent
commit
a785882b29

+ 2 - 0
daemon/container.go

@@ -341,6 +341,8 @@ func populateCommand(c *Container, env []string) error {
 	parts := strings.SplitN(c.hostConfig.NetworkMode, ":", 2)
 	switch parts[0] {
 	case "none":
+	case "host":
+		en.HostNetworking = true
 	case "bridge":
 		if !c.Config.NetworkDisabled {
 			network := c.NetworkSettings

+ 4 - 3
daemon/execdriver/driver.go

@@ -89,9 +89,10 @@ type Driver interface {
 
 // Network settings of the container
 type Network struct {
-	Interface   *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
-	Mtu         int               `json:"mtu"`
-	ContainerID string            `json:"container_id"` // id of the container to join network.
+	Interface      *NetworkInterface `json:"interface"` // if interface is nil then networking is disabled
+	Mtu            int               `json:"mtu"`
+	ContainerID    string            `json:"container_id"` // id of the container to join network.
+	HostNetworking bool              `json:"host_networking"`
 }
 
 type NetworkInterface struct {

+ 5 - 4
daemon/execdriver/lxc/init.go

@@ -3,15 +3,16 @@ package lxc
 import (
 	"encoding/json"
 	"fmt"
-	"github.com/dotcloud/docker/daemon/execdriver"
-	"github.com/dotcloud/docker/pkg/netlink"
-	"github.com/dotcloud/docker/pkg/user"
-	"github.com/syndtr/gocapability/capability"
 	"io/ioutil"
 	"net"
 	"os"
 	"strings"
 	"syscall"
+
+	"github.com/dotcloud/docker/daemon/execdriver"
+	"github.com/dotcloud/docker/pkg/netlink"
+	"github.com/dotcloud/docker/pkg/user"
+	"github.com/syndtr/gocapability/capability"
 )
 
 // Clear environment pollution introduced by lxc-start

+ 3 - 2
daemon/execdriver/lxc/lxc_template.go

@@ -14,12 +14,13 @@ const LxcTemplate = `
 lxc.network.type = veth
 lxc.network.link = {{.Network.Interface.Bridge}}
 lxc.network.name = eth0
-{{else}}
+lxc.network.mtu = {{.Network.Mtu}}
+{{else if not .Network.HostNetworking}}
 # network is disabled (-n=false)
 lxc.network.type = empty
 lxc.network.flags = up
-{{end}}
 lxc.network.mtu = {{.Network.Mtu}}
+{{end}}
 
 # root filesystem
 {{$ROOTFS := .Rootfs}}

+ 4 - 1
daemon/execdriver/native/create.go

@@ -53,6 +53,10 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Container
 }
 
 func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.Command) error {
+	if c.Network.HostNetworking {
+		container.Namespaces.Get("NEWNET").Enabled = false
+		return nil
+	}
 	container.Networks = []*libcontainer.Network{
 		{
 			Mtu:     c.Network.Mtu,
@@ -90,7 +94,6 @@ func (d *driver) createNetwork(container *libcontainer.Container, c *execdriver.
 			},
 		})
 	}
-
 	return nil
 }
 

+ 2 - 0
runconfig/parse.go

@@ -292,6 +292,8 @@ func parseNetMode(netMode string) (string, error) {
 			return "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
 		}
 		return netMode, nil
+	case "host":
+		return netMode, nil
 	default:
 		return "", fmt.Errorf("invalid netmode: %q", netMode)
 	}