Skip to content

Commit de9ff4b

Browse files
committed
Merge pull request moby#21223 from cpuguy83/add_nocp_to_vols
Add explicit flags for volume cp/no-cp
2 parents 0dcdd1c + b0ac69b commit de9ff4b

File tree

14 files changed

+123
-28
lines changed

14 files changed

+123
-28
lines changed

container/container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ func (container *Container) AddMountPointWithVolume(destination string, vol volu
552552
Destination: destination,
553553
RW: rw,
554554
Volume: vol,
555+
CopyData: volume.DefaultCopyMode,
555556
}
556557
}
557558

container/container_unix.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,8 @@ func (container *Container) CopyImagePathContent(v volume.Volume, destination st
185185
if err != nil {
186186
return err
187187
}
188-
189-
if err := copyExistingContents(rootfs, path); err != nil {
190-
return err
191-
}
192-
193-
return v.Unmount()
188+
defer v.Unmount()
189+
return copyExistingContents(rootfs, path)
194190
}
195191

196192
// ShmResourcePath returns path to shm

daemon/create_unix.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ func (daemon *Daemon) createContainerPlatformSpecificSettings(container *contain
6363
// this is only called when the container is created.
6464
func (daemon *Daemon) populateVolumes(c *container.Container) error {
6565
for _, mnt := range c.MountPoints {
66-
// skip binds and volumes referenced by other containers (ie, volumes-from)
67-
if mnt.Driver == "" || mnt.Volume == nil || len(daemon.volumes.Refs(mnt.Volume)) > 1 {
66+
if !mnt.CopyData || mnt.Volume == nil {
6867
continue
6968
}
7069

daemon/volumes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func (daemon *Daemon) registerMountPoints(container *container.Container, hostCo
131131
bind = setBindModeIfNull(bind)
132132
}
133133
}
134+
134135
if label.RelabelNeeded(bind.Mode) {
135136
if err := label.Relabel(bind.Source, container.MountLabel, label.IsShared(bind.Mode)); err != nil {
136137
return err

docs/reference/api/docker_remote_api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ This section lists each version from latest to oldest. Each listing includes a
126126
* `POST /containers/create` now takes `PidsLimit` field, if the kernel is >= 4.3 and the pids cgroup is supported.
127127
* `GET /containers/(id or name)/stats` now returns `pids_stats`, if the kernel is >= 4.3 and the pids cgroup is supported.
128128
* `POST /containers/create` now allows you to override usernamespaces remapping and use privileged options for the container.
129+
* `POST /containers/create` now allows specifying `nocopy` for named volumes, which disables automatic copying from the container path to the volume.
129130
* `POST /auth` now returns an `IdentityToken` when supported by a registry.
130131
* `POST /containers/create` with both `Hostname` and `Domainname` fields specified will result in the container's hostname being set to `Hostname`, rather than `Hostname.Domainname`.
131132

docs/reference/commandline/create.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ Creates a new container.
9090
--uts="" UTS namespace to use
9191
-v, --volume=[host-src:]container-dest[:<options>]
9292
Bind mount a volume. The comma-delimited
93-
`options` are [rw|ro], [z|Z], or
94-
[[r]shared|[r]slave|[r]private]. The
95-
'host-src' is an absolute path or a name
96-
value.
93+
`options` are [rw|ro], [z|Z],
94+
[[r]shared|[r]slave|[r]private], and
95+
[nocopy]. The 'host-src' is an absolute path
96+
or a name value.
9797
--volume-driver="" Container's volume driver
9898
--volumes-from=[] Mount volumes from the specified container(s)
9999
-w, --workdir="" Working directory inside the container

docs/reference/commandline/run.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ parent = "smn_cli"
9292
--uts="" UTS namespace to use
9393
-v, --volume=[host-src:]container-dest[:<options>]
9494
Bind mount a volume. The comma-delimited
95-
`options` are [rw|ro], [z|Z], or
96-
[[r]shared|[r]slave|[r]private]. The
97-
'host-src' is an absolute path or a name
98-
value.
95+
`options` are [rw|ro], [z|Z],
96+
[[r]shared|[r]slave|[r]private], and
97+
[nocopy]. The 'host-src' is an absolute path
98+
or a name value.
9999
--volume-driver="" Container's volume driver
100100
--volumes-from=[] Mount volumes from the specified container(s)
101101
-w, --workdir="" Working directory inside the container

docs/reference/run.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,13 +1400,18 @@ The example below mounts an empty tmpfs into the container with the `rw`,
14001400
### VOLUME (shared filesystems)
14011401

14021402
-v, --volume=[host-src:]container-dest[:<options>]: Bind mount a volume.
1403-
The comma-delimited `options` are [rw|ro], [z|Z], or
1404-
[[r]shared|[r]slave|[r]private]. The 'host-src' is an absolute path or a
1405-
name value.
1403+
The comma-delimited `options` are [rw|ro], [z|Z],
1404+
[[r]shared|[r]slave|[r]private], and [nocopy].
1405+
The 'host-src' is an absolute path or a name value.
14061406

14071407
If neither 'rw' or 'ro' is specified then the volume is mounted in
14081408
read-write mode.
14091409

1410+
The `nocopy` modes is used to disable automatic copying requested volume
1411+
path in the container to the volume storage location.
1412+
For named volumes, `copy` is the default mode. Copy modes are not supported
1413+
for bind-mounted volumes.
1414+
14101415
--volumes-from="": Mount all volumes from the given container(s)
14111416

14121417
> **Note**:

integration-cli/docker_cli_run_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4249,6 +4249,44 @@ func (s *DockerSuite) TestRunVolumeWithOneCharacter(c *check.C) {
42494249
testRequires(c, DaemonIsLinux)
42504250

42514251
out, _ := dockerCmd(c, "run", "-v", "/tmp/q:/foo", "busybox", "sh", "-c", "find /foo")
4252-
fmt.Printf("OUTPUT: %+v", out)
42534252
c.Assert(strings.TrimSpace(out), checker.Equals, "/foo")
42544253
}
4254+
4255+
func (s *DockerSuite) TestRunVolumeCopyFlag(c *check.C) {
4256+
testRequires(c, DaemonIsLinux) // Windows does not support copying data from image to the volume
4257+
_, err := buildImage("volumecopy",
4258+
`FROM busybox
4259+
RUN mkdir /foo && echo hello > /foo/bar
4260+
CMD cat /foo/bar`,
4261+
true,
4262+
)
4263+
c.Assert(err, checker.IsNil)
4264+
4265+
dockerCmd(c, "volume", "create", "--name=test")
4266+
4267+
// test with the nocopy flag
4268+
out, _, err := dockerCmdWithError("run", "-v", "test:/foo:nocopy", "volumecopy")
4269+
c.Assert(err, checker.NotNil, check.Commentf(out))
4270+
// test default behavior which is to copy for non-binds
4271+
out, _ = dockerCmd(c, "run", "-v", "test:/foo", "volumecopy")
4272+
c.Assert(strings.TrimSpace(out), checker.Equals, "hello")
4273+
// error out when the volume is already populated
4274+
out, _, err = dockerCmdWithError("run", "-v", "test:/foo:copy", "volumecopy")
4275+
c.Assert(err, checker.NotNil, check.Commentf(out))
4276+
// do not error out when copy isn't explicitly set even though it's already populated
4277+
out, _ = dockerCmd(c, "run", "-v", "test:/foo", "volumecopy")
4278+
c.Assert(strings.TrimSpace(out), checker.Equals, "hello")
4279+
4280+
// do not allow copy modes on volumes-from
4281+
dockerCmd(c, "run", "--name=test", "-v", "/foo", "busybox", "true")
4282+
out, _, err = dockerCmdWithError("run", "--volumes-from=test:copy", "busybox", "true")
4283+
c.Assert(err, checker.NotNil, check.Commentf(out))
4284+
out, _, err = dockerCmdWithError("run", "--volumes-from=test:nocopy", "busybox", "true")
4285+
c.Assert(err, checker.NotNil, check.Commentf(out))
4286+
4287+
// do not allow copy modes on binds
4288+
out, _, err = dockerCmdWithError("run", "-v", "/foo:/bar:copy", "busybox", "true")
4289+
c.Assert(err, checker.NotNil, check.Commentf(out))
4290+
out, _, err = dockerCmdWithError("run", "-v", "/foo:/bar:nocopy", "busybox", "true")
4291+
c.Assert(err, checker.NotNil, check.Commentf(out))
4292+
}

man/docker-create.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ change propagation properties of source mount. Say `/` is source mount for
434434
> is `slave`, you may not be able to use the `shared` or `rshared` propagation on
435435
> a volume.
436436
437+
438+
To disable automatic copying of data from the container path to the volume, use
439+
the `nocopy` flag. The `nocopy` flag can be set on bind mounts and named volumes.
440+
437441
**--volume-driver**=""
438442
Container's volume driver. This driver creates volumes specified either from
439443
a Dockerfile's `VOLUME` instruction or from the `docker run -v` flag.

0 commit comments

Comments
 (0)