Skip to content

Commit c16df17

Browse files
ndeloofglours
authored andcommitted
don't set propagation if target engine isn't linux
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 20404db commit c16df17

3 files changed

Lines changed: 67 additions & 39 deletions

File tree

pkg/compose/compose.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,23 @@ func (s *composeService) RuntimeVersion(ctx context.Context) (string, error) {
321321

322322
}
323323

324+
var windowsContainer = struct {
325+
once sync.Once
326+
val bool
327+
err error
328+
}{}
329+
330+
func (s *composeService) isWindowsContainer(ctx context.Context) (bool, error) {
331+
windowsContainer.once.Do(func() {
332+
info, err := s.apiClient().Info(ctx)
333+
if err != nil {
334+
windowsContainer.err = err
335+
}
336+
windowsContainer.val = info.OSType == "windows"
337+
})
338+
return windowsContainer.val, windowsContainer.err
339+
}
340+
324341
func (s *composeService) isDesktopIntegrationActive() bool {
325342
return s.desktopCli != nil
326343
}

pkg/compose/create.go

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ func (s *composeService) buildContainerVolumes(
801801
return nil, nil, err
802802
}
803803

804-
mountOptions, err := buildContainerMountOptions(p, service, imgInspect, inherit)
804+
mountOptions, err := s.buildContainerMountOptions(ctx, p, service, imgInspect, inherit)
805805
if err != nil {
806806
return nil, nil, err
807807
}
@@ -834,7 +834,7 @@ MOUNTS:
834834
return binds, mounts, nil
835835
}
836836

837-
func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
837+
func (s *composeService) buildContainerMountOptions(ctx context.Context, p types.Project, service types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
838838
var mounts = map[string]mount.Mount{}
839839
if inherit != nil {
840840
for _, m := range inherit.Mounts {
@@ -859,7 +859,7 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
859859
}
860860
}
861861
volumes := []types.ServiceVolumeConfig{}
862-
for _, v := range s.Volumes {
862+
for _, v := range service.Volumes {
863863
if v.Target != m.Destination || v.Source != "" {
864864
volumes = append(volumes, v)
865865
continue
@@ -872,11 +872,11 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
872872
ReadOnly: !m.RW,
873873
}
874874
}
875-
s.Volumes = volumes
875+
service.Volumes = volumes
876876
}
877877
}
878878

879-
mounts, err := fillBindMounts(p, s, mounts)
879+
mounts, err := s.fillBindMounts(ctx, p, service, mounts)
880880
if err != nil {
881881
return nil, err
882882
}
@@ -888,27 +888,27 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
888888
return values, nil
889889
}
890890

