mirror of
https://github.com/docker/compose.git
synced 2025-07-22 05:04:27 +02:00
prefer mount API over bind
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
6e172d6b89
commit
f9c7a0cc08
@ -28,7 +28,6 @@ import (
|
|||||||
containerType "github.com/docker/docker/api/types/container"
|
containerType "github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
"github.com/docker/go-connections/nat"
|
|
||||||
"go.uber.org/mock/gomock"
|
"go.uber.org/mock/gomock"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
|
||||||
@ -301,17 +300,10 @@ func TestCreateMobyContainer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var falseBool bool
|
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Cond(func(x any) bool {
|
||||||
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Eq(
|
v := x.(*containerType.HostConfig)
|
||||||
&containerType.HostConfig{
|
return v.NetworkMode == "b-moby-name"
|
||||||
PortBindings: nat.PortMap{},
|
}), gomock.Eq(
|
||||||
ExtraHosts: []string{},
|
|
||||||
Tmpfs: map[string]string{},
|
|
||||||
Resources: containerType.Resources{
|
|
||||||
OomKillDisable: &falseBool,
|
|
||||||
},
|
|
||||||
NetworkMode: "b-moby-name",
|
|
||||||
}), gomock.Eq(
|
|
||||||
&network.NetworkingConfig{
|
&network.NetworkingConfig{
|
||||||
EndpointsConfig: map[string]*network.EndpointSettings{
|
EndpointsConfig: map[string]*network.EndpointSettings{
|
||||||
"b-moby-name": {
|
"b-moby-name": {
|
||||||
@ -390,17 +382,10 @@ func TestCreateMobyContainer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var falseBool bool
|
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Cond(func(x any) bool {
|
||||||
apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Eq(
|
v := x.(*containerType.HostConfig)
|
||||||
&containerType.HostConfig{
|
return v.NetworkMode == "b-moby-name"
|
||||||
PortBindings: nat.PortMap{},
|
}), gomock.Eq(
|
||||||
ExtraHosts: []string{},
|
|
||||||
Tmpfs: map[string]string{},
|
|
||||||
Resources: containerType.Resources{
|
|
||||||
OomKillDisable: &falseBool,
|
|
||||||
},
|
|
||||||
NetworkMode: "b-moby-name",
|
|
||||||
}), gomock.Eq(
|
|
||||||
&network.NetworkingConfig{
|
&network.NetworkingConfig{
|
||||||
EndpointsConfig: map[string]*network.EndpointSettings{
|
EndpointsConfig: map[string]*network.EndpointSettings{
|
||||||
"a-moby-name": {
|
"a-moby-name": {
|
||||||
|
@ -824,23 +824,23 @@ func (s *composeService) buildContainerVolumes(
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
version, err := s.RuntimeVersion(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if versions.GreaterThan(version, "1.42") {
|
||||||
|
// We can fully leverage `Mount` API as a replacement for legacy `Bind`
|
||||||
|
return nil, mountOptions, nil
|
||||||
|
}
|
||||||
|
|
||||||
MOUNTS:
|
MOUNTS:
|
||||||
for _, m := range mountOptions {
|
for _, m := range mountOptions {
|
||||||
if m.Type == mount.TypeNamedPipe {
|
|
||||||
mounts = append(mounts, m)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if m.Type == mount.TypeBind {
|
if m.Type == mount.TypeBind {
|
||||||
// `Mount` is preferred but does not offer option to created host path if missing
|
// `Mount` does not offer option to created host path if missing
|
||||||
// so `Bind` API is used here with raw volume string
|
// so `Bind` API is used here with raw volume string
|
||||||
// see https://github.com/moby/moby/issues/43483
|
|
||||||
for _, v := range service.Volumes {
|
for _, v := range service.Volumes {
|
||||||
if v.Target == m.Target {
|
if v.Target == m.Target {
|
||||||
switch {
|
if v.Bind != nil && v.Bind.CreateHostPath {
|
||||||
case string(m.Type) != v.Type:
|
|
||||||
v.Source = m.Source
|
|
||||||
fallthrough
|
|
||||||
case v.Bind != nil && v.Bind.CreateHostPath:
|
|
||||||
binds = append(binds, v.String())
|
binds = append(binds, v.String())
|
||||||
continue MOUNTS
|
continue MOUNTS
|
||||||
}
|
}
|
||||||
@ -1078,7 +1078,7 @@ func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bind, vol, tmpfs := buildMountOptions(project, volume)
|
bind, vol, tmpfs := buildMountOptions(volume)
|
||||||
|
|
||||||
volume.Target = path.Clean(volume.Target)
|
volume.Target = path.Clean(volume.Target)
|
||||||
|
|
||||||
@ -1098,7 +1098,7 @@ func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildMountOptions(project types.Project, volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions) {
|
func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions) {
|
||||||
switch volume.Type {
|
switch volume.Type {
|
||||||
case "bind":
|
case "bind":
|
||||||
if volume.Volume != nil {
|
if volume.Volume != nil {
|
||||||
@ -1115,11 +1115,6 @@ func buildMountOptions(project types.Project, volume types.ServiceVolumeConfig)
|
|||||||
if volume.Tmpfs != nil {
|
if volume.Tmpfs != nil {
|
||||||
logrus.Warnf("mount of type `volume` should not define `tmpfs` option")
|
logrus.Warnf("mount of type `volume` should not define `tmpfs` option")
|
||||||
}
|
}
|
||||||
if v, ok := project.Volumes[volume.Source]; ok && v.DriverOpts["o"] == types.VolumeTypeBind {
|
|
||||||
return buildBindOption(&types.ServiceVolumeBind{
|
|
||||||
CreateHostPath: true,
|
|
||||||
}), nil, nil
|
|
||||||
}
|
|
||||||
return nil, buildVolumeOptions(volume.Volume), nil
|
return nil, buildVolumeOptions(volume.Volume), nil
|
||||||
case "tmpfs":
|
case "tmpfs":
|
||||||
if volume.Bind != nil {
|
if volume.Bind != nil {
|
||||||
@ -1138,7 +1133,8 @@ func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &mount.BindOptions{
|
return &mount.BindOptions{
|
||||||
Propagation: mount.Propagation(bind.Propagation),
|
Propagation: mount.Propagation(bind.Propagation),
|
||||||
|
CreateMountpoint: bind.CreateHostPath,
|
||||||
// NonRecursive: false, FIXME missing from model ?
|
// NonRecursive: false, FIXME missing from model ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user