Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 8d2019e

Browse files
authored
Merge pull request #1831 from ndeloof/inherit_volumes
inherit anoymous volumes
2 parents 99c39c8 + 444fc26 commit 8d2019e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

local/e2e/compose/volumes_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ func TestLocalComposeVolume(t *testing.T) {
7171
assert.Assert(t, strings.Contains(output, `"Destination":"/usr/share/nginx/html"`))
7272
})
7373

74+
t.Run("should inherit anonymous volumes", func(t *testing.T) {
75+
c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "touch", "/usr/src/app/node_modules/test")
76+
c.RunDockerOrExitError("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "-d")
77+
c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "ls", "/usr/src/app/node_modules/test")
78+
})
79+
80+
t.Run("should renew anonymous volumes", func(t *testing.T) {
81+
c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "touch", "/usr/src/app/node_modules/test")
82+
c.RunDockerOrExitError("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "--renew-anon-volumes", "-d")
83+
c.RunDockerOrExitError("exec", "compose-e2e-volume_nginx2_1", "ls", "/usr/src/app/node_modules/test")
84+
})
85+
7486
t.Run("cleanup volume project", func(t *testing.T) {
7587
c.RunDockerCmd("compose", "--project-name", projectName, "down", "--volumes")
7688
res := c.RunDockerCmd("volume", "ls")

pkg/compose/create.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ MOUNTS:
629629
func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
630630
var mounts = map[string]mount.Mount{}
631631
if inherit != nil {
632-
633632
for _, m := range inherit.Mounts {
634633
if m.Type == "tmpfs" {
635634
continue
@@ -651,7 +650,24 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
651650
}
652651
}
653652
}
654-
653+
for i, v := range s.Volumes {
654+
if v.Target != m.Destination {
655+
continue
656+
}
657+
if v.Source == "" {
658+
// inherit previous container's anonymous volume
659+
mounts[m.Destination] = mount.Mount{
660+
Type: m.Type,
661+
Source: src,
662+
Target: m.Destination,
663+
ReadOnly: !m.RW,
664+
}
665+
// Avoid mount to be later re-defined
666+
l := len(s.Volumes) - 1
667+
s.Volumes[i] = s.Volumes[l]
668+
s.Volumes = s.Volumes[:l]
669+
}
670+
}
655671
}
656672
}
657673

0 commit comments

Comments
 (0)