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