Browse code

Merge pull request #10615 from coolljt0725/fix_mount

Fix create volume /etc cover /etc/{hosts,resolv.conf,hostname} Fixes # 10604

Michael Crosby authored on 2015/02/17 03:53:26
Showing 2 changed files
... ...
@@ -316,8 +316,23 @@ func validMountMode(mode string) bool {
316 316
 }
317 317
 
318 318
 func (container *Container) setupMounts() error {
319
-	mounts := []execdriver.Mount{
320
-		{Source: container.ResolvConfPath, Destination: "/etc/resolv.conf", Writable: true, Private: true},
319
+	mounts := []execdriver.Mount{}
320
+
321
+	// Mount user specified volumes
322
+	// Note, these are not private because you may want propagation of (un)mounts from host
323
+	// volumes. For instance if you use -v /usr:/usr and the host later mounts /usr/share you
324
+	// want this new mount in the container
325
+	// These mounts must be ordered based on the length of the path that it is being mounted to (lexicographic)
326
+	for _, path := range container.sortedVolumeMounts() {
327
+		mounts = append(mounts, execdriver.Mount{
328
+			Source:      container.Volumes[path],
329
+			Destination: path,
330
+			Writable:    container.VolumesRW[path],
331
+		})
332
+	}
333
+
334
+	if container.ResolvConfPath != "" {
335
+		mounts = append(mounts, execdriver.Mount{Source: container.ResolvConfPath, Destination: "/etc/resolv.conf", Writable: true, Private: true})
321 336
 	}
322 337
 
323 338
 	if container.HostnamePath != "" {
... ...
@@ -334,19 +349,6 @@ func (container *Container) setupMounts() error {
334 334
 		}
335 335
 	}
336 336
 
337
-	// Mount user specified volumes
338
-	// Note, these are not private because you may want propagation of (un)mounts from host
339
-	// volumes. For instance if you use -v /usr:/usr and the host later mounts /usr/share you
340
-	// want this new mount in the container
341
-	// These mounts must be ordered based on the length of the path that it is being mounted to (lexicographic)
342
-	for _, path := range container.sortedVolumeMounts() {
343
-		mounts = append(mounts, execdriver.Mount{
344
-			Source:      container.Volumes[path],
345
-			Destination: path,
346
-			Writable:    container.VolumesRW[path],
347
-		})
348
-	}
349
-
350 337
 	container.command.Mounts = mounts
351 338
 	return nil
352 339
 }
... ...
@@ -2491,6 +2491,41 @@ func TestRunReuseBindVolumeThatIsSymlink(t *testing.T) {
2491 2491
 	logDone("run - can remount old bindmount volume")
2492 2492
 }
2493 2493
 
2494
+//test create /etc volume
2495
+func TestRunCreateVolumeEtc(t *testing.T) {
2496
+	cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "-v", "/etc", "busybox", "cat", "/etc/resolv.conf")
2497
+	out, _, err := runCommandWithOutput(cmd)
2498
+	if err != nil {
2499
+		t.Fatal("failed to run container: %v, output: %q", err, out)
2500
+	}
2501
+	if !strings.Contains(out, "nameserver 127.0.0.1") {
2502
+		t.Fatal("failed: create /etc volume cover /etc/resolv.conf")
2503
+	}
2504
+
2505
+	cmd = exec.Command(dockerBinary, "run", "-h=test123", "-v", "/etc", "busybox", "cat", "/etc/hostname")
2506
+	out, _, err = runCommandWithOutput(cmd)
2507
+	if err != nil {
2508
+		t.Fatal("failed to run container: %v, output: %q", err, out)
2509
+	}
2510
+	if !strings.Contains(out, "test123") {
2511
+		t.Fatal("failed: create /etc volume cover /etc/hostname")
2512
+	}
2513
+
2514
+	cmd = exec.Command(dockerBinary, "run", "--add-host=test:192.168.0.1", "-v", "/etc", "busybox", "cat", "/etc/hosts")
2515
+	out, _, err = runCommandWithOutput(cmd)
2516
+	if err != nil {
2517
+		t.Fatal("failed to run container: %v, output: %q", err, out)
2518
+	}
2519
+	out = strings.Replace(out, "\n", " ", -1)
2520
+	if !strings.Contains(out, "192.168.0.1"+"\t"+"test") || !strings.Contains(out, "127.0.0.1"+"\t"+"localhost") {
2521
+		t.Fatal("failed: create /etc volume cover /etc/hosts", out)
2522
+	}
2523
+
2524
+	deleteAllContainers()
2525
+
2526
+	logDone("run - create /etc volume success")
2527
+}
2528
+
2494 2529
 func TestVolumesNoCopyData(t *testing.T) {
2495 2530
 	defer deleteImages("dataimage")
2496 2531
 	defer deleteAllContainers()