diff --git a/daemon/config_linux.go b/daemon/config_linux.go index 17b439ebf4..dba7b0bf8e 100644 --- a/daemon/config_linux.go +++ b/daemon/config_linux.go @@ -42,8 +42,8 @@ type bridgeConfig struct { IP string FixedCIDR string FixedCIDRv6 string - DefaultGatewayIPv4 string - DefaultGatewayIPv6 string + DefaultGatewayIPv4 net.IP + DefaultGatewayIPv6 net.IP InterContainerCommunication bool } @@ -68,8 +68,8 @@ func (config *Config) InstallFlags() { flag.StringVar(&config.Bridge.Iface, []string{"b", "-bridge"}, "", "Attach containers to a network bridge") flag.StringVar(&config.Bridge.FixedCIDR, []string{"-fixed-cidr"}, "", "IPv4 subnet for fixed IPs") flag.StringVar(&config.Bridge.FixedCIDRv6, []string{"-fixed-cidr-v6"}, "", "IPv6 subnet for fixed IPs") - flag.StringVar(&config.Bridge.DefaultGatewayIPv4, []string{"-default-gateway"}, "", "Container default gateway IPv4 address") - flag.StringVar(&config.Bridge.DefaultGatewayIPv6, []string{"-default-gateway-v6"}, "", "Container default gateway IPv6 address") + opts.IPVar(&config.Bridge.DefaultGatewayIPv4, []string{"-default-gateway"}, "", "Container default gateway IPv4 address") + opts.IPVar(&config.Bridge.DefaultGatewayIPv6, []string{"-default-gateway-v6"}, "", "Container default gateway IPv6 address") flag.BoolVar(&config.Bridge.InterContainerCommunication, []string{"#icc", "-icc"}, true, "Enable inter-container communication") opts.IPVar(&config.Bridge.DefaultIP, []string{"#ip", "-ip"}, "0.0.0.0", "Default IP when binding container ports") flag.BoolVar(&config.Bridge.EnableUserlandProxy, []string{"-userland-proxy"}, true, "Use userland proxy for loopback traffic") diff --git a/daemon/daemon.go b/daemon/daemon.go index 7488934257..14ea47a6a2 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -946,6 +946,14 @@ func initNetworkController(config *Config) (libnetwork.NetworkController, error) netOption["FixedCIDRv6"] = fCIDRv6 } + if config.Bridge.DefaultGatewayIPv4 != nil { + netOption["DefaultGatewayIPv4"] = config.Bridge.DefaultGatewayIPv4 + } + + if config.Bridge.DefaultGatewayIPv6 != nil { + netOption["DefaultGatewayIPv6"] = config.Bridge.DefaultGatewayIPv6 + } + // --ip processing if config.Bridge.DefaultIP != nil { netOption["DefaultBindingIP"] = config.Bridge.DefaultIP diff --git a/integration-cli/docker_cli_daemon_test.go b/integration-cli/docker_cli_daemon_test.go index d313d73ff1..316bae9b39 100644 --- a/integration-cli/docker_cli_daemon_test.go +++ b/integration-cli/docker_cli_daemon_test.go @@ -526,6 +526,49 @@ func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) { } } +func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) { + defaultNetworkBridge := "docker0" + deleteInterface(c, defaultNetworkBridge) + + d := s.d + + bridgeIp := "192.169.1.1" + bridgeIpNet := fmt.Sprintf("%s/24", bridgeIp) + + err := d.StartWithBusybox("--bip", bridgeIpNet) + c.Assert(err, check.IsNil) + defer d.Restart() + + expectedMessage := fmt.Sprintf("default via %s dev", bridgeIp) + out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0") + c.Assert(strings.Contains(out, expectedMessage), check.Equals, true, + check.Commentf("Implicit default gateway should be bridge IP %s, but default route was '%s'", + bridgeIp, strings.TrimSpace(out))) + deleteInterface(c, defaultNetworkBridge) +} + +func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) { + defaultNetworkBridge := "docker0" + deleteInterface(c, defaultNetworkBridge) + + d := s.d + + bridgeIp := "192.169.1.1" + bridgeIpNet := fmt.Sprintf("%s/24", bridgeIp) + gatewayIp := "192.169.1.254" + + err := d.StartWithBusybox("--bip", bridgeIpNet, "--default-gateway", gatewayIp) + c.Assert(err, check.IsNil) + defer d.Restart() + + expectedMessage := fmt.Sprintf("default via %s dev", gatewayIp) + out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0") + c.Assert(strings.Contains(out, expectedMessage), check.Equals, true, + check.Commentf("Explicit default gateway should be %s, but default route was '%s'", + gatewayIp, strings.TrimSpace(out))) + deleteInterface(c, defaultNetworkBridge) +} + func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) { d := s.d