@@ -801,28 +801,28 @@ func (s *composeService) buildContainerVolumes(
801
801
return nil , nil , err
802
802
}
803
803
804
- mountOptions , err := s . buildContainerMountOptions (ctx , p , service , imgInspect , inherit )
804
+ mountOptions , err := buildContainerMountOptions (p , service , imgInspect , inherit )
805
805
if err != nil {
806
806
return nil , nil , err
807
807
}
808
808
809
- version , err := s .RuntimeVersion (ctx )
810
- if err != nil {
811
- return nil , nil , err
812
- }
813
- if versions .GreaterThan (version , "1.42" ) {
814
- // We can fully leverage `Mount` API as a replacement for legacy `Bind`
815
- return nil , mountOptions , nil
816
- }
817
-
818
809
MOUNTS:
819
810
for _ , m := range mountOptions {
811
+ if m .Type == mount .TypeNamedPipe {
812
+ mounts = append (mounts , m )
813
+ continue
814
+ }
820
815
if m .Type == mount .TypeBind {
821
- // `Mount` does not offer option to created host path if missing
816
+ // `Mount` is preferred but does not offer option to created host path if missing
822
817
// so `Bind` API is used here with raw volume string
818
+ // see https://github.com/moby/moby/issues/43483
823
819
for _ , v := range service .Volumes {
824
820
if v .Target == m .Target {
825
- if v .Bind != nil && v .Bind .CreateHostPath {
821
+ switch {
822
+ case string (m .Type ) != v .Type :
823
+ v .Source = m .Source
824
+ fallthrough
825
+ case v .Bind != nil && v .Bind .CreateHostPath :
826
826
binds = append (binds , v .String ())
827
827
continue MOUNTS
828
828
}
@@ -834,7 +834,7 @@ MOUNTS:
834
834
return binds , mounts , nil
835
835
}
836
836
837
- func ( s * composeService ) buildContainerMountOptions (ctx context. Context , p types.Project , service types.ServiceConfig , img moby.ImageInspect , inherit * moby.Container ) ([]mount.Mount , error ) {
837
+ func buildContainerMountOptions (p types.Project , s types.ServiceConfig , img moby.ImageInspect , inherit * moby.Container ) ([]mount.Mount , error ) {
838
838
var mounts = map [string ]mount.Mount {}
839
839
if inherit != nil {
840
840
for _ , m := range inherit .Mounts {
@@ -859,7 +859,7 @@ func (s *composeService) buildContainerMountOptions(ctx context.Context, p types
859
859
}
860
860
}
861
861
volumes := []types.ServiceVolumeConfig {}
862
- for _ , v := range service .Volumes {
862
+ for _ , v := range s .Volumes {
863
863
if v .Target != m .Destination || v .Source != "" {
864
864
volumes = append (volumes , v )
865
865
continue
@@ -872,11 +872,11 @@ func (s *composeService) buildContainerMountOptions(ctx context.Context, p types
872
872
ReadOnly : ! m .RW ,
873
873
}
874
874
}
875
- service .Volumes = volumes
875
+ s .Volumes = volumes
876
876
}
877
877
}
878
878
879
- mounts , err := s . fillBindMounts (ctx , p , service , mounts )
879
+ mounts , err := fillBindMounts (p , s , mounts )
880
880
if err != nil {
881
881
return nil , err
882
882
}
@@ -888,27 +888,27 @@ func (s *composeService) buildContainerMountOptions(ctx context.Context, p types
888
888
return values , nil
889
889
}
890
890
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 )
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 )
894
894
if err != nil {
895
895
return nil , err
896
896
}
897
897
m [bindMount .Target ] = bindMount
898
898
}
899
899
900
- secrets , err := s . buildContainerSecretMounts (ctx , p , service )
900
+ secrets , err := buildContainerSecretMounts (p , s )
901
901
if err != nil {
902
902
return nil , err
903
903
}
904
- for _ , secret := range secrets {
905
- if _ , found := m [secret .Target ]; found {
904
+ for _ , s := range secrets {
905
+ if _ , found := m [s .Target ]; found {
906
906
continue
907
907
}
908
- m [secret .Target ] = secret
908
+ m [s .Target ] = s
909
909
}
910
910
911
- configs , err := s . buildContainerConfigMounts (ctx , p , service )
911
+ configs , err := buildContainerConfigMounts (p , s )
912
912
if err != nil {
913
913
return nil , err
914
914
}
@@ -921,11 +921,11 @@ func (s *composeService) fillBindMounts(ctx context.Context, p types.Project, se
921
921
return m , nil
922
922
}
923
923
924
- func ( s * composeService ) buildContainerConfigMounts (ctx context. Context , p types.Project , service types.ServiceConfig ) ([]mount.Mount , error ) {
924
+ func buildContainerConfigMounts (p types.Project , s types.ServiceConfig ) ([]mount.Mount , error ) {
925
925
var mounts = map [string ]mount.Mount {}
926
926
927
927
configsBaseDir := "/"
928
- for _ , config := range service .Configs {
928
+ for _ , config := range s .Configs {
929
929
target := config .Target
930
930
if config .Target == "" {
931
931
target = configsBaseDir + config .Source
@@ -953,7 +953,7 @@ func (s *composeService) buildContainerConfigMounts(ctx context.Context, p types
953
953
continue
954
954
}
955
955
956
- bindMount , err := s . buildMount (ctx , p , types.ServiceVolumeConfig {
956
+ bindMount , err := buildMount (p , types.ServiceVolumeConfig {
957
957
Type : types .VolumeTypeBind ,
958
958
Source : definedConfig .File ,
959
959
Target : target ,
@@ -971,11 +971,11 @@ func (s *composeService) buildContainerConfigMounts(ctx context.Context, p types
971
971
return values , nil
972
972
}
973
973
974
- func ( s * composeService ) buildContainerSecretMounts (ctx context. Context , p types.Project , service types.ServiceConfig ) ([]mount.Mount , error ) {
974
+ func buildContainerSecretMounts (p types.Project , s types.ServiceConfig ) ([]mount.Mount , error ) {
975
975
var mounts = map [string ]mount.Mount {}
976
976
977
977
secretsDir := "/run/secrets/"
978
- for _ , secret := range service .Secrets {
978
+ for _ , secret := range s .Secrets {
979
979
target := secret .Target
980
980
if secret .Target == "" {
981
981
target = secretsDir + secret .Source
@@ -1003,7 +1003,7 @@ func (s *composeService) buildContainerSecretMounts(ctx context.Context, p types
1003
1003
continue
1004
1004
}
1005
1005
1006
- mnt , err := s . buildMount (ctx , p , types.ServiceVolumeConfig {
1006
+ mnt , err := buildMount (p , types.ServiceVolumeConfig {
1007
1007
Type : types .VolumeTypeBind ,
1008
1008
Source : definedSecret .File ,
1009
1009
Target : target ,
@@ -1039,7 +1039,7 @@ func isWindowsAbs(p string) bool {
1039
1039
return false
1040
1040
}
1041
1041
1042
- func ( s * composeService ) buildMount (ctx context. Context , project types.Project , volume types.ServiceVolumeConfig ) (mount.Mount , error ) {
1042
+ func buildMount (project types.Project , volume types.ServiceVolumeConfig ) (mount.Mount , error ) {
1043
1043
source := volume .Source
1044
1044
// on windows, filepath.IsAbs(source) is false for unix style abs path like /var/run/docker.sock.
1045
1045
// do not replace these with filepath.Abs(source) that will include a default drive.
@@ -1060,10 +1060,7 @@ func (s *composeService) buildMount(ctx context.Context, project types.Project,
1060
1060
}
1061
1061
}
1062
1062
1063
- bind , vol , tmpfs , err := s .buildMountOptions (ctx , volume )
1064
- if err != nil {
1065
- return mount.Mount {}, err
1066
- }
1063
+ bind , vol , tmpfs := buildMountOptions (project , volume )
1067
1064
1068
1065
volume .Target = path .Clean (volume .Target )
1069
1066
@@ -1083,7 +1080,7 @@ func (s *composeService) buildMount(ctx context.Context, project types.Project,
1083
1080
}, nil
1084
1081
}
1085
1082
1086
- func ( s * composeService ) buildMountOptions (ctx context. Context , volume types.ServiceVolumeConfig ) (* mount.BindOptions , * mount.VolumeOptions , * mount.TmpfsOptions , error ) {
1083
+ func buildMountOptions (project types. Project , volume types.ServiceVolumeConfig ) (* mount.BindOptions , * mount.VolumeOptions , * mount.TmpfsOptions ) {
1087
1084
switch volume .Type {
1088
1085
case "bind" :
1089
1086
if volume .Volume != nil {
@@ -1092,47 +1089,40 @@ func (s *composeService) buildMountOptions(ctx context.Context, volume types.Ser
1092
1089
if volume .Tmpfs != nil {
1093
1090
logrus .Warnf ("mount of type `bind` should not define `tmpfs` option" )
1094
1091
}
1095
- option , err := s .buildBindOption (ctx , volume .Bind )
1096
- return option , nil , nil , err
1092
+ return buildBindOption (volume .Bind ), nil , nil
1097
1093
case "volume" :
1098
1094
if volume .Bind != nil {
1099
1095
logrus .Warnf ("mount of type `volume` should not define `bind` option" )
1100
1096
}
1101
1097
if volume .Tmpfs != nil {
1102
1098
logrus .Warnf ("mount of type `volume` should not define `tmpfs` option" )
1103
1099
}
1104
- return nil , buildVolumeOptions (volume .Volume ), nil , nil
1100
+ if v , ok := project .Volumes [volume .Source ]; ok && v .DriverOpts ["o" ] == types .VolumeTypeBind {
1101
+ return buildBindOption (& types.ServiceVolumeBind {
1102
+ CreateHostPath : true ,
1103
+ }), nil , nil
1104
+ }
1105
+ return nil , buildVolumeOptions (volume .Volume ), nil
1105
1106
case "tmpfs" :
1106
1107
if volume .Bind != nil {
1107
1108
logrus .Warnf ("mount of type `tmpfs` should not define `bind` option" )
1108
1109
}
1109
1110
if volume .Volume != nil {
1110
1111
logrus .Warnf ("mount of type `tmpfs` should not define `volume` option" )
1111
1112
}
1112
- return nil , nil , buildTmpfsOptions (volume .Tmpfs ), nil
1113
+ return nil , nil , buildTmpfsOptions (volume .Tmpfs )
1113
1114
}
1114
- return nil , nil , nil , nil
1115
+ return nil , nil , nil
1115
1116
}
1116
1117
1117
- func ( s * composeService ) buildBindOption (ctx context. Context , bind * types.ServiceVolumeBind ) ( * mount.BindOptions , error ) {
1118
+ func buildBindOption (bind * types.ServiceVolumeBind ) * mount.BindOptions {
1118
1119
if bind == nil {
1119
- return nil , nil
1120
- }
1121
-
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
1120
+ return nil
1129
1121
}
1130
-
1131
1122
return & mount.BindOptions {
1132
- Propagation : mount .Propagation (propagation ),
1133
- CreateMountpoint : bind .CreateHostPath ,
1123
+ Propagation : mount .Propagation (bind .Propagation ),
1134
1124
// NonRecursive: false, FIXME missing from model ?
1135
- }, nil
1125
+ }
1136
1126
}
1137
1127
1138
1128
func buildVolumeOptions (vol * types.ServiceVolumeVolume ) * mount.VolumeOptions {
0 commit comments