Skip to content

Commit 259aa90

Browse files
committed
docs: rewrite section on filesystem mounts
Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
1 parent 52716c8 commit 259aa90

1 file changed

Lines changed: 76 additions & 46 deletions

File tree

docs/reference/run.md

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,82 @@ round-trip min/avg/max = 0.257/0.288/0.326 ms
189189
For more information about container networking, see [Networking
190190
overview](https://docs.docker.com/network/)
191191

192+
## Filesystem mounts
193+
194+
By default, the data in a container is stored in an ephemeral, writable
195+
container layer. Removing the container also removes its data. If you want to
196+
use persistent data with containers, you can use filesystem mounts to store the
197+
data persistently on the host system. Filesystem mounts can also let you share
198+
data between containers and the host.
199+
200+
Docker supports two main categories of mounts:
201+
202+
- Volume mounts
203+
- Bind mounts
204+
205+
Volume mounts are great for persistently storing data for containers, and for
206+
sharing data between containers. Bind mounts, on the other hand, are for
207+
sharing data between a container and the host.
208+
209+
You can add a filesystem mount to a container using the `--mount` flag for the
210+
`docker run` command.
211+
212+
The following sections show basic examples of how to create volumes and bind
213+
mounts. For more in-depth examples and descriptions, refer to the section of
214+
the [storage section](https://docs.docker.com/storage/) in the documentation.
215+
216+
### Volume mounts
217+
218+
To create a volume mount:
219+
220+
```console
221+
$ docker run --mount source=<VOLUME_NAME>,target=[PATH] [IMAGE] [COMMAND...]
222+
```
223+
224+
The `--mount` flag takes two parameters in this case: `source` and `target`.
225+
The value for the `source` parameter is the name of the volume. The value of
226+
`target` is the mount location of the volume inside the container. Once you've
227+
created the volume, any data you write to the volume is persisted, even if you
228+
stop or remove the container:
229+
230+
```console
231+
$ docker run --rm --mount source=my_volume,target=/foo busybox \
232+
echo "hello, volume!" > /foo/hello.txt
233+
$ docker run --mount source=my_volume,target=/bar busybox
234+
cat /bar/hello.txt
235+
hello, volume!
236+
```
237+
238+
The `target` must always be an absolute path, such as `/src/docs`. An absolute
239+
path starts with a `/` (forward slash). Volume names must start with an
240+
alphanumeric character, followed by `a-z0-9`, `_` (underscore), `.` (period) or
241+
`-` (hyphen).
242+
243+
### Bind mounts
244+
245+
To create a bind mount:
246+
247+
```console
248+
$ docker run -it --mount type=bind,source=[PATH],target=[PATH] busybox
249+
```
250+
251+
In this case, the `--mount` flag takes three parameters. A type (`bind`), and
252+
two paths. The `source` path is a the location on the host that you want to
253+
bind mount into the container. The `target` path is the mount destination
254+
inside the container.
255+
256+
Bind mounts are read-write by default, meaning that you can both read and write
257+
files to and from the mounted location from the container. Changes that you
258+
make, such as adding or editing files, are reflected on the host filesystem:
259+
260+
```console
261+
$ docker run -it --mount type=bind,source=.,target=/foo busybox
262+
/ # echo "hello from container" > /foo/hello.txt
263+
/ # exit
264+
$ cat hello.txt
265+
hello from container
266+
```
267+
192268
## Exit status
193269

194270
The exit code from `docker run` gives information about why the container
@@ -900,7 +976,6 @@ override those defaults using flags for the `docker run` command.
900976
- [Expose ports](#exposed-ports)
901977
- [Environment variables](#environment-variables)
902978
- [Healthcheck](#healthchecks)
903-
- [Filesystem mounts](#filesystem-mounts)
904979
- [User](#user)
905980
- [Working directory](#working-directory)
906981

@@ -1130,51 +1205,6 @@ $ sleep 2; docker inspect --format='{{json .State.Health}}' test
11301205

11311206
The health status is also displayed in the `docker ps` output.
11321207

1133-
### Filesystem mounts
1134-
1135-
-v, --volume=[host-src:]container-dest[:<options>]: Bind mount a volume.
1136-
The comma-delimited `options` are [rw|ro], [z|Z],
1137-
[[r]shared|[r]slave|[r]private], and [nocopy].
1138-
The 'host-src' is an absolute path or a name value.
1139-
1140-
If neither 'rw' or 'ro' is specified then the volume is mounted in
1141-
read-write mode.
1142-
1143-
The `nocopy` mode is used to disable automatically copying the requested volume
1144-
path in the container to the volume storage location.
1145-
For named volumes, `copy` is the default mode. Copy modes are not supported
1146-
for bind-mounted volumes.
1147-
1148-
--volumes-from="": Mount all volumes from the given container(s)
1149-
1150-
> **Note**
1151-
>
1152-
> When using systemd to manage the Docker daemon's start and stop, in the systemd
1153-
> unit file there is an option to control mount propagation for the Docker daemon
1154-
> itself, called `MountFlags`. The value of this setting may cause Docker to not
1155-
> see mount propagation changes made on the mount point. For example, if this value
1156-
> is `slave`, you may not be able to use the `shared` or `rshared` propagation on
1157-
> a volume.
1158-
1159-
The volumes commands are complex enough to have their own documentation
1160-
in section [*Use volumes*](https://docs.docker.com/storage/volumes/). A developer can define
1161-
one or more `VOLUME`'s associated with an image, but only the operator
1162-
can give access from one container to another (or from a container to a
1163-
volume mounted on the host).
1164-
1165-
The `container-dest` must always be an absolute path such as `/src/docs`.
1166-
The `host-src` can either be an absolute path or a `name` value. If you
1167-
supply an absolute path for the `host-src`, Docker bind-mounts to the path
1168-
you specify. If you supply a `name`, Docker creates a named volume by that `name`.
1169-
1170-
A `name` value must start with an alphanumeric character,
1171-
followed by `a-z0-9`, `_` (underscore), `.` (period) or `-` (hyphen).
1172-
An absolute path starts with a `/` (forward slash).
1173-
1174-
For example, you can specify either `/foo` or `foo` for a `host-src` value.
1175-
If you supply the `/foo` value, Docker creates a bind mount. If you supply
1176-
the `foo` specification, Docker creates a named volume.
1177-
11781208
### User
11791209

11801210
`root` (id = 0) is the default user within a container. The image developer can

0 commit comments

Comments
 (0)