Browse code

Add -bip flag: allow specification of dynamic bridge IP via CIDR

e.g.:

```
docker -d -bip "10.10.0.1/16"
```

If set and valid, use provided in place of trial and error from pre-defined array in network.go.
Mutually exclusive of -b option.

WarheadsSE authored on 2013/12/14 00:47:19
Showing 4 changed files
... ...
@@ -14,6 +14,7 @@ type DaemonConfig struct {
14 14
 	Dns                         []string
15 15
 	EnableIptables              bool
16 16
 	BridgeIface                 string
17
+	BridgeIp                    string
17 18
 	DefaultIp                   net.IP
18 19
 	InterContainerCommunication bool
19 20
 	GraphDriver                 string
... ...
@@ -36,6 +37,7 @@ func ConfigFromJob(job *engine.Job) *DaemonConfig {
36 36
 	} else {
37 37
 		config.BridgeIface = DefaultNetworkBridge
38 38
 	}
39
+	config.BridgeIp = job.Getenv("BridgeIp")
39 40
 	config.DefaultIp = net.ParseIP(job.Getenv("DefaultIp"))
40 41
 	config.InterContainerCommunication = job.GetenvBool("InterContainerCommunication")
41 42
 	config.GraphDriver = job.Getenv("GraphDriver")
... ...
@@ -30,6 +30,7 @@ func main() {
30 30
 		flDebug              = flag.Bool("D", false, "Enable debug mode")
31 31
 		flAutoRestart        = flag.Bool("r", true, "Restart previously running containers")
32 32
 		bridgeName           = flag.String("b", "", "Attach containers to a pre-existing network bridge; use 'none' to disable container networking")
33
+		bridgeIp             = flag.String("bip", "", "Use this CIDR notation address for the network bridge's IP, not compatible with -b")
33 34
 		pidfile              = flag.String("p", "/var/run/docker.pid", "Path to use for daemon PID file")
34 35
 		flRoot               = flag.String("g", "/var/lib/docker", "Path to use as the root of the docker runtime")
35 36
 		flEnableCors         = flag.Bool("api-enable-cors", false, "Enable CORS headers in the remote API")
... ...
@@ -54,6 +55,10 @@ func main() {
54 54
 		flHosts.Set(fmt.Sprintf("unix://%s", docker.DEFAULTUNIXSOCKET))
55 55
 	}
56 56
 
57
+	if *bridgeName != "" && *bridgeIp != "" {
58
+		log.Fatal("You specified -b & -bip, mutually exclusive options. Please specify only one.")
59
+	}
60
+
57 61
 	if *flDebug {
58 62
 		os.Setenv("DEBUG", "1")
59 63
 	}
... ...
@@ -77,6 +82,7 @@ func main() {
77 77
 		job.SetenvList("Dns", flDns.GetAll())
78 78
 		job.SetenvBool("EnableIptables", *flEnableIptables)
79 79
 		job.Setenv("BridgeIface", *bridgeName)
80
+		job.Setenv("BridgeIp", *bridgeIp)
80 81
 		job.Setenv("DefaultIp", *flDefaultIp)
81 82
 		job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
82 83
 		job.Setenv("GraphDriver", *flGraphDriver)
... ...
@@ -30,6 +30,7 @@ To list available commands, either run ``docker`` with no parameters or execute
30 30
       -H=[unix:///var/run/docker.sock]: Multiple tcp://host:port or unix://path/to/socket to bind in daemon mode, single connection otherwise
31 31
       -api-enable-cors=false: Enable CORS headers in the remote API
32 32
       -b="": Attach containers to a pre-existing network bridge; use 'none' to disable container networking
33
+      -bip="": Use the provided CIDR notation address for the dynamically created bridge (docker0); Mutually exclusive of -b
33 34
       -d=false: Enable daemon mode
34 35
       -dns="": Force docker to use specific DNS servers
35 36
       -g="/var/lib/docker": Path to use as the root of the docker runtime
... ...
@@ -118,6 +118,7 @@ func CreateBridgeIface(config *DaemonConfig) error {
118 118
 		"192.168.44.1/24",
119 119
 	}
120 120
 
121
+
121 122
 	nameservers := []string{}
122 123
 	resolvConf, _ := utils.GetResolvConf()
123 124
 	// we don't check for an error here, because we don't really care
... ...
@@ -129,22 +130,30 @@ func CreateBridgeIface(config *DaemonConfig) error {
129 129
 	}
130 130
 
131 131
 	var ifaceAddr string
132
-	for _, addr := range addrs {
133
-		_, dockerNetwork, err := net.ParseCIDR(addr)
134
-		if err != nil {
135
-			return err
136
-		}
137
-		routes, err := netlink.NetworkGetRoutes()
132
+	if len(config.BridgeIp) != 0 {
133
+		_, _, err := net.ParseCIDR(config.BridgeIp)
138 134
 		if err != nil {
139 135
 			return err
140 136
 		}
141
-		if err := checkRouteOverlaps(routes, dockerNetwork); err == nil {
142
-			if err := checkNameserverOverlaps(nameservers, dockerNetwork); err == nil {
143
-				ifaceAddr = addr
144
-				break
137
+		ifaceAddr = config.BridgeIp
138
+	} else {
139
+		for _, addr := range addrs {
140
+			_, dockerNetwork, err := net.ParseCIDR(addr)
141
+			if err != nil {
142
+				return err
143
+			}
144
+			routes, err := netlink.NetworkGetRoutes()
145
+			if err != nil {
146
+				return err
147
+			}
148
+			if err := checkRouteOverlaps(routes, dockerNetwork); err == nil {
149
+				if err := checkNameserverOverlaps(nameservers, dockerNetwork); err == nil {
150
+					ifaceAddr = addr
151
+					break
152
+				}
153
+			} else {
154
+				utils.Debugf("%s: %s", addr, err)
145 155
 			}
146
-		} else {
147
-			utils.Debugf("%s: %s", addr, err)
148 156
 		}
149 157
 	}
150 158
 	if ifaceAddr == "" {