Browse code

Update host networking with hostname and files Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/05/03 06:45:39
Showing 2 changed files
... ...
@@ -343,7 +343,7 @@ func populateCommand(c *Container, env []string) error {
343 343
 	case "none":
344 344
 	case "host":
345 345
 		en.HostNetworking = true
346
-	case "bridge":
346
+	case "bridge", "": // empty string to support existing containers
347 347
 		if !c.Config.NetworkDisabled {
348 348
 			network := c.NetworkSettings
349 349
 			en.Interface = &execdriver.NetworkInterface{
... ...
@@ -503,9 +503,18 @@ func (container *Container) StderrLogPipe() io.ReadCloser {
503 503
 	return utils.NewBufReader(reader)
504 504
 }
505 505
 
506
-func (container *Container) buildHostnameAndHostsFiles(IP string) {
506
+func (container *Container) buildHostname() {
507 507
 	container.HostnamePath = path.Join(container.root, "hostname")
508
-	ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
508
+
509
+	if container.Config.Domainname != "" {
510
+		ioutil.WriteFile(container.HostnamePath, []byte(fmt.Sprintf("%s.%s\n", container.Config.Hostname, container.Config.Domainname)), 0644)
511
+	} else {
512
+		ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
513
+	}
514
+}
515
+
516
+func (container *Container) buildHostnameAndHostsFiles(IP string) {
517
+	container.buildHostname()
509 518
 
510 519
 	hostsContent := []byte(`
511 520
 127.0.0.1	localhost
... ...
@@ -523,12 +532,11 @@ ff02::2		ip6-allrouters
523 523
 	} else if !container.Config.NetworkDisabled {
524 524
 		hostsContent = append([]byte(fmt.Sprintf("%s\t%s\n", IP, container.Config.Hostname)), hostsContent...)
525 525
 	}
526
-
527 526
 	ioutil.WriteFile(container.HostsPath, hostsContent, 0644)
528 527
 }
529 528
 
530 529
 func (container *Container) allocateNetwork() error {
531
-	if container.Config.NetworkDisabled {
530
+	if container.Config.NetworkDisabled || container.hostConfig.NetworkMode == "host" {
532 531
 		return nil
533 532
 	}
534 533
 
... ...
@@ -981,14 +989,22 @@ func (container *Container) setupContainerDns() error {
981 981
 	if container.ResolvConfPath != "" {
982 982
 		return nil
983 983
 	}
984
+
984 985
 	var (
985 986
 		config = container.hostConfig
986 987
 		daemon = container.daemon
987 988
 	)
989
+
990
+	if config.NetworkMode == "host" {
991
+		container.ResolvConfPath = "/etc/resolv.conf"
992
+		return nil
993
+	}
994
+
988 995
 	resolvConf, err := utils.GetResolvConf()
989 996
 	if err != nil {
990 997
 		return err
991 998
 	}
999
+
992 1000
 	// If custom dns exists, then create a resolv.conf for the container
993 1001
 	if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
994 1002
 		var (
... ...
@@ -1028,7 +1044,22 @@ func (container *Container) setupContainerDns() error {
1028 1028
 }
1029 1029
 
1030 1030
 func (container *Container) initializeNetworking() error {
1031
-	if container.daemon.config.DisableNetwork {
1031
+	var err error
1032
+	if container.hostConfig.NetworkMode == "host" {
1033
+		container.Config.Hostname, err = os.Hostname()
1034
+		if err != nil {
1035
+			return err
1036
+		}
1037
+
1038
+		parts := strings.SplitN(container.Config.Hostname, ".", 2)
1039
+		if len(parts) > 1 {
1040
+			container.Config.Hostname = parts[0]
1041
+			container.Config.Domainname = parts[1]
1042
+		}
1043
+		container.HostsPath = "/etc/hosts"
1044
+
1045
+		container.buildHostname()
1046
+	} else if container.daemon.config.DisableNetwork {
1032 1047
 		container.Config.NetworkDisabled = true
1033 1048
 		container.buildHostnameAndHostsFiles("127.0.1.1")
1034 1049
 	} else {
... ...
@@ -22,33 +22,3 @@ func TestParseLxcConfOpt(t *testing.T) {
22 22
 		}
23 23
 	}
24 24
 }
25
-
26
-func TestParseNetMode(t *testing.T) {
27
-	testFlags := []struct {
28
-		flag      string
29
-		mode      string
30
-		container string
31
-		err       bool
32
-	}{
33
-		{"", "", "", true},
34
-		{"bridge", "bridge", "", false},
35
-		{"disable", "disable", "", false},
36
-		{"container:foo", "container", "foo", false},
37
-		{"container:", "", "", true},
38
-		{"container", "", "", true},
39
-		{"unknown", "", "", true},
40
-	}
41
-
42
-	for _, to := range testFlags {
43
-		mode, err := parseNetMode(to.flag)
44
-		if mode != to.mode {
45
-			t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode)
46
-		}
47
-		if container != to.container {
48
-			t.Fatalf("-net %s: expected net container: %q, got: %q", to.flag, to.container, container)
49
-		}
50
-		if (err != nil) != to.err {
51
-			t.Fatal("-net %s: expected an error got none", to.flag)
52
-		}
53
-	}
54
-}