Bladeren bron

Merge pull request #8475 from cpuguy83/fix_create_phantom_volumes_on_container_restart

Fixes re-creating volume on (re)start
Andrea Luzzardi 10 jaren geleden
bovenliggende
commit
d7bcc099be
4 gewijzigde bestanden met toevoegingen van 54 en 9 verwijderingen
  1. 5 0
      daemon/volumes.go
  2. 37 0
      integration-cli/docker_cli_run_test.go
  3. 11 8
      integration-cli/docker_test_vars.go
  4. 1 1
      integration-cli/docker_utils.go

+ 5 - 0
daemon/volumes.go

@@ -137,6 +137,11 @@ func (container *Container) parseVolumeMountConfig() (map[string]*Mount, error)
 			continue
 		}
 
+		// Check if this has already been created
+		if _, exists := container.Volumes[path]; exists {
+			continue
+		}
+
 		vol, err := container.daemon.volumes.FindOrCreateVolume("", true)
 		if err != nil {
 			return nil, err

+ 37 - 0
integration-cli/docker_cli_run_test.go

@@ -2339,3 +2339,40 @@ func TestVolumesNoCopyData(t *testing.T) {
 
 	logDone("run - volumes do not copy data for volumes-from and bindmounts")
 }
+
+func TestRunVolumesNotRecreatedOnStart(t *testing.T) {
+	// Clear out any remnants from other tests
+	deleteAllContainers()
+	info, err := ioutil.ReadDir(volumesConfigPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if len(info) > 0 {
+		for _, f := range info {
+			if err := os.RemoveAll(volumesConfigPath + "/" + f.Name()); err != nil {
+				t.Fatal(err)
+			}
+		}
+	}
+
+	defer deleteAllContainers()
+	cmd := exec.Command(dockerBinary, "run", "-v", "/foo", "--name", "lone_starr", "busybox")
+	if _, err := runCommand(cmd); err != nil {
+		t.Fatal(err)
+	}
+
+	cmd = exec.Command(dockerBinary, "start", "lone_starr")
+	if _, err := runCommand(cmd); err != nil {
+		t.Fatal(err)
+	}
+
+	info, err = ioutil.ReadDir(volumesConfigPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if len(info) != 1 {
+		t.Fatalf("Expected only 1 volume have %v", len(info))
+	}
+
+	logDone("run - volumes not recreated on start")
+}

+ 11 - 8
integration-cli/docker_test_vars.go

@@ -6,18 +6,21 @@ import (
 	"os/exec"
 )
 
-// the docker binary to use
-var dockerBinary = "docker"
+var (
+	// the docker binary to use
+	dockerBinary = "docker"
 
-// the private registry image to use for tests involving the registry
-var registryImageName = "registry"
+	// the private registry image to use for tests involving the registry
+	registryImageName = "registry"
 
-// the private registry to use for tests
-var privateRegistryURL = "127.0.0.1:5000"
+	// the private registry to use for tests
+	privateRegistryURL = "127.0.0.1:5000"
 
-var execDriverPath = "/var/lib/docker/execdriver/native"
+	execDriverPath    = "/var/lib/docker/execdriver/native"
+	volumesConfigPath = "/var/lib/docker/volumes"
 
-var workingDirectory string
+	workingDirectory string
+)
 
 func init() {
 	if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {

+ 1 - 1
integration-cli/docker_utils.go

@@ -267,7 +267,7 @@ func deleteContainer(container string) error {
 	killSplitArgs := strings.Split(killArgs, " ")
 	killCmd := exec.Command(dockerBinary, killSplitArgs...)
 	runCommand(killCmd)
-	rmArgs := fmt.Sprintf("rm %v", container)
+	rmArgs := fmt.Sprintf("rm -v %v", container)
 	rmSplitArgs := strings.Split(rmArgs, " ")
 	rmCmd := exec.Command(dockerBinary, rmSplitArgs...)
 	exitCode, err := runCommand(rmCmd)