891-
func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
892-
for _, v := range s.Volumes {
893-
bindMount, err := buildMount(p, v)
891+
func (s *composeService) fillBindMounts(ctx context.Context, p types.Project, service types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
892+
for _, v := range service.Volumes {
893+
bindMount, err := s.buildMount(ctx, p, v)
894894
if err != nil {
895895
return nil, err
896896
}
897897
m[bindMount.Target] = bindMount
898898
}
899899

900-
secrets, err := buildContainerSecretMounts(p, s)
900+
secrets, err := s.buildContainerSecretMounts(ctx, p, service)
901901
if err != nil {
902902
return nil, err
903903
}
904-
for _, s := range secrets {
905-
if _, found := m[s.Target]; found {
904+
for _, secret := range secrets {
905+
if _, found := m[secret.Target]; found {
906906
continue
907907
}
908-
m[s.Target] = s
908+
m[secret.Target] = secret
909909
}
910910

911-
configs, err := buildContainerConfigMounts(p, s)
911+
configs, err := s.buildContainerConfigMounts(ctx, p, service)
912912
if err != nil {
913913
return nil, err
914914
}
@@ -921,11 +921,11 @@ func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.M
921921
return m, nil
922922
}
923923

924-
func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
924+
func (s *composeService) buildContainerConfigMounts(ctx context.Context, p types.Project, service types.ServiceConfig) ([]mount.Mount, error) {
925925
var mounts = map[string]mount.Mount{}
926926

927927
configsBaseDir := "/"
928-
for _, config := range s.Configs {
928+
for _, config := range service.Configs {
929929
target := config.Target
930930
if config.Target == "" {
931931
target = configsBaseDir + config.Source
@@ -953,7 +953,7 @@ func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount
953953
continue
954954
}
955955

956-
bindMount, err := buildMount(p, types.ServiceVolumeConfig{
956+
bindMount, err := s.buildMount(ctx, p, types.ServiceVolumeConfig{
957957
Type: types.VolumeTypeBind,
958958
Source: definedConfig.File,
959959
Target: target,
@@ -971,11 +971,11 @@ func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount
971971
return values, nil
972972
}
973973

974-
func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
974+
func (s *composeService) buildContainerSecretMounts(ctx context.Context, p types.Project, service types.ServiceConfig) ([]mount.Mount, error) {
975975
var mounts = map[string]mount.Mount{}
976976

977977
secretsDir := "/run/secrets/"
978-
for _, secret := range s.Secrets {
978+
for _, secret := range service.Secrets {
979979
target := secret.Target
980980
if secret.Target == "" {
981981
target = secretsDir + secret.Source
@@ -1003,7 +1003,7 @@ func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount
10031003
continue
10041004
}
10051005

1006-
mnt, err := buildMount(p, types.ServiceVolumeConfig{
1006+
mnt, err := s.buildMount(ctx, p, types.ServiceVolumeConfig{
10071007
Type: types.VolumeTypeBind,
10081008
Source: definedSecret.File,
10091009
Target: target,
@@ -1039,7 +1039,7 @@ func isWindowsAbs(p string) bool {
10391039
return false
10401040
}
10411041

1042-
func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
1042+
func (s *composeService) buildMount(ctx context.Context, project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
10431043
source := volume.Source
10441044
// on windows, filepath.IsAbs(source) is false for unix style abs path like /var/run/docker.sock.
10451045
// do not replace these with filepath.Abs(source) that will include a default drive.
@@ -1060,7 +1060,10 @@ func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.
10601060
}
10611061
}
10621062

1063-
bind, vol, tmpfs := buildMountOptions(volume)
1063+
bind, vol, tmpfs, err := s.buildMountOptions(ctx, volume)
1064+
if err != nil {
1065+
return mount.Mount{}, err
1066+
}
10641067

10651068
volume.Target = path.Clean(volume.Target)
10661069

@@ -1080,7 +1083,7 @@ func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.
10801083
}, nil
10811084
}
10821085

1083-
func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions) {
1086+
func (s *composeService) buildMountOptions(ctx context.Context, volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions, error) {
10841087
switch volume.Type {
10851088
case "bind":
10861089
if volume.Volume != nil {
@@ -1089,43 +1092,47 @@ func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *m
10891092
if volume.Tmpfs != nil {
10901093
logrus.Warnf("mount of type `bind` should not define `tmpfs` option")
10911094
}
1092-
return buildBindOption(volume.Bind), nil, nil
1095+
option, err := s.buildBindOption(ctx, volume.Bind)
1096+
return option, nil, nil, err
10931097
case "volume":
10941098
if volume.Bind != nil {
10951099
logrus.Warnf("mount of type `volume` should not define `bind` option")
10961100
}
10971101
if volume.Tmpfs != nil {
10981102
logrus.Warnf("mount of type `volume` should not define `tmpfs` option")
10991103
}
1100-
return nil, buildVolumeOptions(volume.Volume), nil
1104+
return nil, buildVolumeOptions(volume.Volume), nil, nil
11011105
case "tmpfs":
11021106
if volume.Bind != nil {
11031107
logrus.Warnf("mount of type `tmpfs` should not define `bind` option")
11041108
}
11051109
if volume.Volume != nil {
11061110
logrus.Warnf("mount of type `tmpfs` should not define `volume` option")
11071111
}
1108-
return nil, nil, buildTmpfsOptions(volume.Tmpfs)
1112+
return nil, nil, buildTmpfsOptions(volume.Tmpfs), nil
11091113
}
1110-
return nil, nil, nil
1114+
return nil, nil, nil, nil
11111115
}
11121116

1113-
func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
1117+
func (s *composeService) buildBindOption(ctx context.Context, bind *types.ServiceVolumeBind) (*mount.BindOptions, error) {
11141118
if bind == nil {
1115-
return nil
1119+
return nil, nil
11161120
}
11171121

1118-
// I miss ternary operator
1119-
propagation := types.PropagationRPrivate
1120-
if bind.Propagation != "" {
1121-
propagation = bind.Propagation
1122+
propagation := bind.Propagation
1123+
isWindowsContainer, err := s.isWindowsContainer(ctx)
1124+
if err != nil {
1125+
return nil, err
1126+
}
1127+
if propagation == "" && !isWindowsContainer {
1128+
propagation = types.PropagationRPrivate
11221129
}
11231130

11241131
return &mount.BindOptions{
11251132
Propagation: mount.Propagation(propagation),
11261133
CreateMountpoint: bind.CreateHostPath,
11271134
// NonRecursive: false, FIXME missing from model ?
1128-
}
1135+
}, nil
11291136
}
11301137

11311138
func buildVolumeOptions(vol *types.ServiceVolumeVolume) *mount.VolumeOptions {

pkg/compose/create_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package compose
1818

1919
import (
20+
"context"
2021
"os"
2122
"path/filepath"
2223
"sort"
@@ -41,7 +42,8 @@ func TestBuildBindMount(t *testing.T) {
4142
Source: "",
4243
Target: "/data",
4344
}
44-
mount, err := buildMount(project, volume)
45+
s := composeService{}
46+
mount, err := s.buildMount(context.TODO(), project, volume)
4547
assert.NilError(t, err)
4648
assert.Assert(t, filepath.IsAbs(mount.Source))
4749
_, err = os.Stat(mount.Source)
@@ -56,7 +58,8 @@ func TestBuildNamedPipeMount(t *testing.T) {
5658
Source: "\\\\.\\pipe\\docker_engine_windows",
5759
Target: "\\\\.\\pipe\\docker_engine",
5860
}
59-
mount, err := buildMount(project, volume)
61+
s := composeService{}
62+
mount, err := s.buildMount(context.TODO(), project, volume)
6063
assert.NilError(t, err)
6164
assert.Equal(t, mount.Type, mountTypes.TypeNamedPipe)
6265
}
@@ -75,7 +78,8 @@ func TestBuildVolumeMount(t *testing.T) {
7578
Source: "myVolume",
7679
Target: "/data",
7780
}
78-
mount, err := buildMount(project, volume)
81+
s := composeService{}
82+
mount, err := s.buildMount(context.TODO(), project, volume)
7983
assert.NilError(t, err)
8084
assert.Equal(t, mount.Source, "myProject_myVolume")
8185
assert.Equal(t, mount.Type, mountTypes.TypeVolume)
@@ -152,8 +156,8 @@ func TestBuildContainerMountOptions(t *testing.T) {
152156
},
153157
},
154158
}
155-
156-
mounts, err := buildContainerMountOptions(project, project.Services["myService"], moby.ImageInspect{}, inherit)
159+
s := composeService{}
160+
mounts, err := s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], moby.ImageInspect{}, inherit)
157161
sort.Slice(mounts, func(i, j int) bool {
158162
return mounts[i].Target < mounts[j].Target
159163
})
@@ -165,7 +169,7 @@ func TestBuildContainerMountOptions(t *testing.T) {
165169
assert.Equal(t, mounts[2].VolumeOptions.Subpath, "etc")
166170
assert.Equal(t, mounts[3].Target, "\\\\.\\pipe\\docker_engine")
167171

168-
mounts, err = buildContainerMountOptions(project, project.Services["myService"], moby.ImageInspect{}, inherit)
172+
mounts, err = s.buildContainerMountOptions(context.TODO(), project, project.Services["myService"], moby.ImageInspect{}, inherit)
169173
sort.Slice(mounts, func(i, j int) bool {
170174
return mounts[i].Target < mounts[j].Target
171175
})

0 commit comments

Comments
 (0)