github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/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 list Placement constraints (default []) 25 --container-label list Container labels (default []) 26 --dns list Set custom DNS servers (default []) 27 --dns-option list Set DNS options (default []) 28 --dns-search list Set custom DNS search domains (default []) 29 --endpoint-mode string Endpoint mode ("vip"|"dnsrr") (default "vip") 30 -e, --env list Set environment variables (default []) 31 --env-file list Read in a file of environment variables (default []) 32 --group list Set one or more supplementary user groups for the container (default []) 33 --health-cmd string Command to run to check health 34 --health-interval duration Time between running the check (ns|us|ms|s|m|h) 35 --health-retries int Consecutive failures needed to report unhealthy 36 --health-timeout duration Maximum time to allow one check to run (ns|us|ms|s|m|h) 37 --help Print usage 38 --host list Set one or more custom host-to-IP mappings (host:ip) (default []) 39 --hostname string Container hostname 40 -l, --label list Service labels (default []) 41 --limit-cpu decimal Limit CPUs (default 0.000) 42 --limit-memory bytes Limit Memory 43 --log-driver string Logging driver for service 44 --log-opt list Logging driver options (default []) 45 --mode string Service mode (replicated or global) (default "replicated") 46 --mount mount Attach a filesystem mount to the service 47 --name string Service name 48 --network list Network attachments (default []) 49 --no-healthcheck Disable any container-specified HEALTHCHECK 50 -p, --publish port Publish a port as a node port 51 --read-only Mount the container's root filesystem as read only 52 --replicas uint Number of tasks 53 --reserve-cpu decimal Reserve CPUs (default 0.000) 54 --reserve-memory bytes Reserve Memory 55 --restart-condition string Restart when condition is met ("none"|"on-failure"|"any") 56 --restart-delay duration Delay between restart attempts (ns|us|ms|s|m|h) 57 --restart-max-attempts uint Maximum number of restarts before giving up 58 --restart-window duration Window used to evaluate the restart policy (ns|us|ms|s|m|h) 59 --secret secret Specify secrets to expose to the service 60 --stop-grace-period duration Time to wait before force killing a container (ns|us|ms|s|m|h) 61 -t, --tty Allocate a pseudo-TTY 62 --update-delay duration Delay between updates (ns|us|ms|s|m|h) (default 0s) 63 --update-failure-action string Action on update failure ("pause"|"continue") (default "pause") 64 --update-max-failure-ratio float Failure rate to tolerate during an update 65 --update-monitor duration Duration after each task update to monitor for failure (ns|us|ms|s|m|h) (default 0s) 66 --update-parallelism uint Maximum number of tasks updated simultaneously (0 to update all at once) (default 1) 67 -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) 68 --with-registry-auth Send registry authentication details to swarm agents 69 -w, --workdir string Working directory inside the container 70 ``` 71 72 ## Description 73 74 Creates a service as described by the specified parameters. You must run this 75 command on a manager node. 76 77 ## Examples 78 79 ### Create a service 80 81 ```bash 82 $ docker service create --name redis redis:3.0.6 83 84 dmu1ept4cxcfe8k8lhtux3ro3 85 86 $ docker service create --mode global --name redis2 redis:3.0.6 87 88 a8q9dasaafudfs8q8w32udass 89 90 $ docker service ls 91 92 ID NAME MODE REPLICAS IMAGE 93 dmu1ept4cxcf redis replicated 1/1 redis:3.0.6 94 a8q9dasaafud redis2 global 1/1 redis:3.0.6 95 ``` 96 97 ### Create a service with 5 replica tasks (--replicas) 98 99 Use the `--replicas` flag to set the number of replica tasks for a replicated 100 service. The following command creates a `redis` service with `5` replica tasks: 101 102 ```bash 103 $ docker service create --name redis --replicas=5 redis:3.0.6 104 105 4cdgfyky7ozwh3htjfw0d12qv 106 ``` 107 108 The above command sets the *desired* number of tasks for the service. Even 109 though the command returns immediately, actual scaling of the service may take 110 some time. The `REPLICAS` column shows both the *actual* and *desired* number 111 of replica tasks for the service. 112 113 In the following example the desired state is `5` replicas, but the current 114 number of `RUNNING` tasks is `3`: 115 116 ```bash 117 $ docker service ls 118 119 ID NAME MODE REPLICAS IMAGE 120 4cdgfyky7ozw redis replicated 3/5 redis:3.0.7 121 ``` 122 123 Once all the tasks are created and `RUNNING`, the actual number of tasks is 124 equal to the desired number: 125 126 ```bash 127 $ docker service ls 128 129 ID NAME MODE REPLICAS IMAGE 130 4cdgfyky7ozw redis replicated 5/5 redis:3.0.7 131 ``` 132 133 ### Create a service with secrets 134 135 Use the `--secret` flag to give a container access to a 136 [secret](secret_create.md). 137 138 Create a service specifying a secret: 139 140 ```bash 141 $ docker service create --name redis --secret secret.json redis:3.0.6 142 143 4cdgfyky7ozwh3htjfw0d12qv 144 ``` 145 146 Create a service specifying the secret, target, user/group ID and mode: 147 148 ```bash 149 $ docker service create --name redis \ 150 --secret source=ssh-key,target=ssh \ 151 --secret source=app-key,target=app,uid=1000,gid=1001,mode=0400 \ 152 redis:3.0.6 153 154 4cdgfyky7ozwh3htjfw0d12qv 155 ``` 156 157 Secrets are located in `/run/secrets` in the container. If no target is 158 specified, the name of the secret will be used as the in memory file in the 159 container. If a target is specified, that will be the filename. In the 160 example above, two files will be created: `/run/secrets/ssh` and 161 `/run/secrets/app` for each of the secret targets specified. 162 163 ### Create a service with a rolling update policy 164 165 ```bash 166 $ docker service create \ 167 --replicas 10 \ 168 --name redis \ 169 --update-delay 10s \ 170 --update-parallelism 2 \ 171 redis:3.0.6 172 ``` 173 174 When you run a [service update](service_update.md), the scheduler updates a 175 maximum of 2 tasks at a time, with `10s` between updates. For more information, 176 refer to the [rolling updates 177 tutorial](https://docs.docker.com/engine/swarm/swarm-tutorial/rolling-update/). 178 179 ### Set environment variables (-e, --env) 180 181 This sets environmental variables for all tasks in a service. For example: 182 183 ```bash 184 $ docker service create --name redis_2 --replicas 5 --env MYVAR=foo redis:3.0.6 185 ``` 186 187 ### Create a service with specific hostname (--hostname) 188 189 This option sets the docker service containers hostname to a specific string. 190 For example: 191 192 ```bash 193 $ docker service create --name redis --hostname myredis redis:3.0.6 194 ``` 195 196 ### Set metadata on a service (-l, --label) 197 198 A label is a `key=value` pair that applies metadata to a service. To label a 199 service with two labels: 200 201 ```bash 202 $ docker service create \ 203 --name redis_2 \ 204 --label com.example.foo="bar" 205 --label bar=baz \ 206 redis:3.0.6 207 ``` 208 209 For more information about labels, refer to [apply custom 210 metadata](https://docs.docker.com/engine/userguide/labels-custom-metadata/). 211 212 ### Add bind-mounts or volumes 213 214 Docker supports two different kinds of mounts, which allow containers to read to 215 or write from files or directories on other containers or the host operating 216 system. These types are _data volumes_ (often referred to simply as volumes) and 217 _bind-mounts_. 218 219 Additionally, Docker supports `tmpfs` mounts. 220 221 A **bind-mount** makes a file or directory on the host available to the 222 container it is mounted within. A bind-mount may be either read-only or 223 read-write. For example, a container might share its host's DNS information by 224 means of a bind-mount of the host's `/etc/resolv.conf` or a container might 225 write logs to its host's `/var/log/myContainerLogs` directory. If you use 226 bind-mounts and your host and containers have different notions of permissions, 227 access controls, or other such details, you will run into portability issues. 228 229 A **named volume** is a mechanism for decoupling persistent data needed by your 230 container from the image used to create the container and from the host machine. 231 Named volumes are created and managed by Docker, and a named volume persists 232 even when no container is currently using it. Data in named volumes can be 233 shared between a container and the host machine, as well as between multiple 234 containers. Docker uses a _volume driver_ to create, manage, and mount volumes. 235 You can back up or restore volumes using Docker commands. 236 237 A **tmpfs** mounts a tmpfs inside a container for volatile data. 238 239 Consider a situation where your image starts a lightweight web server. You could 240 use that image as a base image, copy in your website's HTML files, and package 241 that into another image. Each time your website changed, you'd need to update 242 the new image and redeploy all of the containers serving your website. A better 243 solution is to store the website in a named volume which is attached to each of 244 your web server containers when they start. To update the website, you just 245 update the named volume. 246 247 For more information about named volumes, see 248 [Data Volumes](https://docs.docker.com/engine/tutorials/dockervolumes/). 249 250 The following table describes options which apply to both bind-mounts and named 251 volumes in a service: 252 253 | Option | Required | Description 254 |:-----------------------------------------|:--------------------------|:----------------------------------------------------------------------------------------- 255 | **type** | | The type of mount, can be either `volume`, `bind`, or `tmpfs`. 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><li>`tmpfs`: mount a tmpfs in the container</li></ul> 256 | **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><li>`type=tmpfs`: `src` is not supported.</li></ul> 257 | **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. 258 | **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> 259 260 #### Bind Propagation 261 262 Bind propagation refers to whether or not mounts created within a given 263 bind-mount or named volume can be propagated to replicas of that mount. Consider 264 a mount point `/mnt`, which is also mounted on `/tmp`. The propation settings 265 control whether a mount on `/tmp/a` would also be available on `/mnt/a`. Each 266 propagation setting has a recursive counterpoint. In the case of recursion, 267 consider that `/tmp/a` is also mounted as `/foo`. The propagation settings 268 control whether `/mnt/a` and/or `/tmp/a` would exist. 269 270 The `bind-propagation` option defaults to `rprivate` for both bind-mounts and 271 volume mounts, and is only configurable for bind-mounts. In other words, named 272 volumes do not support bind propagation. 273 274 - **`shared`**: Sub-mounts of the original mount are exposed to replica mounts, 275 and sub-mounts of replica mounts are also propagated to the 276 original mount. 277 - **`slave`**: similar to a shared mount, but only in one direction. If the 278 original mount exposes a sub-mount, the replica mount can see it. 279 However, if the replica mount exposes a sub-mount, the original 280 mount cannot see it. 281 - **`private`**: The mount is private. Sub-mounts within it are not exposed to 282 replica mounts, and sub-mounts of replica mounts are not 283 exposed to the original mount. 284 - **`rshared`**: The same as shared, but the propagation also extends to and from 285 mount points nested within any of the original or replica mount 286 points. 287 - **`rslave`**: The same as `slave`, but the propagation also extends to and from 288 mount points nested within any of the original or replica mount 289 points. 290 - **`rprivate`**: The default. The same as `private`, meaning that no mount points 291 anywhere within the original or replica mount points propagate 292 in either direction. 293 294 For more information about bind propagation, see the 295 [Linux kernel documentation for shared subtree](https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt). 296 297 #### Options for Named Volumes 298 The following options can only be used for named volumes (`type=volume`); 299 300 | Option | Description 301 |:----------------------|:-------------------------------------------------------------------------------------------------------------------- 302 | **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. 303 | **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/). 304 | **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> 305 | **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. 306 307 #### Options for tmpfs 308 The following options can only be used for tmpfs mounts (`type=tmpfs`); 309 310 | Option | Description 311 |:----------------------|:-------------------------------------------------------------------------------------------------------------------- 312 | **tmpfs-size** | Size of the tmpfs mount in bytes. Unlimited by default in Linux. 313 | **tmpfs-mode** | File mode of the tmpfs in octal. (e.g. `"700"` or `"0700"`.) Defaults to ``"1777"`` in Linux. 314 315 #### Differences between "--mount" and "--volume" 316 317 The `--mount` flag supports most options that are supported by the `-v` 318 or `--volume` flag for `docker run`, with some important exceptions: 319 320 - The `--mount` flag allows you to specify a volume driver and volume driver 321 options *per volume*, without creating the volumes in advance. In contrast, 322 `docker run` allows you to specify a single volume driver which is shared 323 by all volumes, using the `--volume-driver` flag. 324 325 - The `--mount` flag allows you to specify custom metadata ("labels") for a volume, 326 before the volume is created. 327 328 - When you use `--mount` with `type=bind`, the host-path must refer to an *existing* 329 path on the host. The path will not be created for you and the service will fail 330 with an error if the path does not exist. 331 332 - The `--mount` flag does not allow you to relabel a volume with `Z` or `z` flags, 333 which are used for `selinux` labeling. 334 335 #### Create a service using a named volume 336 337 The following example creates a service that uses a named volume: 338 339 ```bash 340 $ docker service create \ 341 --name my-service \ 342 --replicas 3 \ 343 --mount type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round" \ 344 nginx:alpine 345 ``` 346 347 For each replica of the service, the engine requests a volume named "my-volume" 348 from the default ("local") volume driver where the task is deployed. If the 349 volume does not exist, the engine creates a new volume and applies the "color" 350 and "shape" labels. 351 352 When the task is started, the volume is mounted on `/path/in/container/` inside 353 the container. 354 355 Be aware that the default ("local") volume is a locally scoped volume driver. 356 This means that depending on where a task is deployed, either that task gets a 357 *new* volume named "my-volume", or shares the same "my-volume" with other tasks 358 of the same service. Multiple containers writing to a single shared volume can 359 cause data corruption if the software running inside the container is not 360 designed to handle concurrent processes writing to the same location. Also take 361 into account that containers can be re-scheduled by the Swarm orchestrator and 362 be deployed on a different node. 363 364 #### Create a service that uses an anonymous volume 365 366 The following command creates a service with three replicas with an anonymous 367 volume on `/path/in/container`: 368 369 ```bash 370 $ docker service create \ 371 --name my-service \ 372 --replicas 3 \ 373 --mount type=volume,destination=/path/in/container \ 374 nginx:alpine 375 ``` 376 377 In this example, no name (`source`) is specified for the volume, so a new volume 378 is created for each task. This guarantees that each task gets its own volume, 379 and volumes are not shared between tasks. Anonymous volumes are removed after 380 the task using them is complete. 381 382 #### Create a service that uses a bind-mounted host directory 383 384 The following example bind-mounts a host directory at `/path/in/container` in 385 the containers backing the service: 386 387 ```bash 388 $ docker service create \ 389 --name my-service \ 390 --mount type=bind,source=/path/on/host,destination=/path/in/container \ 391 nginx:alpine 392 ``` 393 394 ### Set service mode (--mode) 395 396 The service mode determines whether this is a _replicated_ service or a _global_ 397 service. A replicated service runs as many tasks as specified, while a global 398 service runs on each active node in the swarm. 399 400 The following command creates a global service: 401 402 ```bash 403 $ docker service create \ 404 --name redis_2 \ 405 --mode global \ 406 redis:3.0.6 407 ``` 408 409 ### Specify service constraints (--constraint) 410 411 You can limit the set of nodes where a task can be scheduled by defining 412 constraint expressions. Multiple constraints find nodes that satisfy every 413 expression (AND match). Constraints can match node or Docker Engine labels as 414 follows: 415 416 | node attribute | matches | example | 417 |:----------------|:--------------------------|:------------------------------------------------| 418 | node.id | node ID | `node.id == 2ivku8v2gvtg4` | 419 | node.hostname | node hostname | `node.hostname != node-2` | 420 | node.role | node role: manager | `node.role == manager` | 421 | node.labels | user defined node labels | `node.labels.security == high` | 422 | engine.labels | Docker Engine's labels | `engine.labels.operatingsystem == ubuntu 14.04` | 423 424 `engine.labels` apply to Docker Engine labels like operating system, 425 drivers, etc. Swarm administrators add `node.labels` for operational purposes by 426 using the [`docker node update`](node_update.md) command. 427 428 For example, the following limits tasks for the redis service to nodes where the 429 node type label equals queue: 430 431 ```bash 432 $ docker service create \ 433 --name redis_2 \ 434 --constraint 'node.labels.type == queue' \ 435 redis:3.0.6 436 ``` 437 438 ### Attach a service to an existing network (--network) 439 440 You can use overlay networks to connect one or more services within the swarm. 441 442 First, create an overlay network on a manager node the docker network create 443 command: 444 445 ```bash 446 $ docker network create --driver overlay my-network 447 448 etjpu59cykrptrgw0z0hk5snf 449 ``` 450 451 After you create an overlay network in swarm mode, all manager nodes have 452 access to the network. 453 454 When you create a service and pass the --network flag to attach the service to 455 the overlay network: 456 457 ```bash 458 $ docker service create \ 459 --replicas 3 \ 460 --network my-network \ 461 --name my-web \ 462 nginx 463 464 716thylsndqma81j6kkkb5aus 465 ``` 466 467 The swarm extends my-network to each node running the service. 468 469 Containers on the same network can access each other using 470 [service discovery](https://docs.docker.com/engine/swarm/networking/#use-swarm-mode-service-discovery). 471 472 ### Publish service ports externally to the swarm (-p, --publish) 473 474 You can publish service ports to make them available externally to the swarm 475 using the `--publish` flag: 476 477 ```bash 478 $ docker service create --publish <TARGET-PORT>:<SERVICE-PORT> nginx 479 ``` 480 481 For example: 482 483 ```bash 484 $ docker service create --name my_web --replicas 3 --publish 8080:80 nginx 485 ``` 486 487 When you publish a service port, the swarm routing mesh makes the service 488 accessible at the target port on every node regardless if there is a task for 489 the service running on the node. For more information refer to 490 [Use swarm mode routing mesh](https://docs.docker.com/engine/swarm/ingress/). 491 492 ### Publish a port for TCP only or UDP only 493 494 By default, when you publish a port, it is a TCP port. You can 495 specifically publish a UDP port instead of or in addition to a TCP port. When 496 you publish both TCP and UDP ports, Docker 1.12.2 and earlier require you to 497 add the suffix `/tcp` for TCP ports. Otherwise it is optional. 498 499 #### TCP only 500 501 The following two commands are equivalent. 502 503 ```bash 504 $ docker service create --name dns-cache -p 53:53 dns-cache 505 506 $ docker service create --name dns-cache -p 53:53/tcp dns-cache 507 ``` 508 509 #### TCP and UDP 510 511 ```bash 512 $ docker service create --name dns-cache -p 53:53/tcp -p 53:53/udp dns-cache 513 ``` 514 515 #### UDP only 516 517 ```bash 518 $ docker service create --name dns-cache -p 53:53/udp dns-cache 519 ``` 520 521 ### Create services using templates 522 523 You can use templates for some flags of `service create`, using the syntax 524 provided by the Go's [text/template](http://golang.org/pkg/text/template/) package. 525 526 The supported flags are the following : 527 528 - `--hostname` 529 - `--mount` 530 - `--env` 531 532 Valid placeholders for the Go template are listed below: 533 534 Placeholder | Description 535 ----------------- | -------------------------------------------- 536 `.Service.ID` | Service ID 537 `.Service.Name` | Service name 538 `.Service.Labels` | Service labels 539 `.Node.ID` | Node ID 540 `.Task.ID` | Task ID 541 `.Task.Name` | Task name 542 `.Task.Slot` | Task slot 543 544 #### Template example 545 546 In this example, we are going to set the template of the created containers based on the 547 service's name and the node's ID where it sits. 548 549 ```bash 550 {% raw %} 551 $ docker service create --name hosttempl \ 552 --hostname={% raw %}"{{.Node.ID}}-{{.Service.Name}}"\ 553 busybox top 554 555 va8ew30grofhjoychbr6iot8c 556 557 $ docker service ps va8ew30grofhjoychbr6iot8c 558 559 ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 560 wo41w8hg8qan hosttempl.1 busybox:latest@sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912 2e7a8a9c4da2 Running Running about a minute ago 561 562 $ docker inspect --format="{{.Config.Hostname}}" hosttempl.1.wo41w8hg8qanxwjwsg4kxpprj 563 564 x3ti0erg11rjpg64m75kej2mz-hosttempl 565 {% endraw %} 566 ``` 567 568 ## Related commands 569 570 * [service inspect](service_inspect.md) 571 * [service logs](service_logs.md) 572 * [service ls](service_ls.md) 573 * [service rm](service_rm.md) 574 * [service scale](service_scale.md) 575 * [service ps](service_ps.md) 576 * [service update](service_update.md) 577 578 <style>table tr > td:first-child { white-space: nowrap;}</style>