Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/reference/commandline/service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,22 @@ The following options can only be used for bind mounts (`type=bind`):
When the option is not specified, the default behavior corresponds to setting <tt>enabled</tt>.
</td>
</tr>
<tr>
<td><b>bind-create-mountpoint</b></td>
<td>
By default, bind mounts require the source path to exist on the host. This is a significant difference
from the <tt>-v</tt> flag, which creates the source path if it doesn't exist.<br />
<br />
Set <tt>bind-create-mountpoint</tt> to create the source path on the host if it doesn't exist.<br />
<br />
A value is optional:<br />
<br />
<ul>
<li><tt>true</tt> or <tt>1</tt>: Create path on host if it doesn't exist.</li>
<li><tt>false</tt> or <tt>0</tt>: Default behavior. If source path doesn't exist, an error will be reported.</li>
</ul>
</td>
</tr>
</table>

##### Bind propagation
Expand Down
31 changes: 31 additions & 0 deletions e2e/container/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"math/rand"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -160,6 +161,36 @@ func TestMountSubvolume(t *testing.T) {
}
}

func TestMountBindCreateMountpoint(t *testing.T) {
environment.SkipIfDaemonNotLinux(t)

for _, tc := range []struct {
name string
value string
expectSuccess bool
}{
{name: "flag only", value: "bind-create-mountpoint", expectSuccess: true},
{name: "true", value: "bind-create-mountpoint=true", expectSuccess: true},
{name: "1", value: "bind-create-mountpoint=1", expectSuccess: true},
{name: "false", value: "bind-create-mountpoint=false", expectSuccess: false},
{name: "0", value: "bind-create-mountpoint=0", expectSuccess: false},
} {
t.Run(tc.name, func(t *testing.T) {
srcPath := filepath.Join("/tmp", t.Name(), "does", "not", "exist")
result := icmd.RunCommand("docker", "run", "--rm",
"--mount", "type=bind,src="+srcPath+",dst=/mnt,"+tc.value,
fixtures.AlpineImage, "cat", "/proc/mounts")
if tc.expectSuccess {
result.Assert(t, icmd.Success)
assert.Check(t, is.Contains(result.Stdout(), "/mnt"))
} else {
result.Assert(t, icmd.Expected{ExitCode: 125})
assert.Check(t, is.Contains(result.Stderr(), srcPath))
}
})
}
}

func TestProcessTermination(t *testing.T) {
var out bytes.Buffer
cmd := icmd.Command("docker", "run", "--rm", "-i", fixtures.AlpineImage,
Expand Down
7 changes: 6 additions & 1 deletion opts/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (m *MountOpt) Set(value string) error {

if !hasValue {
switch key {
case "readonly", "ro", "volume-nocopy", "bind-nonrecursive":
case "readonly", "ro", "volume-nocopy", "bind-nonrecursive", "bind-create-mountpoint":
// boolean values
default:
return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
Expand Down Expand Up @@ -102,6 +102,11 @@ func (m *MountOpt) Set(value string) error {
default:
return fmt.Errorf(`invalid value for %s: %s (must be "enabled", "disabled", "writable", or "readonly")`, key, val)
}
case "bind-create-mountpoint":
ensureBindOptions(&mount).CreateMountpoint, err = parseBoolValue(key, val, hasValue)
if err != nil {
return err
}
case "volume-subpath":
ensureVolumeOptions(&mount).Subpath = val
case "volume-nocopy":
Expand Down
44 changes: 44 additions & 0 deletions opts/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,50 @@ func TestMountOptSetTmpfsNoError(t *testing.T) {
}
}

func TestMountOptSetBindCreateMountpoint(t *testing.T) {
tests := []struct {
value string
exp bool
expErr string
}{
{value: "", exp: false},
{value: "bind-create-mountpoint", exp: true},
{value: "bind-create-mountpoint=", expErr: `invalid value for 'bind-create-mountpoint': value is empty`},
{value: "bind-create-mountpoint= true", expErr: `invalid value for 'bind-create-mountpoint' in 'bind-create-mountpoint= true': value should not have whitespace`},
{value: "bind-create-mountpoint=no", expErr: `invalid value for 'bind-create-mountpoint': invalid boolean value ("no"): must be one of "true", "1", "false", or "0" (default "true")`},
{value: "bind-create-mountpoint=1", exp: true},
{value: "bind-create-mountpoint=true", exp: true},
{value: "bind-create-mountpoint=0", exp: false},
{value: "bind-create-mountpoint=false", exp: false},
}

for _, tc := range tests {
name := tc.value
if name == "" {
name = "not set"
}
t.Run(name, func(t *testing.T) {
val := "type=bind,target=/foo,source=/foo"
if tc.value != "" {
val += "," + tc.value
}
var m MountOpt
err := m.Set(val)
if tc.expErr != "" {
assert.Error(t, err, tc.expErr)
return
}
assert.NilError(t, err)
if tc.value == "" {
assert.Check(t, is.Nil(m.values[0].BindOptions))
} else {
assert.Check(t, m.values[0].BindOptions != nil)
assert.Check(t, is.Equal(m.values[0].BindOptions.CreateMountpoint, tc.exp))
}
})
}
}

func TestMountOptSetBindRecursive(t *testing.T) {
t.Run("enabled", func(t *testing.T) {
var m MountOpt
Expand Down
Loading