Browse code

Add --mount syntax documentation to CLI reference

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2016/08/16 06:51:15
Showing 3 changed files
... ...
@@ -240,6 +240,8 @@ binary (refer to [get the linux binary](
240 240
 you give the container the full access to create and manipulate the host's
241 241
 Docker daemon.
242 242
 
243
+For in-depth information about volumes, refer to [manage data in containers](../../tutorials/dockervolumes.md)
244
+
243 245
 ### Publish or expose port (-p, --expose)
244 246
 
245 247
     $ docker run -p 127.0.0.1:80:8080 ubuntu bash
... ...
@@ -137,6 +137,114 @@ $ docker service create \
137 137
 For more information about labels, refer to [apply custom
138 138
 metadata](../../userguide/labels-custom-metadata.md).
139 139
 
140
+### Add volumes or bind-mounts
141
+
142
+The following table describes the options for defining mounts in a service:
143
+
144
+| Option                | Required          | Description
145
+|:----------------------|:------------------|:--------------------------------------------------------------------------------------------------------------------
146
+| **type**              |                   | The type of mount, can be either "volume", or "bind". Defaults to "volume" if no type is specified.<ul><li>`volume`: (default) mounts a [managed volume](volume_create.md) into the container.</li><li>`bind`: bind-mounts a directory or file from the host into the container.</li></ul>
147
+| **src**               | `bind`&nbsp;only  | <ul><li>`type=volume`: Use `src` to specify the name of the volume (e.g., `src=my-volume`). If a volume with the given name does not exist, it is automatically created. If this option is omitted, an ephemeral volume with a random name is generated. Random names are guaranteed to be unique on the host, but may not be unique cluster-wide. Ephemeral volumes have the same lifecycle as the container it is attached to, and are destroyed when the *container* is destroyed (which is upon `service update`, or when scaling or re-balancing the service).</li><li>`type=bind`: Use `src` to specify host-path to bind mount (e.g., `src=/path/on/host/`). When using a bind-mount (`"type=bind"`), the `src` path must be specified as an absolute path, and *must* be a pre-existing path, or an error is produced.</li></ul>
148
+| **source**            |                   | Alias for `src`.
149
+| **dst**               | yes               | Mount path inside the container, for example `/some/path/in/container/`. If the path does not exist in the container's filesystem, the Engine creates a directory at the specified location before mounting the volume or bind-mount.
150
+| **destination**       |                   | Alias for `dst`.
151
+| **target**            |                   | Alias for `dst`.
152
+| **readonly**          |                   | By default, the Engine mounts binds and volumes `read-write`. Pass the `readonly` option to mount the bind or volume `read-only` in the container.<br /><br />A value is optional:<ul><li>`true` or `1`: Default if you do not provide a value. Mounts the bind or volume read-only in the container.</li><li>`false` or `0`: Mounts the bind or volume read-write in the container.</li></ul>
153
+| **ro**                |                   | Alias for `readonly`.
154
+
155
+The following options can only be used for bind-mounts (`type=bind`);
156
+
157
+
158
+| Option                | Description
159
+|:----------------------|:--------------------------------------------------------------------------------------------------------------------
160
+| **bind-propagation**  | Bind propagation options to set on the mount at runtime. Valid options are `shared`, `slave`, `private`, `rshared`, `rslave`, and `rprivate`. Defaults to `rprivate` if not specified. For volumes, bind propagation is not configurable, and also defaults to `rprivate`.
161
+
162
+
163
+The following options can only be used for named volumes (`type=volume`);
164
+
165
+| Option                | Description
166
+|:----------------------|:--------------------------------------------------------------------------------------------------------------------
167
+| **volume-driver**     | Name of the volume-driver plugin to use for the volume. Defaults to the ``"local"`` volume driver to create the volume if it does not exist.
168
+| **volume-label**      | Custom metadata ("labels") to apply to the volume upon creation. Labels are provided as comma-separated list of key/value pairs, for example, `volume-label=hello=world`. For more information about labels, refer to [apply custom metadata](../../userguide/labels-custom-metadata.md).
169
+| **volume-nocopy**     | By default, if you attach an empty volume to a container, the Engine propagates the files and directories that are present at the mount-path (`dst`) inside the container into the volume. Set `volume-nocopy` to disables copying files from the container's filesystem to the volume and mount the empty volume.<br /><br />A value is optional:<ul><li>`true` or `1`: Default if you do not provide a value. Disables copying.</li><li>`false` or `0`: Enables copying.</li></ul>
170
+| **volume-opt**        | Volume driver-specific options to use when creating the volume. Options are provided as comma-separated list of key/value pairs, for example, `volume-opt=some-option=some-value,some-other-option=some-other-value`. For available options, refer to the documentation of the volume driver that is used.
171
+
172
+#### Differences between "--mount" and "--volume"
173
+
174
+The `--mount` flag features most options that are supported by the `-v` /
175
+`--volume` flag for `docker run`. There are some differences;
176
+
177
+- The `--mount` flag allows specifying a volume driver, and volume driver
178
+  options *per volume*, without having to create volumes in advance. When using
179
+  `docker run`, only a single volume driver can be specified (using the
180
+  `--volume-driver` flag), which is shared by all volumes.
181
+- The `--mount` flag allows specifying custom metadata ("labels") for the volume,
182
+  without having to create the volume out of band.
183
+- When using `type=bind`, the host-path must refer to an *existing* path on the
184
+  host, and is not automatically created if the path does not exist. If the
185
+  specified path does not exist on the host, an error is produced, and the
186
+  service will fail to be deployed succesfully.
187
+- The `--mount` flag does not allow you to relabel volumes with `Z` or `z`
188
+
189
+#### Create a service using a named volume
190
+
191
+The following example creates a service that uses a named volume:
192
+
193
+```bash
194
+$ docker service create \
195
+  --name my-service \
196
+  --replicas 3 \
197
+  --mount type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round" \
198
+  nginx:alpine
199
+```
200
+
201
+For each replica of the service, the engine requests a volume named "my-volume"
202
+from the default ("local") volume driver where the task is deployed. If the
203
+volume does not exist, the engine creates a new volume and applies the "color"
204
+and "shape" labels.
205
+
206
+When the task is started, the volume is mounted on `/path/in/container/` inside
207
+the container.
208
+
209
+Be aware that the default ("local") volume is a locally scoped volume driver.
210
+This means that depending on where a task is deployed, either that task gets a
211
+*new* volume named "my-volume", or shares the same "my-volume" with other tasks
212
+of the same service. Multiple containers writing to a single shared volume can
213
+cause data corruption if the software running inside the container is not
214
+designed to handle concurrent processes writing to the same location. Also take
215
+into account that containers can be re-scheduled by the Swarm orchestrator and
216
+be deployed on a different node.
217
+
218
+#### Create a service that uses an anonymous (ephemeral) volume
219
+
220
+The following command creates a service with three replicas with an anonymous 
221
+volume on `/path/in/container`:
222
+
223
+```bash
224
+$ docker service create \
225
+  --name my-service \
226
+  --replicas 3 \
227
+  --mount type=volume,destination=/path/in/container \
228
+  nginx:alpine
229
+```
230
+
231
+In this example, no name (`source`) is specified for the volume, hence a new,
232
+*randomly named* volume is created for each task. This guarantees that each task
233
+gets its own volume, and volumes are not shared between tasks. Unnamed volumes
234
+are considered "ephemeral", and are destroyed when the container is destroyed.
235
+
236
+#### Create a service that uses a bind-mounted host directory
237
+
238
+The following example bind-mounts a host directory at `/path/in/container` in
239
+the containers backing the service:
240
+
241
+```bash
242
+$ docker service create \
243
+  --name my-service \
244
+  --mount type=bind,source=/path/on/host,destination=/path/in/container \
245
+  nginx:alpine
246
+```
247
+
140 248
 ### Set service mode (--mode)
141 249
 
142 250
 You can set the service mode to "replicated" (default) or to "global". A
... ...
@@ -159,13 +267,13 @@ constraint expressions. Multiple constraints find nodes that satisfy every
159 159
 expression (AND match). Constraints can match node or Docker Engine labels as
160 160
 follows:
161 161
 
162
-| node attribute | matches | example |
163
-|:------------- |:-------------| :---------------------------------------------|
164
-| node.id | node ID | `node.id == 2ivku8v2gvtg4`                               |
165
-| node.hostname | node hostname | `node.hostname != node-2`                    |
166
-| node.role | node role: manager | `node.role == manager`                      |
167
-| node.labels | user defined node labels | `node.labels.security == high`      |
168
-| engine.labels | Docker Engine's labels | `engine.labels.operatingsystem == ubuntu 14.04`|
162
+| node attribute  | matches                   | example                                         |
163
+|:----------------|:--------------------------|:------------------------------------------------|
164
+| node.id         | node ID                   | `node.id == 2ivku8v2gvtg4`                      |
165
+| node.hostname   | node hostname             | `node.hostname != node-2`                       |
166
+| node.role       | node role: manager        | `node.role == manager`                          |
167
+| node.labels     | user defined node labels  | `node.labels.security == high`                  |
168
+| engine.labels   | Docker Engine's labels    | `engine.labels.operatingsystem == ubuntu 14.04` |
169 169
 
170 170
 `engine.labels` apply to Docker Engine labels like operating system,
171 171
 drivers, etc. Swarm administrators add `node.labels` for operational purposes by
... ...
@@ -240,3 +348,6 @@ the service running on the node. For more information refer to
240 240
 * [service scale](service_scale.md)
241 241
 * [service ps](service_ps.md)
242 242
 * [service update](service_update.md)
243
+
244
+<style>table tr > td:first-child { white-space: nowrap;}</style>
245
+
... ...
@@ -67,6 +67,35 @@ for further information.
67 67
 $ docker service update --limit-cpu 2 redis
68 68
 ```
69 69
 
70
+### Adding and removing mounts
71
+
72
+You can add, or remove bind-mounts or volumes to a service using the
73
+`--mount-add` and `--mount-rm` options.
74
+
75
+The following example creates a service using the `test-data` volume, then
76
+updates the service to mount another volume, and finally unmounts the first
77
+volume:
78
+
79
+```bash
80
+$ docker service create --name=myservice --mount type=volume,source=test-data,target=/somewhere nginx:alpine
81
+
82
+myservice
83
+
84
+$ docker service update --mount-add type=volume,source=other-volume,target=/somewhere-else myservice
85
+
86
+myservice
87
+
88
+$ docker service update --mount-rm /somewhere myservice
89
+
90
+myservice
91
+```
92
+
93
+The `--mount-rm` flag takes the `target` path of the mount. The `--mount-add`
94
+flag takes the same parameters as the `--mount` flag on `service create`. Refer
95
+to the [volumes and bind-mounts](service_create.md#volumes-and-bind-mounts-mount) section in the
96
+`service create` reference for details.
97
+
98
+
70 99
 ## Related information
71 100
 
72 101
 * [service create](service_create.md)