From 444fc26a51eadbcfb73f6cbfea2537e001545145 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 22 Jun 2021 14:37:52 +0200 Subject: [PATCH] inherit anoymous volumes Signed-off-by: Nicolas De Loof --- local/e2e/compose/volumes_test.go | 12 ++++++++++++ pkg/compose/create.go | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/local/e2e/compose/volumes_test.go b/local/e2e/compose/volumes_test.go index 935577361..2b44878f0 100644 --- a/local/e2e/compose/volumes_test.go +++ b/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") diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 38ab41d09..efe1f4002 100644 --- a/pkg/compose/create.go +++ b/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] + } + } } }