Bläddra i källkod

inherit anoymous volumes

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 år sedan
förälder
incheckning
444fc26a51
2 ändrade filer med 30 tillägg och 2 borttagningar
  1. 12 0
      local/e2e/compose/volumes_test.go
  2. 18 2
      pkg/compose/create.go

+ 12 - 0
local/e2e/compose/volumes_test.go

@@ -71,6 +71,18 @@ func TestLocalComposeVolume(t *testing.T) {
 		assert.Assert(t, strings.Contains(output, `"Destination":"/usr/share/nginx/html"`))
 	})
 
+	t.Run("should inherit anonymous volumes", func(t *testing.T) {
+		c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "touch", "/usr/src/app/node_modules/test")
+		c.RunDockerOrExitError("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "-d")
+		c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "ls", "/usr/src/app/node_modules/test")
+	})
+
+	t.Run("should renew anonymous volumes", func(t *testing.T) {
+		c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "touch", "/usr/src/app/node_modules/test")
+		c.RunDockerOrExitError("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "--renew-anon-volumes", "-d")
+		c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "ls", "/usr/src/app/node_modules/test")
+	})
+
 	t.Run("cleanup volume project", func(t *testing.T) {
 		c.RunDockerCmd("compose", "--project-name", projectName, "down", "--volumes")
 		res := c.RunDockerCmd("volume", "ls")

+ 18 - 2
pkg/compose/create.go

@@ -629,7 +629,6 @@ MOUNTS:
 func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
 	var mounts = map[string]mount.Mount{}
 	if inherit != nil {
-
 		for _, m := range inherit.Mounts {
 			if m.Type == "tmpfs" {
 				continue
@@ -651,7 +650,24 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
 					}
 				}
 			}
-
+			for i, v := range s.Volumes {
+				if v.Target != m.Destination {
+					continue
+				}
+				if v.Source == "" {
+					// inherit previous container's anonymous volume
+					mounts[m.Destination] = mount.Mount{
+						Type:     m.Type,
+						Source:   src,
+						Target:   m.Destination,
+						ReadOnly: !m.RW,
+					}
+					// Avoid mount to be later re-defined
+					l := len(s.Volumes) - 1
+					s.Volumes[i] = s.Volumes[l]
+					s.Volumes = s.Volumes[:l]
+				}
+			}
 		}
 	}