github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/docs/reference/commandline/service_create.md (about) 1 --- 2 title: "service create" 3 description: "The service create command description and usage" 4 keywords: ["service, create"] 5 --- 6 7 <!-- This file is maintained within the docker/docker Github 8 repository at https://github.com/docker/docker/. Make all 9 pull requests against that repo. If you see this file in 10 another repository, consider it read-only there, as it will 11 periodically be overwritten by the definitive file. Pull 12 requests which include edits to this file in other repositories 13 will be rejected. 14 --> 15 16 # service create 17 18 ```Markdown 19 Usage: docker service create [OPTIONS] IMAGE [COMMAND] [ARG...] 20 21 Create a new service 22 23 Options: 24 --constraint value Placement constraints (default []) 25 --container-label value Service container labels (default []) 26 --endpoint-mode string Endpoint mode (vip or dnsrr) 27 -e, --env value Set environment variables (default []) 28 --env-file value Read in a file of environment variables (default []) 29 --group value Set one or more supplementary user groups for the container (default []) 30 --health-cmd string Command to run to check health 31 --health-interval duration Time between running the check 32 --health-retries int Consecutive failures needed to report unhealthy 33 --health-timeout duration Maximum time to allow one check to run 34 --help Print usage 35 -l, --label value Service labels (default []) 36 --limit-cpu value Limit CPUs (default 0.000) 37 --limit-memory value Limit Memory (default 0 B) 38 --log-driver string Logging driver for service 39 --log-opt value Logging driver options (default []) 40 --mode string Service mode (replicated or global) (default "replicated") 41 --mount value Attach a mount to the service 42 --name string Service name 43 --network value Network attachments (default []) 44 --no-healthcheck Disable any container-specified HEALTHCHECK 45 -p, --publish value Publish a port as a node port (default []) 46 --replicas value Number of tasks (default none) 47 --reserve-cpu value Reserve CPUs (default 0.000) 48 --reserve-memory value Reserve Memory (default 0 B) 49 --restart-condition string Restart when condition is met (none, on-failure, or any) 50 --restart-delay value Delay between restart attempts (default none) 51 --restart-max-attempts value Maximum number of restarts before giving up (default none) 52 --restart-window value Window used to evaluate the restart policy (default none) 53 --stop-grace-period value Time to wait before force killing a container (default none) 54 --update-delay duration Delay between updates 55 --update-failure-action string Action on update failure (pause|continue) (default "pause") 56 --update-max-failure-ratio value Failure rate to tolerate during an update 57 --update-monitor duration Duration after each task update to monitor for failure (default 0s) 58 --update-parallelism uint Maximum number of tasks updated simultaneously (0 to update all at once) (default 1) 59 -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) 60 --with-registry-auth Send registry authentication details to Swarm agents 61 -w, --workdir string Working directory inside the container 62 ``` 63 64 Creates a service as described by the specified parameters. You must run this 65 command on a manager node. 66 67 ## Examples 68 69 ### Create a service 70 71 ```bash 72 $ docker service create --name redis redis:3.0.6 73 dmu1ept4cxcfe8k8lhtux3ro3 74 75 $ docker service ls 76 ID NAME REPLICAS IMAGE COMMAND 77 dmu1ept4cxcf redis 1/1 redis:3.0.6 78 ``` 79 80 ### Create a service with 5 replica tasks (--replicas) 81 82 Use the `--replicas` flag to set the number of replica tasks for a replicated 83 service. The following command creates a `redis` service with `5` replica tasks: 84 85 ```bash 86 $ docker service create --name redis --replicas=5 redis:3.0.6 87 4cdgfyky7ozwh3htjfw0d12qv 88 ``` 89 90 The above command sets the *desired* number of tasks for the service. Even 91 though the command returns immediately, actual scaling of the service may take 92 some time. The `REPLICAS` column shows both the *actual* and *desired* number 93 of replica tasks for the service. 94 95 In the following example the desired state is `5` replicas, but the current 96 number of `RUNNING` tasks is `3`: 97 98 ```bash 99 $ docker service ls 100 ID NAME REPLICAS IMAGE COMMAND 101 4cdgfyky7ozw redis 3/5 redis:3.0.7 102 ``` 103 104 Once all the tasks are created and `RUNNING`, the actual number of tasks is 105 equal to the desired number: 106 107 ```bash 108 $ docker service ls 109 ID NAME REPLICAS IMAGE COMMAND 110 4cdgfyky7ozw redis 5/5 redis:3.0.7 111 ``` 112 113 ### Create a service with a rolling update policy 114 115 ```bash 116 $ docker service create \ 117 --replicas 10 \ 118 --name redis \ 119 --update-delay 10s \ 120 --update-parallelism 2 \ 121 redis:3.0.6 122 ``` 123 124 When you run a [service update](service_update.md), the scheduler updates a 125 maximum of 2 tasks at a time, with `10s` between updates. For more information, 126 refer to the [rolling updates 127 tutorial](https://docs.docker.com/engine/swarm/swarm-tutorial/rolling-update/). 128 129 ### Set environment variables (-e, --env) 130 131 This sets environmental variables for all tasks in a service. For example: 132 133 ```bash 134 $ docker service create --name redis_2 --replicas 5 --env MYVAR=foo redis:3.0.6 135 ``` 136 137 ### Set metadata on a service (-l, --label) 138 139 A label is a `key=value` pair that applies metadata to a service. To label a 140 service with two labels: 141 142 ```bash 143 $ docker service create \ 144 --name redis_2 \ 145 --label com.example.foo="bar" 146 --label bar=baz \ 147 redis:3.0.6 148 ``` 149 150 For more information about labels, refer to [apply custom 151 metadata](https://docs.docker.com/engine/userguide/labels-custom-metadata/). 152 153 ### Add bind-mounts or volumes 154 155 Docker supports two different kinds of mounts, which allow containers to read to 156 or write from files or directories on other containers or the host operating 157 system. These types are _data volumes_ (often referred to simply as volumes) and 158 _bind-mounts_. 159 160 A **bind-mount** makes a file or directory on the host available to the 161 container it is mounted within. A bind-mount may be either read-only or 162 read-write. For example, a container might share its host's DNS information by 163 means of a bind-mount of the host's `/etc/resolv.conf` or a container might 164 write logs to its host's `/var/log/myContainerLogs` directory. If you use 165 bind-mounts and your host and containers have different notions of permissions, 166 access controls, or other such details, you will run into portability issues. 167 168 A **named volume** is a mechanism for decoupling persistent data needed by your 169 container from the image used to create the container and from the host machine. 170 Named volumes are created and managed by Docker, and a named volume persists 171 even when no container is currently using it. Data in named volumes can be 172 shared between a container and the host machine, as well as between multiple 173 containers. Docker uses a _volume driver_ to create, manage, and mount volumes. 174 You can back up or restore volumes using Docker commands. 175 176 Consider a situation where your image starts a lightweight web server. You could 177 use that image as a base image, copy in your website's HTML files, and package 178 that into another image. Each time your website changed, you'd need to update 179 the new image and redeploy all of the containers serving your website. A better 180 solution is to store the website in a named volume which is attached to each of 181 your web server containers when they start. To update the website, you just 182 update the named volume. 183 184 For more information about named volumes, see 185 [Data Volumes](https://docs.docker.com/engine/tutorials/dockervolumes/). 186 187 The following table describes options which apply to both bind-mounts and named 188 volumes in a service: 189 190 | Option | Required | Description 191 |:-----------------------------------------|:--------------------------|:----------------------------------------------------------------------------------------- 192 | **type** | | The type of mount, can be either `volume`, or `bind`. Defaults to `volume` if no type is specified.<ul><li>`volume`: 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> 193 | **src** or **source** | for `type=bind` only | <ul><li>`type=volume`: `src` is an optional way to specify the name of the volume (for example, `src=my-volume`). If the named volume does not exist, it is automatically created. If no `src` is specified, the volume is assigned a random name which is guaranteed to be unique on the host, but may not be unique cluster-wide. A randomly-named volume has the same lifecycle as its container and is destroyed when the *container* is destroyed (which is upon `service update`, or when scaling or re-balancing the service).</li><li>`type=bind`: `src` is required, and specifies an absolute path to the file or directory to bind-mount (for example, `src=/path/on/host/`). An error is produced if the file or directory does not exist.</li></ul> 194 | **dst** or **destination** or **target** | 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. 195 | **readonly** or **ro** | | The Engine mounts binds and volumes `read-write` unless `readonly` option is given when mounting the bind or volume.<br /><br /><ul><li>`true` or `1` or no value: Mounts the bind or volume read-only.</li><li>`false` or `0`: Mounts the bind or volume read-write.</li></ul> 196 197 #### Bind Propagation 198 199 Bind propagation refers to whether or not mounts created within a given 200 bind-mount or named volume can be propagated to replicas of that mount. Consider 201 a mount point `/mnt`, which is also mounted on `/tmp`. The propation settings 202 control whether a mount on `/tmp/a` would also be available on `/mnt/a`. Each 203 propagation setting has a recursive counterpoint. In the case of recursion, 204 consider that `/tmp/a` is also mounted as `/foo`. The propagation settings 205 control whether `/mnt/a` and/or `/tmp/a` would exist. 206 207 The `bind-propagation` option defaults to `rprivate` for both bind-mounts and 208 volume mounts, and is only configurable for bind-mounts. In other words, named 209 volumes do not support bind propagation. 210 211 - **`shared`**: Sub-mounts of the original mount are exposed to replica mounts, 212 and sub-mounts of replica mounts are also propagated to the 213 original mount. 214 - **`slave`**: similar to a shared mount, but only in one direction. If the 215 original mount exposes a sub-mount, the replica mount can see it. 216 However, if the replica mount exposes a sub-mount, the original 217 mount cannot see it. 218 - **`private`**: The mount is private. Sub-mounts within it are not exposed to 219 replica mounts, and sub-mounts of replica mounts are not 220 exposed to the original mount. 221 - **`rshared`**: The same as shared, but the propagation also extends to and from 222 mount points nested within any of the original or replica mount 223 points. 224 - **`rslave`**: The same as `slave`, but the propagation also extends to and from 225 mount points nested within any of the original or replica mount 226 points. 227 - **`rprivate`**: The default. The same as `private`, meaning that no mount points 228 anywhere within the original or replica mount points propagate 229 in either direction. 230 231 For more information about bind propagation, see the 232 [Linux kernel documentation for shared subtree](https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt). 233 234 #### Options for Named Volumes 235 The following options can only be used for named volumes (`type=volume`); 236 237 | Option | Description 238 |:----------------------|:-------------------------------------------------------------------------------------------------------------------- 239 | **volume-driver** | Name of the volume-driver plugin to use for the volume. Defaults to ``"local"``, to use the local volume driver to create the volume if the volume does not exist. 240 | **volume-label** | One or more custom metadata ("labels") to apply to the volume upon creation. For example, `volume-label=mylabel=hello-world,my-other-label=hello-mars`. For more information about labels, refer to [apply custom metadata](https://docs.docker.com/engine/userguide/labels-custom-metadata/). 241 | **volume-nocopy** | By default, if you attach an empty volume to a container, and files or directories already existed at the mount-path in the container (`dst`), the Engine copies those files and directories into the volume, allowing the host to access them. 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> 242 | **volume-opt** | Options specific to a given volume driver, which will be passed to the driver when creating the volume. Options are provided as a comma-separated list of key/value pairs, for example, `volume-opt=some-option=some-value,some-other-option=some-other-value`. For available options for a given driver, refer to that driver's documentation. 243 244 #### Differences between "--mount" and "--volume" 245 246 The `--mount` flag supports most options that are supported by the `-v` 247 or `--volume` flag for `docker run`, with some important exceptions: 248 249 - The `--mount` flag allows you to specify a volume driver and volume driver 250 options *per volume*, without creating the volumes in advance. In contrast, 251 `docker run` allows you to specify a single volume driver which is shared 252 by all volumes, using the `--volume-driver` flag. 253 254 - The `--mount` flag allows you to specify custom metadata ("labels") for a volume, 255 before the volume is created. 256 257 - When you use `--mount` with `type=bind`, the host-path must refer to an *existing* 258 path on the host. The path will not be created for you and the service will fail 259 with an error if the path does not exist. 260 261 - The `--mount` flag does not allow you to relabel a volume with `Z` or `z` flags, 262 which are used for `selinux` labeling. 263 264 #### Create a service using a named volume 265 266 The following example creates a service that uses a named volume: 267 268 ```bash 269 $ docker service create \ 270 --name my-service \ 271 --replicas 3 \ 272 --mount type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round" \ 273 nginx:alpine 274 ``` 275 276 For each replica of the service, the engine requests a volume named "my-volume" 277 from the default ("local") volume driver where the task is deployed. If the 278 volume does not exist, the engine creates a new volume and applies the "color" 279 and "shape" labels. 280 281 When the task is started, the volume is mounted on `/path/in/container/` inside 282 the container. 283 284 Be aware that the default ("local") volume is a locally scoped volume driver. 285 This means that depending on where a task is deployed, either that task gets a 286 *new* volume named "my-volume", or shares the same "my-volume" with other tasks 287 of the same service. Multiple containers writing to a single shared volume can 288 cause data corruption if the software running inside the container is not 289 designed to handle concurrent processes writing to the same location. Also take 290 into account that containers can be re-scheduled by the Swarm orchestrator and 291 be deployed on a different node. 292 293 #### Create a service that uses an anonymous volume 294 295 The following command creates a service with three replicas with an anonymous 296 volume on `/path/in/container`: 297 298 ```bash 299 $ docker service create \ 300 --name my-service \ 301 --replicas 3 \ 302 --mount type=volume,destination=/path/in/container \ 303 nginx:alpine 304 ``` 305 306 In this example, no name (`source`) is specified for the volume, so a new volume 307 is created for each task. This guarantees that each task gets its own volume, 308 and volumes are not shared between tasks. Anonymous volumes are removed after 309 the task using them is complete. 310 311 #### Create a service that uses a bind-mounted host directory 312 313 The following example bind-mounts a host directory at `/path/in/container` in 314 the containers backing the service: 315 316 ```bash 317 $ docker service create \ 318 --name my-service \ 319 --mount type=bind,source=/path/on/host,destination=/path/in/container \ 320 nginx:alpine 321 ``` 322 323 ### Set service mode (--mode) 324 325 The service mode determines whether this is a _replicated_ service or a _global_ 326 service. A replicated service runs as many tasks as specified, while a global 327 service runs on each active node in the swarm. 328 329 The following command creates a global service: 330 331 ```bash 332 $ docker service create \ 333 --name redis_2 \ 334 --mode global \ 335 redis:3.0.6 336 ``` 337 338 ### Specify service constraints (--constraint) 339 340 You can limit the set of nodes where a task can be scheduled by defining 341 constraint expressions. Multiple constraints find nodes that satisfy every 342 expression (AND match). Constraints can match node or Docker Engine labels as 343 follows: 344 345 | node attribute | matches | example | 346 |:----------------|:--------------------------|:------------------------------------------------| 347 | node.id | node ID | `node.id == 2ivku8v2gvtg4` | 348 | node.hostname | node hostname | `node.hostname != node-2` | 349 | node.role | node role: manager | `node.role == manager` | 350 | node.labels | user defined node labels | `node.labels.security == high` | 351 | engine.labels | Docker Engine's labels | `engine.labels.operatingsystem == ubuntu 14.04` | 352 353 `engine.labels` apply to Docker Engine labels like operating system, 354 drivers, etc. Swarm administrators add `node.labels` for operational purposes by 355 using the [`docker node update`](node_update.md) command. 356 357 For example, the following limits tasks for the redis service to nodes where the 358 node type label equals queue: 359 360 ```bash 361 $ docker service create \ 362 --name redis_2 \ 363 --constraint 'node.labels.type == queue' \ 364 redis:3.0.6 365 ``` 366 367 ### Attach a service to an existing network (--network) 368 369 You can use overlay networks to connect one or more services within the swarm. 370 371 First, create an overlay network on a manager node the docker network create 372 command: 373 374 ```bash 375 $ docker network create --driver overlay my-network 376 377 etjpu59cykrptrgw0z0hk5snf 378 ``` 379 380 After you create an overlay network in swarm mode, all manager nodes have 381 access to the network. 382 383 When you create a service and pass the --network flag to attach the service to 384 the overlay network: 385 386 ```bash 387 $ docker service create \ 388 --replicas 3 \ 389 --network my-network \ 390 --name my-web \ 391 nginx 392 393 716thylsndqma81j6kkkb5aus 394 ``` 395 396 The swarm extends my-network to each node running the service. 397 398 Containers on the same network can access each other using 399 [service discovery](https://docs.docker.com/engine/swarm/networking/#use-swarm-mode-service-discovery). 400 401 ### Publish service ports externally to the swarm (-p, --publish) 402 403 You can publish service ports to make them available externally to the swarm 404 using the `--publish` flag: 405 406 ```bash 407 $ docker service create --publish <TARGET-PORT>:<SERVICE-PORT> nginx 408 ``` 409 410 For example: 411 412 ```bash 413 $ docker service create --name my_web --replicas 3 --publish 8080:80 nginx 414 ``` 415 416 When you publish a service port, the swarm routing mesh makes the service 417 accessible at the target port on every node regardless if there is a task for 418 the service running on the node. For more information refer to 419 [Use swarm mode routing mesh](https://docs.docker.com/engine/swarm/ingress/). 420 421 ## Related information 422 423 * [service inspect](service_inspect.md) 424 * [service ls](service_ls.md) 425 * [service rm](service_rm.md) 426 * [service scale](service_scale.md) 427 * [service ps](service_ps.md) 428 * [service update](service_update.md) 429 430 <style>table tr > td:first-child { white-space: nowrap;}</style>