Skip to content

Commit 12f6e87

Browse files
committed
introduce bind create-host-path option, aligned with docker-compose
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent f5a7a3c commit 12f6e87

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

docs/reference/commandline/service_create.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,24 @@ The following options can only be used for bind mounts (`type=bind`):
452452
Read-only mounts are made recursively read-only if kernel is v5.12 or later.
453453
Otherwise the Engine raises an error.</li>
454454
</ul>
455-
When the option is not specified, the default behavior correponds to setting <tt>enabled</tt>.
455+
When the option is not specified, the default behavior corresponds to setting <tt>enabled</tt>.
456+
</td>
457+
</tr>
458+
<tr>
459+
<td><b>bind-create-host-path</b></td>
460+
<td>
461+
By default, mounts require the mount point to exist on host. This is a significant difference with
462+
the <tt>volumes</tt> flag.
463+
Set <tt>bind-create-host-path</tt> to control the behavior of the engine regarding a non-existent
464+
mount target.<br />
465+
<br />
466+
A value is one of:<br />
467+
<br />
468+
<ul>
469+
<li><<tt>true</tt>: Create path on host if it doesn't exists.</li>
470+
<li><<tt>false</tt>: Default behavior. If remote path doesn't exists, an error will be reported.</li>
471+
</ul>
472+
When the option is not specified, the default behavior corresponds to setting <tt>true</tt>.
456473
</td>
457474
</tr>
458475
</table>

opts/mount.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ func (m *MountOpt) Set(value string) error {
131131
default:
132132
return fmt.Errorf(`invalid value for %s: %s (must be "enabled", "disabled", "writable", or "readonly")`, key, val)
133133
}
134+
case "bind-create-host-path":
135+
switch val {
136+
case "true":
137+
bindOptions().CreateMountpoint = true
138+
case "false":
139+
bindOptions().CreateMountpoint = false
140+
default:
141+
return fmt.Errorf(`invalid value for %s: %s (must be "true" or "false")`, key, val)
142+
}
134143
case "volume-subpath":
135144
volumeOptions().Subpath = val
136145
case "volume-nocopy":

opts/mount_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,19 @@ func TestMountOptSetBindRecursive(t *testing.T) {
325325
},
326326
}, m.Value()))
327327
})
328+
329+
t.Run("create-host-path", func(t *testing.T) {
330+
var m MountOpt
331+
assert.NilError(t, m.Set("type=bind,source=/foo,target=/bar,bind-create-host-path=true"))
332+
assert.Check(t, is.DeepEqual([]mount.Mount{
333+
{
334+
Type: mount.TypeBind,
335+
Source: "/foo",
336+
Target: "/bar",
337+
BindOptions: &mount.BindOptions{
338+
CreateMountpoint: true,
339+
},
340+
},
341+
}, m.Value()))
342+
})
328343
}

0 commit comments

Comments
 (0)