Skip to content

Commit 273eeb8

Browse files
committed
cli: add --mount to docker run
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 0f9ba0e commit 273eeb8

16 files changed

Lines changed: 511 additions & 290 deletions

File tree

cli/command/service/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
3535
flags.Var(&opts.containerLabels, flagContainerLabel, "Container labels")
3636
flags.VarP(&opts.env, flagEnv, "e", "Set environment variables")
3737
flags.Var(&opts.envFile, flagEnvFile, "Read in a file of environment variables")
38-
flags.Var(&opts.mounts, flagMount, "Attach a mount to the service")
38+
flags.Var(&opts.mounts, flagMount, "Attach a filesystem mount to the service")
3939
flags.StringSliceVar(&opts.constraints, flagConstraint, []string{}, "Placement constraints")
4040
flags.StringSliceVar(&opts.networks, flagNetwork, []string{}, "Network attachments")
4141
flags.VarP(&opts.endpoint.ports, flagPublish, "p", "Publish a port as a node port")

cli/command/service/opts.go

Lines changed: 1 addition & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package service
22

33
import (
4-
"encoding/csv"
54
"fmt"
65
"math/big"
76
"strconv"
87
"strings"
98
"time"
109

1110
"github.com/docker/docker/api/types/container"
12-
mounttypes "github.com/docker/docker/api/types/mount"
1311
"github.com/docker/docker/api/types/swarm"
1412
"github.com/docker/docker/opts"
1513
runconfigopts "github.com/docker/docker/runconfig/opts"
@@ -149,143 +147,6 @@ func (i *Uint64Opt) Value() *uint64 {
149147
return i.value
150148
}
151149

152-
// MountOpt is a Value type for parsing mounts
153-
type MountOpt struct {
154-
values []mounttypes.Mount
155-
}
156-
157-
// Set a new mount value
158-
func (m *MountOpt) Set(value string) error {
159-
csvReader := csv.NewReader(strings.NewReader(value))
160-
fields, err := csvReader.Read()
161-
if err != nil {
162-
return err
163-
}
164-
165-
mount := mounttypes.Mount{}
166-
167-
volumeOptions := func() *mounttypes.VolumeOptions {
168-
if mount.VolumeOptions == nil {
169-
mount.VolumeOptions = &mounttypes.VolumeOptions{
170-
Labels: make(map[string]string),
171-
}
172-
}
173-
if mount.VolumeOptions.DriverConfig == nil {
174-
mount.VolumeOptions.DriverConfig = &mounttypes.Driver{}
175-
}
176-
return mount.VolumeOptions
177-
}
178-
179-
bindOptions := func() *mounttypes.BindOptions {
180-
if mount.BindOptions == nil {
181-
mount.BindOptions = new(mounttypes.BindOptions)
182-
}
183-
return mount.BindOptions
184-
}
185-
186-
setValueOnMap := func(target map[string]string, value string) {
187-
parts := strings.SplitN(value, "=", 2)
188-
if len(parts) == 1 {
189-
target[value] = ""
190-
} else {
191-
target[parts[0]] = parts[1]
192-
}
193-
}
194-
195-
mount.Type = mounttypes.TypeVolume // default to volume mounts
196-
// Set writable as the default
197-
for _, field := range fields {
198-
parts := strings.SplitN(field, "=", 2)
199-
key := strings.ToLower(parts[0])
200-
201-
if len(parts) == 1 {
202-
switch key {
203-
case "readonly", "ro":
204-
mount.ReadOnly = true
205-
continue
206-
case "volume-nocopy":
207-
volumeOptions().NoCopy = true
208-
continue
209-
}
210-
}
211-
212-
if len(parts) != 2 {
213-
return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
214-
}
215-
216-
value := parts[1]
217-
switch key {
218-
case "type":
219-
mount.Type = mounttypes.Type(strings.ToLower(value))
220-
case "source", "src":
221-
mount.Source = value
222-
case "target", "dst", "destination":
223-
mount.Target = value
224-
case "readonly", "ro":
225-
mount.ReadOnly, err = strconv.ParseBool(value)
226-
if err != nil {
227-
return fmt.Errorf("invalid value for %s: %s", key, value)
228-
}
229-
case "bind-propagation":
230-
bindOptions().Propagation = mounttypes.Propagation(strings.ToLower(value))
231-
case "volume-nocopy":
232-
volumeOptions().NoCopy, err = strconv.ParseBool(value)
233-
if err != nil {
234-
return fmt.Errorf("invalid value for populate: %s", value)
235-
}
236-
case "volume-label":
237-
setValueOnMap(volumeOptions().Labels, value)
238-
case "volume-driver":
239-
volumeOptions().DriverConfig.Name = value
240-
case "volume-opt":
241-
if volumeOptions().DriverConfig.Options == nil {
242-
volumeOptions().DriverConfig.Options = make(map[string]string)
243-
}
244-
setValueOnMap(volumeOptions().DriverConfig.Options, value)
245-
default:
246-
return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
247-
}
248-
}
249-
250-
if mount.Type == "" {
251-
return fmt.Errorf("type is required")
252-
}
253-
254-
if mount.Target == "" {
255-
return fmt.Errorf("target is required")
256-
}
257-
258-
if mount.Type == mounttypes.TypeBind && mount.VolumeOptions != nil {
259-
return fmt.Errorf("cannot mix 'volume-*' options with mount type '%s'", mounttypes.TypeBind)
260-
}
261-
if mount.Type == mounttypes.TypeVolume && mount.BindOptions != nil {
262-
return fmt.Errorf("cannot mix 'bind-*' options with mount type '%s'", mounttypes.TypeVolume)
263-
}
264-
265-
m.values = append(m.values, mount)
266-
return nil
267-
}
268-
269-
// Type returns the type of this option
270-
func (m *MountOpt) Type() string {
271-
return "mount"
272-
}
273-
274-
// String returns a string repr of this option
275-
func (m *MountOpt) String() string {
276-
mounts := []string{}
277-
for _, mount := range m.values {
278-
repr := fmt.Sprintf("%s %s %s", mount.Type, mount.Source, mount.Target)
279-
mounts = append(mounts, repr)
280-
}
281-
return strings.Join(mounts, ", ")
282-
}
283-
284-
// Value returns the mounts
285-
func (m *MountOpt) Value() []mounttypes.Mount {
286-
return m.values
287-
}
288-
289150
type updateOptions struct {
290151
parallelism uint64
291152
delay time.Duration
@@ -460,7 +321,7 @@ type serviceOptions struct {
460321
workdir string
461322
user string
462323
groups []string
463-
mounts MountOpt
324+
mounts opts.MountOpt
464325

465326
resources resourceOptions
466327
stopGrace DurationOpt

cli/command/service/opts_test.go

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/docker/docker/api/types/container"
9-
mounttypes "github.com/docker/docker/api/types/mount"
109
"github.com/docker/docker/pkg/testutil/assert"
1110
)
1211

@@ -68,151 +67,6 @@ func TestUint64OptSetAndValue(t *testing.T) {
6867
assert.Equal(t, *opt.Value(), uint64(14445))
6968
}
7069

71-
func TestMountOptString(t *testing.T) {
72-
mount := MountOpt{
73-
values: []mounttypes.Mount{
74-
{
75-
Type: mounttypes.TypeBind,
76-
Source: "/home/path",
77-
Target: "/target",
78-
},
79-
{
80-
Type: mounttypes.TypeVolume,
81-
Source: "foo",
82-
Target: "/target/foo",
83-
},
84-
},
85-
}
86-
expected := "bind /home/path /target, volume foo /target/foo"
87-
assert.Equal(t, mount.String(), expected)
88-
}
89-
90-
func TestMountOptSetBindNoErrorBind(t *testing.T) {
91-
for _, testcase := range []string{
92-
// tests several aliases that should have same result.
93-
"type=bind,target=/target,source=/source",
94-
"type=bind,src=/source,dst=/target",
95-
"type=bind,source=/source,dst=/target",
96-
"type=bind,src=/source,target=/target",
97-
} {
98-
var mount MountOpt
99-
100-
assert.NilError(t, mount.Set(testcase))
101-
102-
mounts := mount.Value()
103-
assert.Equal(t, len(mounts), 1)
104-
assert.Equal(t, mounts[0], mounttypes.Mount{
105-
Type: mounttypes.TypeBind,
106-
Source: "/source",
107-
Target: "/target",
108-
})
109-
}
110-
}
111-
112-
func TestMountOptSetVolumeNoError(t *testing.T) {
113-
for _, testcase := range []string{
114-
// tests several aliases that should have same result.
115-
"type=volume,target=/target,source=/source",
116-
"type=volume,src=/source,dst=/target",
117-
"type=volume,source=/source,dst=/target",
118-
"type=volume,src=/source,target=/target",
119-
} {
120-
var mount MountOpt
121-
122-
assert.NilError(t, mount.Set(testcase))
123-
124-
mounts := mount.Value()
125-
assert.Equal(t, len(mounts), 1)
126-
assert.Equal(t, mounts[0], mounttypes.Mount{
127-
Type: mounttypes.TypeVolume,
128-
Source: "/source",
129-
Target: "/target",
130-
})
131-
}
132-
}
133-
134-
// TestMountOptDefaultType ensures that a mount without the type defaults to a
135-
// volume mount.
136-
func TestMountOptDefaultType(t *testing.T) {
137-
var mount MountOpt
138-
assert.NilError(t, mount.Set("target=/target,source=/foo"))
139-
assert.Equal(t, mount.values[0].Type, mounttypes.TypeVolume)
140-
}
141-
142-
func TestMountOptSetErrorNoTarget(t *testing.T) {
143-
var mount MountOpt
144-
assert.Error(t, mount.Set("type=volume,source=/foo"), "target is required")
145-
}
146-
147-
func TestMountOptSetErrorInvalidKey(t *testing.T) {
148-
var mount MountOpt
149-
assert.Error(t, mount.Set("type=volume,bogus=foo"), "unexpected key 'bogus'")
150-
}
151-
152-
func TestMountOptSetErrorInvalidField(t *testing.T) {
153-
var mount MountOpt
154-
assert.Error(t, mount.Set("type=volume,bogus"), "invalid field 'bogus'")
155-
}
156-
157-
func TestMountOptSetErrorInvalidReadOnly(t *testing.T) {
158-
var mount MountOpt
159-
assert.Error(t, mount.Set("type=volume,readonly=no"), "invalid value for readonly: no")
160-
assert.Error(t, mount.Set("type=volume,readonly=invalid"), "invalid value for readonly: invalid")
161-
}
162-
163-
func TestMountOptDefaultEnableReadOnly(t *testing.T) {
164-
var m MountOpt
165-
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo"))
166-
assert.Equal(t, m.values[0].ReadOnly, false)
167-
168-
m = MountOpt{}
169-
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly"))
170-
assert.Equal(t, m.values[0].ReadOnly, true)
171-
172-
m = MountOpt{}
173-
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=1"))
174-
assert.Equal(t, m.values[0].ReadOnly, true)
175-
176-
m = MountOpt{}
177-
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=true"))
178-
assert.Equal(t, m.values[0].ReadOnly, true)
179-
180-
m = MountOpt{}
181-
assert.NilError(t, m.Set("type=bind,target=/foo,source=/foo,readonly=0"))
182-
assert.Equal(t, m.values[0].ReadOnly, false)
183-
}
184-
185-
func TestMountOptVolumeNoCopy(t *testing.T) {
186-
var m MountOpt
187-
assert.NilError(t, m.Set("type=volume,target=/foo,volume-nocopy"))
188-
assert.Equal(t, m.values[0].Source, "")
189-
190-
m = MountOpt{}
191-
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo"))
192-
assert.Equal(t, m.values[0].VolumeOptions == nil, true)
193-
194-
m = MountOpt{}
195-
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=true"))
196-
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
197-
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
198-
199-
m = MountOpt{}
200-
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy"))
201-
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
202-
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
203-
204-
m = MountOpt{}
205-
assert.NilError(t, m.Set("type=volume,target=/foo,source=foo,volume-nocopy=1"))
206-
assert.Equal(t, m.values[0].VolumeOptions != nil, true)
207-
assert.Equal(t, m.values[0].VolumeOptions.NoCopy, true)
208-
}
209-
210-
func TestMountOptTypeConflict(t *testing.T) {
211-
var m MountOpt
212-
assert.Error(t, m.Set("type=bind,target=/foo,source=/foo,volume-nocopy=true"), "cannot mix")
213-
assert.Error(t, m.Set("type=volume,target=/foo,source=/foo,bind-propagation=rprivate"), "cannot mix")
214-
}
215-
21670
func TestHealthCheckOptionsToHealthConfig(t *testing.T) {
21771
dur := time.Second
21872
opt := healthCheckOptions{

cli/command/service/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func removeItems(
404404

405405
func updateMounts(flags *pflag.FlagSet, mounts *[]mounttypes.Mount) {
406406
if flags.Changed(flagMountAdd) {
407-
values := flags.Lookup(flagMountAdd).Value.(*MountOpt).Value()
407+
values := flags.Lookup(flagMountAdd).Value.(*opts.MountOpt).Value()
408408
*mounts = append(*mounts, values...)
409409
}
410410
toRemove := buildToRemoveSet(flags, flagMountRemove)

contrib/completion/bash/docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ _docker_container_run() {
12681268
--memory-swap
12691269
--memory-swappiness
12701270
--memory-reservation
1271+
--mount
12711272
--name
12721273
--network
12731274
--network-alias

contrib/completion/fish/docker.fish

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l link -d 'Add
137137
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -s m -l memory -d 'Memory limit (format: <number>[<unit>], where unit = b, k, m or g)'
138138
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l mac-address -d 'Container MAC address (e.g. 92:d0:c6:0a:29:33)'
139139
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l memory-swap -d "Total memory usage (memory + swap), set '-1' to disable swap (format: <number>[<unit>], where unit = b, k, m or g)"
140+
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l mount -d 'Attach a filesystem mount to the container'
140141
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l name -d 'Assign a name to the container'
141142
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -l net -d 'Set the Network mode for the container'
142143
complete -c docker -A -f -n '__fish_seen_subcommand_from create' -s P -l publish-all -d 'Publish all exposed ports to random ports on the host interfaces'
@@ -328,6 +329,7 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l link -d 'Add li
328329
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -s m -l memory -d 'Memory limit (format: <number>[<unit>], where unit = b, k, m or g)'
329330
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l mac-address -d 'Container MAC address (e.g. 92:d0:c6:0a:29:33)'
330331
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l memory-swap -d "Total memory usage (memory + swap), set '-1' to disable swap (format: <number>[<unit>], where unit = b, k, m or g)"
332+
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l mount -d 'Attach a filesystem mount to the container'
331333
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l name -d 'Assign a name to the container'
332334
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -l net -d 'Set the Network mode for the container'
333335
complete -c docker -A -f -n '__fish_seen_subcommand_from run' -s P -l publish-all -d 'Publish all exposed ports to random ports on the host interfaces'

contrib/completion/zsh/_docker

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ __docker_service_subcommand() {
11011101
"($help)--limit-memory=[Limit Memory]:value: "
11021102
"($help)--log-driver=[Logging driver for service]:logging driver:__docker_log_drivers"
11031103
"($help)*--log-opt=[Logging driver options]:log driver options:__docker_log_options"
1104-
"($help)*--mount=[Attach a mount to the service]:mount: "
1104+
"($help)*--mount=[Attach a filesystem mount to the service]:mount: "
11051105
"($help)*--network=[Network attachments]:network: "
11061106
"($help)--no-healthcheck[Disable any container-specified HEALTHCHECK]"
11071107
"($help)*"{-p=,--publish=}"[Publish a port as a node port]:port: "
@@ -1481,6 +1481,7 @@ __docker_subcommand() {
14811481
"($help)--log-driver=[Default driver for container logs]:logging driver:__docker_log_drivers"
14821482
"($help)*--log-opt=[Log driver specific options]:log driver options:__docker_log_options"
14831483
"($help)--mac-address=[Container MAC address]:MAC address: "
1484+
"($help)*--mount=[Attach a filesystem mount to the container]:mount: "
14841485
"($help)--name=[Container name]:name: "
14851486
"($help)--network=[Connect a container to a network]:network mode:(bridge none container host)"
14861487
"($help)*--network-alias=[Add network-scoped alias for the container]:alias: "

docs/reference/commandline/create.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Options:
7878
--memory-reservation string Memory soft limit
7979
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
8080
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
81+
--mount value Attach a filesytem mount to the container (default [])
8182
--name string Assign a name to the container
8283
--network-alias value Add network-scoped alias for the container (default [])
8384
--network string Connect a container to a network (default "default")

0 commit comments

Comments
 (0)