Merge pull request #1831 from ndeloof/inherit_volumes

inherit anoymous volumes
This commit is contained in:
Nicolas De loof 2021-06-23 15:35:18 +02:00 committed by GitHub
commit 8d2019e77d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -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")

View File

@ -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]
}
}
}
}