mirror of
https://github.com/docker/compose.git
synced 2025-07-26 15:14:04 +02:00
Fix index out of range on compose.buildContainerMountOptions
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
0062703bea
commit
ef786f9245
@ -750,24 +750,21 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i, v := range s.Volumes {
|
volumes := []types.ServiceVolumeConfig{}
|
||||||
if v.Target != m.Destination {
|
for _, v := range s.Volumes {
|
||||||
|
if v.Target != m.Destination || v.Source != "" {
|
||||||
|
volumes = append(volumes, v)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if v.Source == "" {
|
// inherit previous container's anonymous volume
|
||||||
// inherit previous container's anonymous volume
|
mounts[m.Destination] = mount.Mount{
|
||||||
mounts[m.Destination] = mount.Mount{
|
Type: m.Type,
|
||||||
Type: m.Type,
|
Source: src,
|
||||||
Source: src,
|
Target: m.Destination,
|
||||||
Target: m.Destination,
|
ReadOnly: !m.RW,
|
||||||
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]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s.Volumes = volumes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,10 +21,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
composetypes "github.com/compose-spec/compose-go/types"
|
composetypes "github.com/compose-spec/compose-go/types"
|
||||||
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
|
moby "github.com/docker/docker/api/types"
|
||||||
mountTypes "github.com/docker/docker/api/types/mount"
|
mountTypes "github.com/docker/docker/api/types/mount"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
@ -81,3 +81,57 @@ func TestPrepareNetworkLabels(t *testing.T) {
|
|||||||
"com.docker.compose.version": api.ComposeVersion,
|
"com.docker.compose.version": api.ComposeVersion,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildContainerMountOptions(t *testing.T) {
|
||||||
|
project := composetypes.Project{
|
||||||
|
Name: "myProject",
|
||||||
|
Services: []composetypes.ServiceConfig{
|
||||||
|
{
|
||||||
|
Name: "myService",
|
||||||
|
Volumes: []composetypes.ServiceVolumeConfig{
|
||||||
|
{
|
||||||
|
Type: composetypes.VolumeTypeVolume,
|
||||||
|
Target: "/var/myvolume1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: composetypes.VolumeTypeVolume,
|
||||||
|
Target: "/var/myvolume2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Volumes: composetypes.Volumes(map[string]composetypes.VolumeConfig{
|
||||||
|
"myVolume1": {
|
||||||
|
Name: "myProject_myVolume1",
|
||||||
|
},
|
||||||
|
"myVolume2": {
|
||||||
|
Name: "myProject_myVolume2",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
inherit := &moby.Container{
|
||||||
|
Mounts: []moby.MountPoint{
|
||||||
|
{
|
||||||
|
Type: composetypes.VolumeTypeVolume,
|
||||||
|
Destination: "/var/myvolume1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: composetypes.VolumeTypeVolume,
|
||||||
|
Destination: "/var/myvolume2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
mounts, err := buildContainerMountOptions(project, project.Services[0], moby.ImageInspect{}, inherit)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, len(mounts) == 2)
|
||||||
|
assert.Assert(t, mounts[0].Target == "/var/myvolume1")
|
||||||
|
assert.Assert(t, mounts[1].Target == "/var/myvolume2")
|
||||||
|
|
||||||
|
mounts, err = buildContainerMountOptions(project, project.Services[0], moby.ImageInspect{}, inherit)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, len(mounts) == 2)
|
||||||
|
assert.Assert(t, mounts[0].Target == "/var/myvolume1")
|
||||||
|
assert.Assert(t, mounts[1].Target == "/var/myvolume2")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user