mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge branch 'stfp-858-disable-network-configuration'
This commit is contained in:
commit
a93a87f64a
8 changed files with 134 additions and 46 deletions
1
AUTHORS
1
AUTHORS
|
@ -76,6 +76,7 @@ Shawn Siefkas <shawn.siefkas@meredith.com>
|
||||||
Silas Sewell <silas@sewell.org>
|
Silas Sewell <silas@sewell.org>
|
||||||
Solomon Hykes <solomon@dotcloud.com>
|
Solomon Hykes <solomon@dotcloud.com>
|
||||||
Sridhar Ratnakumar <sridharr@activestate.com>
|
Sridhar Ratnakumar <sridharr@activestate.com>
|
||||||
|
Stefan Praszalowicz <stefan@greplin.com>
|
||||||
Thatcher Peskens <thatcher@dotcloud.com>
|
Thatcher Peskens <thatcher@dotcloud.com>
|
||||||
Thomas Bikeev <thomas.bikeev@mac.com>
|
Thomas Bikeev <thomas.bikeev@mac.com>
|
||||||
Thomas Hansen <thomas.hansen@gmail.com>
|
Thomas Hansen <thomas.hansen@gmail.com>
|
||||||
|
|
94
container.go
94
container.go
|
@ -58,25 +58,26 @@ type Container struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Hostname string
|
Hostname string
|
||||||
User string
|
User string
|
||||||
Memory int64 // Memory limit (in bytes)
|
Memory int64 // Memory limit (in bytes)
|
||||||
MemorySwap int64 // Total memory usage (memory + swap); set `-1' to disable swap
|
MemorySwap int64 // Total memory usage (memory + swap); set `-1' to disable swap
|
||||||
CpuShares int64 // CPU shares (relative weight vs. other containers)
|
CpuShares int64 // CPU shares (relative weight vs. other containers)
|
||||||
AttachStdin bool
|
AttachStdin bool
|
||||||
AttachStdout bool
|
AttachStdout bool
|
||||||
AttachStderr bool
|
AttachStderr bool
|
||||||
PortSpecs []string
|
PortSpecs []string
|
||||||
Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
|
Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
|
||||||
OpenStdin bool // Open stdin
|
OpenStdin bool // Open stdin
|
||||||
StdinOnce bool // If true, close stdin after the 1 attached client disconnects.
|
StdinOnce bool // If true, close stdin after the 1 attached client disconnects.
|
||||||
Env []string
|
Env []string
|
||||||
Cmd []string
|
Cmd []string
|
||||||
Dns []string
|
Dns []string
|
||||||
Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
|
Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
|
||||||
Volumes map[string]struct{}
|
Volumes map[string]struct{}
|
||||||
VolumesFrom string
|
VolumesFrom string
|
||||||
Entrypoint []string
|
Entrypoint []string
|
||||||
|
NetworkDisabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type HostConfig struct {
|
type HostConfig struct {
|
||||||
|
@ -106,6 +107,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
|
||||||
flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
||||||
flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
|
flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
|
||||||
flContainerIDFile := cmd.String("cidfile", "", "Write the container ID to the file")
|
flContainerIDFile := cmd.String("cidfile", "", "Write the container ID to the file")
|
||||||
|
flNetwork := cmd.Bool("n", true, "Enable networking for this container")
|
||||||
|
|
||||||
if capabilities != nil && *flMemory > 0 && !capabilities.MemoryLimit {
|
if capabilities != nil && *flMemory > 0 && !capabilities.MemoryLimit {
|
||||||
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
|
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
|
||||||
|
@ -174,23 +176,24 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &Config{
|
config := &Config{
|
||||||
Hostname: *flHostname,
|
Hostname: *flHostname,
|
||||||
PortSpecs: flPorts,
|
PortSpecs: flPorts,
|
||||||
User: *flUser,
|
User: *flUser,
|
||||||
Tty: *flTty,
|
Tty: *flTty,
|
||||||
OpenStdin: *flStdin,
|
NetworkDisabled: !*flNetwork,
|
||||||
Memory: *flMemory,
|
OpenStdin: *flStdin,
|
||||||
CpuShares: *flCpuShares,
|
Memory: *flMemory,
|
||||||
AttachStdin: flAttach.Get("stdin"),
|
CpuShares: *flCpuShares,
|
||||||
AttachStdout: flAttach.Get("stdout"),
|
AttachStdin: flAttach.Get("stdin"),
|
||||||
AttachStderr: flAttach.Get("stderr"),
|
AttachStdout: flAttach.Get("stdout"),
|
||||||
Env: flEnv,
|
AttachStderr: flAttach.Get("stderr"),
|
||||||
Cmd: runCmd,
|
Env: flEnv,
|
||||||
Dns: flDns,
|
Cmd: runCmd,
|
||||||
Image: image,
|
Dns: flDns,
|
||||||
Volumes: flVolumes,
|
Image: image,
|
||||||
VolumesFrom: *flVolumesFrom,
|
Volumes: flVolumes,
|
||||||
Entrypoint: entrypoint,
|
VolumesFrom: *flVolumesFrom,
|
||||||
|
Entrypoint: entrypoint,
|
||||||
}
|
}
|
||||||
hostConfig := &HostConfig{
|
hostConfig := &HostConfig{
|
||||||
Binds: binds,
|
Binds: binds,
|
||||||
|
@ -511,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 {
|
if container.runtime.networkManager.disabled {
|
||||||
return err
|
container.Config.NetworkDisabled = true
|
||||||
|
} 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
|
||||||
|
@ -626,7 +633,9 @@ func (container *Container) Start(hostConfig *HostConfig) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Networking
|
// Networking
|
||||||
params = append(params, "-g", container.network.Gateway.String())
|
if !container.Config.NetworkDisabled {
|
||||||
|
params = append(params, "-g", container.network.Gateway.String())
|
||||||
|
}
|
||||||
|
|
||||||
// User
|
// User
|
||||||
if container.Config.User != "" {
|
if container.Config.User != "" {
|
||||||
|
@ -728,6 +737,10 @@ func (container *Container) StderrPipe() (io.ReadCloser, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) allocateNetwork() error {
|
func (container *Container) allocateNetwork() error {
|
||||||
|
if container.Config.NetworkDisabled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
iface, err := container.runtime.networkManager.Allocate()
|
iface, err := container.runtime.networkManager.Allocate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -754,6 +767,9 @@ func (container *Container) allocateNetwork() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) releaseNetwork() {
|
func (container *Container) releaseNetwork() {
|
||||||
|
if container.Config.NetworkDisabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
container.network.Release()
|
container.network.Release()
|
||||||
container.network = nil
|
container.network = nil
|
||||||
container.NetworkSettings = &NetworkSettings{}
|
container.NetworkSettings = &NetworkSettings{}
|
||||||
|
|
|
@ -1252,3 +1252,41 @@ func TestRestartWithVolumes(t *testing.T) {
|
||||||
t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
|
t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
|
||||||
|
runtime := mkRuntime(t)
|
||||||
|
defer nuke(runtime)
|
||||||
|
|
||||||
|
config, hc, _, err := ParseRun([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show"}, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
c, err := NewBuilder(runtime).Create(config)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
stdout, err := c.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer runtime.Destroy(c)
|
||||||
|
if err := c.Start(hc); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
c.WaitTimeout(500 * time.Millisecond)
|
||||||
|
c.Wait()
|
||||||
|
output, err := ioutil.ReadAll(stdout)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
|
||||||
|
if len(interfaces) != 1 {
|
||||||
|
t.Fatalf("Wrong interface count in test container: expected [1: lo], got [%s]", interfaces)
|
||||||
|
}
|
||||||
|
if interfaces[0] != "1: lo" {
|
||||||
|
t.Fatalf("Wrong interface in test container: expected [1: lo], got [%s]", interfaces)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ func main() {
|
||||||
flDaemon := flag.Bool("d", false, "Daemon mode")
|
flDaemon := flag.Bool("d", false, "Daemon mode")
|
||||||
flDebug := flag.Bool("D", false, "Debug mode")
|
flDebug := flag.Bool("D", false, "Debug mode")
|
||||||
flAutoRestart := flag.Bool("r", false, "Restart previously running containers")
|
flAutoRestart := flag.Bool("r", false, "Restart previously running containers")
|
||||||
bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge")
|
bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge. Use 'none' to disable container networking")
|
||||||
pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID")
|
pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID")
|
||||||
flGraphPath := flag.String("g", "/var/lib/docker", "Path to graph storage base dir.")
|
flGraphPath := flag.String("g", "/var/lib/docker", "Path to graph storage base dir.")
|
||||||
flEnableCors := flag.Bool("api-enable-cors", false, "Enable CORS requests in the remote api.")
|
flEnableCors := flag.Bool("api-enable-cors", false, "Enable CORS requests in the remote api.")
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
-h="": Container host name
|
-h="": Container host name
|
||||||
-i=false: Keep stdin open even if not attached
|
-i=false: Keep stdin open even if not attached
|
||||||
-m=0: Memory limit (in bytes)
|
-m=0: Memory limit (in bytes)
|
||||||
|
-n=true: Enable networking for this container
|
||||||
-p=[]: Map a network port to the container
|
-p=[]: Map a network port to the container
|
||||||
-t=false: Allocate a pseudo-tty
|
-t=false: Allocate a pseudo-tty
|
||||||
-u="": Username or UID
|
-u="": Username or UID
|
||||||
|
|
|
@ -13,6 +13,10 @@ lxc.utsname = {{.Id}}
|
||||||
{{end}}
|
{{end}}
|
||||||
#lxc.aa_profile = unconfined
|
#lxc.aa_profile = unconfined
|
||||||
|
|
||||||
|
{{if .Config.NetworkDisabled}}
|
||||||
|
# network is disabled (-n=false)
|
||||||
|
lxc.network.type = empty
|
||||||
|
{{else}}
|
||||||
# network configuration
|
# network configuration
|
||||||
lxc.network.type = veth
|
lxc.network.type = veth
|
||||||
lxc.network.flags = up
|
lxc.network.flags = up
|
||||||
|
@ -20,6 +24,7 @@ lxc.network.link = {{.NetworkSettings.Bridge}}
|
||||||
lxc.network.name = eth0
|
lxc.network.name = eth0
|
||||||
lxc.network.mtu = 1500
|
lxc.network.mtu = 1500
|
||||||
lxc.network.ipv4 = {{.NetworkSettings.IPAddress}}/{{.NetworkSettings.IPPrefixLen}}
|
lxc.network.ipv4 = {{.NetworkSettings.IPAddress}}/{{.NetworkSettings.IPPrefixLen}}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
# root filesystem
|
# root filesystem
|
||||||
{{$ROOTFS := .RootfsPath}}
|
{{$ROOTFS := .RootfsPath}}
|
||||||
|
|
27
network.go
27
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
|
||||||
)
|
)
|
||||||
|
@ -472,10 +473,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
|
||||||
|
@ -571,6 +578,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 {
|
||||||
|
@ -598,10 +610,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
|
||||||
|
@ -615,6 +634,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
|
||||||
|
|
|
@ -17,12 +17,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
unitTestImageName = "docker-test-image"
|
unitTestImageName = "docker-test-image"
|
||||||
unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
|
unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
|
||||||
unitTestNetworkBridge = "testdockbr0"
|
unitTestNetworkBridge = "testdockbr0"
|
||||||
unitTestStoreBase = "/var/lib/docker/unit-tests"
|
unitTestStoreBase = "/var/lib/docker/unit-tests"
|
||||||
testDaemonAddr = "127.0.0.1:4270"
|
testDaemonAddr = "127.0.0.1:4270"
|
||||||
testDaemonProto = "tcp"
|
testDaemonProto = "tcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalRuntime *Runtime
|
var globalRuntime *Runtime
|
||||||
|
|
Loading…
Reference in a new issue