Skip to content

Commit ef82308

Browse files
committed
engine: volume subpath mount
Signed-off-by: David Karlsson <[email protected]>
1 parent 8e1a75c commit ef82308

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

content/engine/storage/volumes.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ If you need to specify volume driver options, you must use `--mount`.
7474
- The `destination` takes as its value the path where the file or directory
7575
is mounted in the container. Can be specified as `destination`, `dst`,
7676
or `target`.
77+
- The `volume-subpath` option takes a path to a subdirectory within the
78+
volume to mount into the container. The subdirectory must exist in the
79+
volume before the volume is mounted to a container.
80+
See [Mount a volume subdirectory](#mount-a-volume-subdirectory).
7781
- The `readonly` option, if present, causes the bind mount to be [mounted into
7882
the container as read-only](#use-a-read-only-volume). Can be specified as `readonly` or `ro`.
7983
- The `volume-opt` option, which can be specified more than once, takes a
@@ -404,6 +408,43 @@ $ docker container rm nginxtest
404408
$ docker volume rm nginx-vol
405409
```
406410

411+
## Mount a volume subdirectory
412+
413+
When you mount a volume to a container, you can specify a subdirectory of the
414+
volume to use, with the `volume-subpath` parameter for the `--mount` flag. The
415+
subdirectory that you specify must exist in the volume before you attempt to
416+
mount it into a container; if it doesn't exist, the mount fails.
417+
418+
Specifying `volume-subpath` is useful if you only want to share a specific
419+
portion of a volume with a container. Say for example that you have multiple
420+
containers running and you want to store logs from each container in a shared
421+
volume. You can create a subdirectory for each container in the shared volume,
422+
and mount the subdirectory to the container.
423+
424+
The following example creates a `logs` volume and initiates the subdirectories
425+
`app1` and `app2` in the volume. It then starts two containers and mounts one
426+
of the subdirectories of the `logs` volume to each container. This example
427+
assumes that the processes in the containers write their logs to
428+
`/var/log/app1` and `/var/log/app2`.
429+
430+
```console
431+
$ docker volume create logs
432+
$ docker run --rm \
433+
--mount src=logs,dst=/logs \
434+
alpine mkdir -p /logs/app1 /logs/app2
435+
$ docker run -d \
436+
--name=app1 \
437+
--mount src=logs,dst=/var/log/app1/,volume-subpath=app1 \
438+
app1:latest
439+
$ docker run -d \
440+
--name=app2 \
441+
--mount src=logs,dst=/var/log/app2,volume-subpath=app2 \
442+
app2:latest
443+
```
444+
445+
With this setup, the containers write their logs to separate subdirectories of
446+
the `logs` volume. The containers can't access the other container's logs.
447+
407448
## Share data between machines
408449

409450
When building fault-tolerant applications, you may need to configure multiple

0 commit comments

Comments
 (0)