inherit anoymous volumes

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-06-22 14:37:52 +02:00
parent 6bfdfa8947
commit 444fc26a51
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
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]
}
}
}
}