github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/docs/reference/commandline/service_create.md (about) 1 # service create 2 3 <!---MARKER_GEN_START--> 4 Create a new service 5 6 ### Options 7 8 | Name | Type | Default | Description | 9 |:----------------------------------------------------|:------------------|:-------------|:----------------------------------------------------------------------------------------------------| 10 | `--cap-add` | `list` | | Add Linux capabilities | 11 | `--cap-drop` | `list` | | Drop Linux capabilities | 12 | [`--config`](#config) | `config` | | Specify configurations to expose to the service | 13 | [`--constraint`](#constraint) | `list` | | Placement constraints | 14 | `--container-label` | `list` | | Container labels | 15 | `--credential-spec` | `credential-spec` | | Credential spec for managed service account (Windows only) | 16 | `-d`, `--detach` | | | Exit immediately instead of waiting for the service to converge | 17 | `--dns` | `list` | | Set custom DNS servers | 18 | `--dns-option` | `list` | | Set DNS options | 19 | `--dns-search` | `list` | | Set custom DNS search domains | 20 | `--endpoint-mode` | `string` | `vip` | Endpoint mode (vip or dnsrr) | 21 | `--entrypoint` | `command` | | Overwrite the default ENTRYPOINT of the image | 22 | [`-e`](#env), [`--env`](#env) | `list` | | Set environment variables | 23 | `--env-file` | `list` | | Read in a file of environment variables | 24 | `--generic-resource` | `list` | | User defined resources | 25 | `--group` | `list` | | Set one or more supplementary user groups for the container | 26 | `--health-cmd` | `string` | | Command to run to check health | 27 | `--health-interval` | `duration` | | Time between running the check (ms\|s\|m\|h) | 28 | `--health-retries` | `int` | `0` | Consecutive failures needed to report unhealthy | 29 | `--health-start-period` | `duration` | | Start period for the container to initialize before counting retries towards unstable (ms\|s\|m\|h) | 30 | `--health-timeout` | `duration` | | Maximum time to allow one check to run (ms\|s\|m\|h) | 31 | `--host` | `list` | | Set one or more custom host-to-IP mappings (host:ip) | 32 | [`--hostname`](#hostname) | `string` | | Container hostname | 33 | `--init` | | | Use an init inside each service container to forward signals and reap processes | 34 | [`--isolation`](#isolation) | `string` | | Service container isolation mode | 35 | [`-l`](#label), [`--label`](#label) | `list` | | Service labels | 36 | `--limit-cpu` | `decimal` | | Limit CPUs | 37 | `--limit-memory` | `bytes` | `0` | Limit Memory | 38 | `--limit-pids` | `int64` | `0` | Limit maximum number of processes (default 0 = unlimited) | 39 | `--log-driver` | `string` | | Logging driver for service | 40 | `--log-opt` | `list` | | Logging driver options | 41 | `--max-concurrent` | `uint` | | Number of job tasks to run concurrently (default equal to --replicas) | 42 | `--mode` | `string` | `replicated` | Service mode (`replicated`, `global`, `replicated-job`, `global-job`) | 43 | [`--mount`](#mount) | `mount` | | Attach a filesystem mount to the service | 44 | `--name` | `string` | | Service name | 45 | [`--network`](#network) | `network` | | Network attachments | 46 | `--no-healthcheck` | | | Disable any container-specified HEALTHCHECK | 47 | `--no-resolve-image` | | | Do not query the registry to resolve image digest and supported platforms | 48 | [`--placement-pref`](#placement-pref) | `pref` | | Add a placement preference | 49 | [`-p`](#publish), [`--publish`](#publish) | `port` | | Publish a port as a node port | 50 | `-q`, `--quiet` | | | Suppress progress output | 51 | `--read-only` | | | Mount the container's root filesystem as read only | 52 | [`--replicas`](#replicas) | `uint` | | Number of tasks | 53 | [`--replicas-max-per-node`](#replicas-max-per-node) | `uint64` | `0` | Maximum number of tasks per node (default 0 = unlimited) | 54 | `--reserve-cpu` | `decimal` | | Reserve CPUs | 55 | [`--reserve-memory`](#reserve-memory) | `bytes` | `0` | Reserve Memory | 56 | `--restart-condition` | `string` | | Restart when condition is met (`none`, `on-failure`, `any`) (default `any`) | 57 | `--restart-delay` | `duration` | | Delay between restart attempts (ns\|us\|ms\|s\|m\|h) (default 5s) | 58 | `--restart-max-attempts` | `uint` | | Maximum number of restarts before giving up | 59 | `--restart-window` | `duration` | | Window used to evaluate the restart policy (ns\|us\|ms\|s\|m\|h) | 60 | `--rollback-delay` | `duration` | `0s` | Delay between task rollbacks (ns\|us\|ms\|s\|m\|h) (default 0s) | 61 | `--rollback-failure-action` | `string` | | Action on rollback failure (`pause`, `continue`) (default `pause`) | 62 | `--rollback-max-failure-ratio` | `float` | `0` | Failure rate to tolerate during a rollback (default 0) | 63 | `--rollback-monitor` | `duration` | `0s` | Duration after each task rollback to monitor for failure (ns\|us\|ms\|s\|m\|h) (default 5s) | 64 | `--rollback-order` | `string` | | Rollback order (`start-first`, `stop-first`) (default `stop-first`) | 65 | `--rollback-parallelism` | `uint64` | `1` | Maximum number of tasks rolled back simultaneously (0 to roll back all at once) | 66 | [`--secret`](#secret) | `secret` | | Specify secrets to expose to the service | 67 | `--stop-grace-period` | `duration` | | Time to wait before force killing a container (ns\|us\|ms\|s\|m\|h) (default 10s) | 68 | `--stop-signal` | `string` | | Signal to stop the container | 69 | `--sysctl` | `list` | | Sysctl options | 70 | `-t`, `--tty` | | | Allocate a pseudo-TTY | 71 | `--ulimit` | `ulimit` | | Ulimit options | 72 | [`--update-delay`](#update-delay) | `duration` | `0s` | Delay between updates (ns\|us\|ms\|s\|m\|h) (default 0s) | 73 | `--update-failure-action` | `string` | | Action on update failure (`pause`, `continue`, `rollback`) (default `pause`) | 74 | `--update-max-failure-ratio` | `float` | `0` | Failure rate to tolerate during an update (default 0) | 75 | `--update-monitor` | `duration` | `0s` | Duration after each task update to monitor for failure (ns\|us\|ms\|s\|m\|h) (default 5s) | 76 | `--update-order` | `string` | | Update order (`start-first`, `stop-first`) (default `stop-first`) | 77 | `--update-parallelism` | `uint64` | `1` | Maximum number of tasks updated simultaneously (0 to update all at once) | 78 | `-u`, `--user` | `string` | | Username or UID (format: <name\|uid>[:<group\|gid>]) | 79 | [`--with-registry-auth`](#with-registry-auth) | | | Send registry authentication details to swarm agents | 80 | `-w`, `--workdir` | `string` | | Working directory inside the container | 81 82 83 <!---MARKER_GEN_END--> 84 85 ## Description 86 87 Creates a service as described by the specified parameters. 88 89 > **Note** 90 > 91 > This is a cluster management command, and must be executed on a swarm 92 > manager node. To learn about managers and workers, refer to the 93 > [Swarm mode section](https://docs.docker.com/engine/swarm/) in the 94 > documentation. 95 96 ## Examples 97 98 ### Create a service 99 100 ```console 101 $ docker service create --name redis redis:3.0.6 102 103 dmu1ept4cxcfe8k8lhtux3ro3 104 105 $ docker service create --mode global --name redis2 redis:3.0.6 106 107 a8q9dasaafudfs8q8w32udass 108 109 $ docker service ls 110 111 ID NAME MODE REPLICAS IMAGE 112 dmu1ept4cxcf redis replicated 1/1 redis:3.0.6 113 a8q9dasaafud redis2 global 1/1 redis:3.0.6 114 ``` 115 116 #### <a name="with-registry-auth"></a> Create a service using an image on a private registry (--with-registry-auth) 117 118 If your image is available on a private registry which requires login, use the 119 `--with-registry-auth` flag with `docker service create`, after logging in. If 120 your image is stored on `registry.example.com`, which is a private registry, use 121 a command like the following: 122 123 ```console 124 $ docker login registry.example.com 125 126 $ docker service create \ 127 --with-registry-auth \ 128 --name my_service \ 129 registry.example.com/acme/my_image:latest 130 ``` 131 132 This passes the login token from your local client to the swarm nodes where the 133 service is deployed, using the encrypted WAL logs. With this information, the 134 nodes are able to log into the registry and pull the image. 135 136 ### <a name="replicas"></a> Create a service with 5 replica tasks (--replicas) 137 138 Use the `--replicas` flag to set the number of replica tasks for a replicated 139 service. The following command creates a `redis` service with `5` replica tasks: 140 141 ```console 142 $ docker service create --name redis --replicas=5 redis:3.0.6 143 144 4cdgfyky7ozwh3htjfw0d12qv 145 ``` 146 147 The above command sets the *desired* number of tasks for the service. Even 148 though the command returns immediately, actual scaling of the service may take 149 some time. The `REPLICAS` column shows both the *actual* and *desired* number 150 of replica tasks for the service. 151 152 In the following example the desired state is `5` replicas, but the current 153 number of `RUNNING` tasks is `3`: 154 155 ```console 156 $ docker service ls 157 158 ID NAME MODE REPLICAS IMAGE 159 4cdgfyky7ozw redis replicated 3/5 redis:3.0.7 160 ``` 161 162 Once all the tasks are created and `RUNNING`, the actual number of tasks is 163 equal to the desired number: 164 165 ```console 166 $ docker service ls 167 168 ID NAME MODE REPLICAS IMAGE 169 4cdgfyky7ozw redis replicated 5/5 redis:3.0.7 170 ``` 171 172 ### <a name="secret"></a> Create a service with secrets (--secret) 173 174 Use the `--secret` flag to give a container access to a 175 [secret](secret_create.md). 176 177 Create a service specifying a secret: 178 179 ```console 180 $ docker service create --name redis --secret secret.json redis:3.0.6 181 182 4cdgfyky7ozwh3htjfw0d12qv 183 ``` 184 185 Create a service specifying the secret, target, user/group ID, and mode: 186 187 ```console 188 $ docker service create --name redis \ 189 --secret source=ssh-key,target=ssh \ 190 --secret source=app-key,target=app,uid=1000,gid=1001,mode=0400 \ 191 redis:3.0.6 192 193 4cdgfyky7ozwh3htjfw0d12qv 194 ``` 195 196 To grant a service access to multiple secrets, use multiple `--secret` flags. 197 198 Secrets are located in `/run/secrets` in the container if no target is specified. 199 If no target is specified, the name of the secret is used as the in memory file 200 in the container. If a target is specified, that is used as the filename. In the 201 example above, two files are created: `/run/secrets/ssh` and 202 `/run/secrets/app` for each of the secret targets specified. 203 204 ### <a name="config"></a> Create a service with configs (--config) 205 206 Use the `--config` flag to give a container access to a 207 [config](config_create.md). 208 209 Create a service with a config. The config will be mounted into `redis-config`, 210 be owned by the user who runs the command inside the container (often `root`), 211 and have file mode `0444` or world-readable. You can specify the `uid` and `gid` 212 as numerical IDs or names. When using names, the provided group/user names must 213 pre-exist in the container. The `mode` is specified as a 4-number sequence such 214 as `0755`. 215 216 ```console 217 $ docker service create --name=redis --config redis-conf redis:3.0.6 218 ``` 219 220 Create a service with a config and specify the target location and file mode: 221 222 ```console 223 $ docker service create --name redis \ 224 --config source=redis-conf,target=/etc/redis/redis.conf,mode=0400 redis:3.0.6 225 ``` 226 227 To grant a service access to multiple configs, use multiple `--config` flags. 228 229 Configs are located in `/` in the container if no target is specified. If no 230 target is specified, the name of the config is used as the name of the file in 231 the container. If a target is specified, that is used as the filename. 232 233 ### <a name="update-delay"></a> Create a service with a rolling update policy 234 235 ```console 236 $ docker service create \ 237 --replicas 10 \ 238 --name redis \ 239 --update-delay 10s \ 240 --update-parallelism 2 \ 241 redis:3.0.6 242 ``` 243 244 When you run a [service update](service_update.md), the scheduler updates a 245 maximum of 2 tasks at a time, with `10s` between updates. For more information, 246 refer to the [rolling updates 247 tutorial](https://docs.docker.com/engine/swarm/swarm-tutorial/rolling-update/). 248 249 ### <a name="env"></a> Set environment variables (-e, --env) 250 251 This sets an environment variable for all tasks in a service. For example: 252 253 ```console 254 $ docker service create \ 255 --name redis_2 \ 256 --replicas 5 \ 257 --env MYVAR=foo \ 258 redis:3.0.6 259 ``` 260 261 To specify multiple environment variables, specify multiple `--env` flags, each 262 with a separate key-value pair. 263 264 ```console 265 $ docker service create \ 266 --name redis_2 \ 267 --replicas 5 \ 268 --env MYVAR=foo \ 269 --env MYVAR2=bar \ 270 redis:3.0.6 271 ``` 272 273 ### <a name="hostname"></a> Create a service with specific hostname (--hostname) 274 275 This option sets the docker service containers hostname to a specific string. 276 For example: 277 278 ```console 279 $ docker service create --name redis --hostname myredis redis:3.0.6 280 ``` 281 282 ### <a name="label"></a> Set metadata on a service (-l, --label) 283 284 A label is a `key=value` pair that applies metadata to a service. To label a 285 service with two labels: 286 287 ```console 288 $ docker service create \ 289 --name redis_2 \ 290 --label com.example.foo="bar" \ 291 --label bar=baz \ 292 redis:3.0.6 293 ``` 294 295 For more information about labels, refer to [apply custom 296 metadata](https://docs.docker.com/config/labels-custom-metadata/). 297 298 ### <a name="mount"></a> Add bind mounts, volumes or memory filesystems (--mount) 299 300 Docker supports three different kinds of mounts, which allow containers to read 301 from or write to files or directories, either on the host operating system, or 302 on memory filesystems. These types are _data volumes_ (often referred to simply 303 as volumes), _bind mounts_, _tmpfs_, and _named pipes_. 304 305 A **bind mount** makes a file or directory on the host available to the 306 container it is mounted within. A bind mount may be either read-only or 307 read-write. For example, a container might share its host's DNS information by 308 means of a bind mount of the host's `/etc/resolv.conf` or a container might 309 write logs to its host's `/var/log/myContainerLogs` directory. If you use 310 bind mounts and your host and containers have different notions of permissions, 311 access controls, or other such details, you will run into portability issues. 312 313 A **named volume** is a mechanism for decoupling persistent data needed by your 314 container from the image used to create the container and from the host machine. 315 Named volumes are created and managed by Docker, and a named volume persists 316 even when no container is currently using it. Data in named volumes can be 317 shared between a container and the host machine, as well as between multiple 318 containers. Docker uses a _volume driver_ to create, manage, and mount volumes. 319 You can back up or restore volumes using Docker commands. 320 321 A **tmpfs** mounts a tmpfs inside a container for volatile data. 322 323 A **npipe** mounts a named pipe from the host into the container. 324 325 Consider a situation where your image starts a lightweight web server. You could 326 use that image as a base image, copy in your website's HTML files, and package 327 that into another image. Each time your website changed, you'd need to update 328 the new image and redeploy all of the containers serving your website. A better 329 solution is to store the website in a named volume which is attached to each of 330 your web server containers when they start. To update the website, you just 331 update the named volume. 332 333 For more information about named volumes, see 334 [Data Volumes](https://docs.docker.com/storage/volumes/). 335 336 The following table describes options which apply to both bind mounts and named 337 volumes in a service: 338 339 <table> 340 <tr> 341 <th>Option</th> 342 <th>Required</th> 343 <th>Description</th> 344 </tr> 345 <tr> 346 <td><b>type</b></td> 347 <td></td> 348 <td> 349 <p>The type of mount, can be either <tt>volume</tt>, <tt>bind</tt>, <tt>tmpfs</tt>, or <tt>npipe</tt>. Defaults to <tt>volume</tt> if no type is specified.</p> 350 <ul> 351 <li><tt>volume</tt>: mounts a <a href="https://docs.docker.com/engine/reference/commandline/volume_create/">managed volume</a> 352 into the container.</li> <li><tt>bind</tt>: 353 bind-mounts a directory or file from the host into the container.</li> 354 <li><tt>tmpfs</tt>: mount a tmpfs in the container</li> 355 <li><tt>npipe</tt>: mounts named pipe from the host into the container (Windows containers only).</li> 356 </ul> 357 </td> 358 </tr> 359 <tr> 360 <td><b>src</b> or <b>source</b></td> 361 <td>for <tt>type=bind</tt> and <tt>type=npipe</tt></td> 362 <td> 363 <ul> 364 <li> 365 <tt>type=volume</tt>: <tt>src</tt> is an optional way to specify the name of the volume (for example, <tt>src=my-volume</tt>). 366 If the named volume does not exist, it is automatically created. If no <tt>src</tt> is specified, the volume is 367 assigned a random name which is guaranteed to be unique on the host, but may not be unique cluster-wide. 368 A randomly-named volume has the same lifecycle as its container and is destroyed when the <i>container</i> 369 is destroyed (which is upon <tt>service update</tt>, or when scaling or re-balancing the service) 370 </li> 371 <li> 372 <tt>type=bind</tt>: <tt>src</tt> is required, and specifies an absolute path to the file or directory to bind-mount 373 (for example, <tt>src=/path/on/host/</tt>). An error is produced if the file or directory does not exist. 374 </li> 375 <li> 376 <tt>type=tmpfs</tt>: <tt>src</tt> is not supported. 377 </li> 378 </ul> 379 </td> 380 </tr> 381 <tr> 382 <td><p><b>dst</b> or <b>destination</b> or <b>target</b></p></td> 383 <td>yes</td> 384 <td> 385 <p>Mount path inside the container, for example <tt>/some/path/in/container/</tt>. 386 If the path does not exist in the container's filesystem, the Engine creates 387 a directory at the specified location before mounting the volume or bind mount.</p> 388 </td> 389 </tr> 390 <tr> 391 <td><p><b>readonly</b> or <b>ro</b></p></td> 392 <td></td> 393 <td> 394 <p>The Engine mounts binds and volumes <tt>read-write</tt> unless <tt>readonly</tt> option 395 is given when mounting the bind or volume. Note that setting <tt>readonly</tt> for a 396 bind-mount does not make its submounts <tt>readonly</tt> on the current Linux implementation. See also <tt>bind-nonrecursive</tt>.</p> 397 <ul> 398 <li><tt>true</tt> or <tt>1</tt> or no value: Mounts the bind or volume read-only.</li> 399 <li><tt>false</tt> or <tt>0</tt>: Mounts the bind or volume read-write.</li> 400 </ul> 401 </td> 402 </tr> 403 </table> 404 405 #### Options for Bind Mounts 406 407 The following options can only be used for bind mounts (`type=bind`): 408 409 410 <table> 411 <tr> 412 <th>Option</th> 413 <th>Description</th> 414 </tr> 415 <tr> 416 <td><b>bind-propagation</b></td> 417 <td> 418 <p>See the <a href="#bind-propagation">bind propagation section</a>.</p> 419 </td> 420 </tr> 421 <tr> 422 <td><b>consistency</b></td> 423 <td> 424 <p>The consistency requirements for the mount; one of </p> 425 <ul> 426 <li><tt>default</tt>: Equivalent to <tt>consistent</tt>.</li> 427 <li><tt>consistent</tt>: Full consistency. The container runtime and the host maintain an identical view of the mount at all times.</li> 428 <li><tt>cached</tt>: The host's view of the mount is authoritative. There may be delays before updates made on the host are visible within a container.</li> 429 <li><tt>delegated</tt>: The container runtime's view of the mount is authoritative. There may be delays before updates made in a container are visible on the host.</li> 430 </ul> 431 </td> 432 </tr> 433 <tr> 434 <td><b>bind-nonrecursive</b></td> 435 <td> 436 By default, submounts are recursively bind-mounted as well. However, this behavior can be confusing when a 437 bind mount is configured with <tt>readonly</tt> option, because submounts are not mounted as read-only. 438 Set <tt>bind-nonrecursive</tt> to disable recursive bind-mount.<br /> 439 <br /> 440 A value is optional:<br /> 441 <br /> 442 <ul> 443 <li><tt>true</tt> or <tt>1</tt>: Disables recursive bind-mount.</li> 444 <li><tt>false</tt> or <tt>0</tt>: Default if you do not provide a value. Enables recursive bind-mount.</li> 445 </ul> 446 </td> 447 </tr> 448 </table> 449 450 ##### Bind propagation 451 452 Bind propagation refers to whether or not mounts created within a given 453 bind mount or named volume can be propagated to replicas of that mount. Consider 454 a mount point `/mnt`, which is also mounted on `/tmp`. The propagation settings 455 control whether a mount on `/tmp/a` would also be available on `/mnt/a`. Each 456 propagation setting has a recursive counterpoint. In the case of recursion, 457 consider that `/tmp/a` is also mounted as `/foo`. The propagation settings 458 control whether `/mnt/a` and/or `/tmp/a` would exist. 459 460 The `bind-propagation` option defaults to `rprivate` for both bind mounts and 461 volume mounts, and is only configurable for bind mounts. In other words, named 462 volumes do not support bind propagation. 463 464 - **`shared`**: Sub-mounts of the original mount are exposed to replica mounts, 465 and sub-mounts of replica mounts are also propagated to the 466 original mount. 467 - **`slave`**: similar to a shared mount, but only in one direction. If the 468 original mount exposes a sub-mount, the replica mount can see it. 469 However, if the replica mount exposes a sub-mount, the original 470 mount cannot see it. 471 - **`private`**: The mount is private. Sub-mounts within it are not exposed to 472 replica mounts, and sub-mounts of replica mounts are not 473 exposed to the original mount. 474 - **`rshared`**: The same as shared, but the propagation also extends to and from 475 mount points nested within any of the original or replica mount 476 points. 477 - **`rslave`**: The same as `slave`, but the propagation also extends to and from 478 mount points nested within any of the original or replica mount 479 points. 480 - **`rprivate`**: The default. The same as `private`, meaning that no mount points 481 anywhere within the original or replica mount points propagate 482 in either direction. 483 484 For more information about bind propagation, see the 485 [Linux kernel documentation for shared subtree](https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt). 486 487 #### Options for named volumes 488 489 The following options can only be used for named volumes (`type=volume`): 490 491 492 <table> 493 <tr> 494 <th>Option</th> 495 <th>Description</th> 496 </tr> 497 <tr> 498 <td><b>volume-driver</b></td> 499 <td> 500 <p>Name of the volume-driver plugin to use for the volume. Defaults to 501 <tt>"local"</tt>, to use the local volume driver to create the volume if the 502 volume does not exist.</p> 503 </td> 504 </tr> 505 <tr> 506 <td><b>volume-label</b></td> 507 <td> 508 One or more custom metadata ("labels") to apply to the volume upon 509 creation. For example, 510 <tt>volume-label=mylabel=hello-world,my-other-label=hello-mars</tt>. For more 511 information about labels, refer to 512 <a href="https://docs.docker.com/config/labels-custom-metadata/">apply custom metadata</a>. 513 </td> 514 </tr> 515 <tr> 516 <td><b>volume-nocopy</b></td> 517 <td> 518 By default, if you attach an empty volume to a container, and files or 519 directories already existed at the mount-path in the container (<tt>dst</tt>), 520 the Engine copies those files and directories into the volume, allowing 521 the host to access them. Set <tt>volume-nocopy</tt> to disable copying files 522 from the container's filesystem to the volume and mount the empty volume.<br /> 523 <br /> 524 A value is optional:<br /> 525 <br /> 526 <ul> 527 <li><tt>true</tt> or <tt>1</tt>: Default if you do not provide a value. Disables copying.</li> 528 <li><tt>false</tt> or <tt>0</tt>: Enables copying.</li> 529 </ul> 530 </td> 531 </tr> 532 <tr> 533 <td><b>volume-opt</b></td> 534 <td> 535 Options specific to a given volume driver, which will be passed to the 536 driver when creating the volume. Options are provided as a comma-separated 537 list of key/value pairs, for example, 538 <tt>volume-opt=some-option=some-value,volume-opt=some-other-option=some-other-value</tt>. 539 For available options for a given driver, refer to that driver's 540 documentation. 541 </td> 542 </tr> 543 </table> 544 545 546 #### Options for tmpfs 547 548 The following options can only be used for tmpfs mounts (`type=tmpfs`); 549 550 551 <table> 552 <tr> 553 <th>Option</th> 554 <th>Description</th> 555 </tr> 556 <tr> 557 <td><b>tmpfs-size</b></td> 558 <td>Size of the tmpfs mount in bytes. Unlimited by default in Linux.</td> 559 </tr> 560 <tr> 561 <td><b>tmpfs-mode</b></td> 562 <td>File mode of the tmpfs in octal. (e.g. <tt>"700"</tt> or <tt>"0700"</tt>.) Defaults to <tt>"1777"</tt> in Linux.</td> 563 </tr> 564 </table> 565 566 567 #### Differences between "--mount" and "--volume" 568 569 The `--mount` flag supports most options that are supported by the `-v` 570 or `--volume` flag for `docker run`, with some important exceptions: 571 572 - The `--mount` flag allows you to specify a volume driver and volume driver 573 options *per volume*, without creating the volumes in advance. In contrast, 574 `docker run` allows you to specify a single volume driver which is shared 575 by all volumes, using the `--volume-driver` flag. 576 577 - The `--mount` flag allows you to specify custom metadata ("labels") for a volume, 578 before the volume is created. 579 580 - When you use `--mount` with `type=bind`, the host-path must refer to an *existing* 581 path on the host. The path will not be created for you and the service will fail 582 with an error if the path does not exist. 583 584 - The `--mount` flag does not allow you to relabel a volume with `Z` or `z` flags, 585 which are used for `selinux` labeling. 586 587 #### Create a service using a named volume 588 589 The following example creates a service that uses a named volume: 590 591 ```console 592 $ docker service create \ 593 --name my-service \ 594 --replicas 3 \ 595 --mount type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round" \ 596 nginx:alpine 597 ``` 598 599 For each replica of the service, the engine requests a volume named "my-volume" 600 from the default ("local") volume driver where the task is deployed. If the 601 volume does not exist, the engine creates a new volume and applies the "color" 602 and "shape" labels. 603 604 When the task is started, the volume is mounted on `/path/in/container/` inside 605 the container. 606 607 Be aware that the default ("local") volume is a locally scoped volume driver. 608 This means that depending on where a task is deployed, either that task gets a 609 *new* volume named "my-volume", or shares the same "my-volume" with other tasks 610 of the same service. Multiple containers writing to a single shared volume can 611 cause data corruption if the software running inside the container is not 612 designed to handle concurrent processes writing to the same location. Also take 613 into account that containers can be re-scheduled by the Swarm orchestrator and 614 be deployed on a different node. 615 616 #### Create a service that uses an anonymous volume 617 618 The following command creates a service with three replicas with an anonymous 619 volume on `/path/in/container`: 620 621 ```console 622 $ docker service create \ 623 --name my-service \ 624 --replicas 3 \ 625 --mount type=volume,destination=/path/in/container \ 626 nginx:alpine 627 ``` 628 629 In this example, no name (`source`) is specified for the volume, so a new volume 630 is created for each task. This guarantees that each task gets its own volume, 631 and volumes are not shared between tasks. Anonymous volumes are removed after 632 the task using them is complete. 633 634 #### Create a service that uses a bind-mounted host directory 635 636 The following example bind-mounts a host directory at `/path/in/container` in 637 the containers backing the service: 638 639 ```console 640 $ docker service create \ 641 --name my-service \ 642 --mount type=bind,source=/path/on/host,destination=/path/in/container \ 643 nginx:alpine 644 ``` 645 646 ### Set service mode (--mode) 647 648 The service mode determines whether this is a _replicated_ service or a _global_ 649 service. A replicated service runs as many tasks as specified, while a global 650 service runs on each active node in the swarm. 651 652 The following command creates a global service: 653 654 ```console 655 $ docker service create \ 656 --name redis_2 \ 657 --mode global \ 658 redis:3.0.6 659 ``` 660 661 ### <a name="constraint"></a> Specify service constraints (--constraint) 662 663 You can limit the set of nodes where a task can be scheduled by defining 664 constraint expressions. Constraint expressions can either use a _match_ (`==`) 665 or _exclude_ (`!=`) rule. Multiple constraints find nodes that satisfy every 666 expression (AND match). Constraints can match node or Docker Engine labels as 667 follows: 668 669 | node attribute | matches | example | 670 |----------------------|--------------------------------|-----------------------------------------------| 671 | `node.id` | Node ID | `node.id==2ivku8v2gvtg4` | 672 | `node.hostname` | Node hostname | `node.hostname!=node-2` | 673 | `node.role` | Node role (`manager`/`worker`) | `node.role==manager` | 674 | `node.platform.os` | Node operating system | `node.platform.os==windows` | 675 | `node.platform.arch` | Node architecture | `node.platform.arch==x86_64` | 676 | `node.labels` | User-defined node labels | `node.labels.security==high` | 677 | `engine.labels` | Docker Engine's labels | `engine.labels.operatingsystem==ubuntu-22.04` | 678 679 `engine.labels` apply to Docker Engine labels like operating system, drivers, 680 etc. Swarm administrators add `node.labels` for operational purposes by using 681 the [`docker node update`](node_update.md) command. 682 683 For example, the following limits tasks for the redis service to nodes where the 684 node type label equals queue: 685 686 ```console 687 $ docker service create \ 688 --name redis_2 \ 689 --constraint node.platform.os==linux \ 690 --constraint node.labels.type==queue \ 691 redis:3.0.6 692 ``` 693 694 If the service constraints exclude all nodes in the cluster, a message is printed 695 that no suitable node is found, but the scheduler will start a reconciliation 696 loop and deploy the service once a suitable node becomes available. 697 698 In the example below, no node satisfying the constraint was found, causing the 699 service to not reconcile with the desired state: 700 701 ```console 702 $ docker service create \ 703 --name web \ 704 --constraint node.labels.region==east \ 705 nginx:alpine 706 707 lx1wrhhpmbbu0wuk0ybws30bc 708 overall progress: 0 out of 1 tasks 709 1/1: no suitable node (scheduling constraints not satisfied on 5 nodes) 710 711 $ docker service ls 712 ID NAME MODE REPLICAS IMAGE PORTS 713 b6lww17hrr4e web replicated 0/1 nginx:alpine 714 ``` 715 716 After adding the `region=east` label to a node in the cluster, the service 717 reconciles, and the desired number of replicas are deployed: 718 719 ```console 720 $ docker node update --label-add region=east yswe2dm4c5fdgtsrli1e8ya5l 721 yswe2dm4c5fdgtsrli1e8ya5l 722 723 $ docker service ls 724 ID NAME MODE REPLICAS IMAGE PORTS 725 b6lww17hrr4e web replicated 1/1 nginx:alpine 726 ``` 727 728 ### <a name="placement-pref"></a> Specify service placement preferences (--placement-pref) 729 730 You can set up the service to divide tasks evenly over different categories of 731 nodes. One example of where this can be useful is to balance tasks over a set 732 of datacenters or availability zones. The example below illustrates this: 733 734 ```console 735 $ docker service create \ 736 --replicas 9 \ 737 --name redis_2 \ 738 --placement-pref spread=node.labels.datacenter \ 739 redis:3.0.6 740 ``` 741 742 This uses `--placement-pref` with a `spread` strategy (currently the only 743 supported strategy) to spread tasks evenly over the values of the `datacenter` 744 node label. In this example, we assume that every node has a `datacenter` node 745 label attached to it. If there are three different values of this label among 746 nodes in the swarm, one third of the tasks will be placed on the nodes 747 associated with each value. This is true even if there are more nodes with one 748 value than another. For example, consider the following set of nodes: 749 750 - Three nodes with `node.labels.datacenter=east` 751 - Two nodes with `node.labels.datacenter=south` 752 - One node with `node.labels.datacenter=west` 753 754 Since we are spreading over the values of the `datacenter` label and the 755 service has 9 replicas, 3 replicas will end up in each datacenter. There are 756 three nodes associated with the value `east`, so each one will get one of the 757 three replicas reserved for this value. There are two nodes with the value 758 `south`, and the three replicas for this value will be divided between them, 759 with one receiving two replicas and another receiving just one. Finally, `west` 760 has a single node that will get all three replicas reserved for `west`. 761 762 If the nodes in one category (for example, those with 763 `node.labels.datacenter=south`) can't handle their fair share of tasks due to 764 constraints or resource limitations, the extra tasks will be assigned to other 765 nodes instead, if possible. 766 767 Both engine labels and node labels are supported by placement preferences. The 768 example above uses a node label, because the label is referenced with 769 `node.labels.datacenter`. To spread over the values of an engine label, use 770 `--placement-pref spread=engine.labels.<labelname>`. 771 772 It is possible to add multiple placement preferences to a service. This 773 establishes a hierarchy of preferences, so that tasks are first divided over 774 one category, and then further divided over additional categories. One example 775 of where this may be useful is dividing tasks fairly between datacenters, and 776 then splitting the tasks within each datacenter over a choice of racks. To add 777 multiple placement preferences, specify the `--placement-pref` flag multiple 778 times. The order is significant, and the placement preferences will be applied 779 in the order given when making scheduling decisions. 780 781 The following example sets up a service with multiple placement preferences. 782 Tasks are spread first over the various datacenters, and then over racks 783 (as indicated by the respective labels): 784 785 ```console 786 $ docker service create \ 787 --replicas 9 \ 788 --name redis_2 \ 789 --placement-pref 'spread=node.labels.datacenter' \ 790 --placement-pref 'spread=node.labels.rack' \ 791 redis:3.0.6 792 ``` 793 794 When updating a service with `docker service update`, `--placement-pref-add` 795 appends a new placement preference after all existing placement preferences. 796 `--placement-pref-rm` removes an existing placement preference that matches the 797 argument. 798 799 ### <a name="reserve-memory"></a> Specify memory requirements and constraints for a service (--reserve-memory and --limit-memory) 800 801 If your service needs a minimum amount of memory in order to run correctly, 802 you can use `--reserve-memory` to specify that the service should only be 803 scheduled on a node with this much memory available to reserve. If no node is 804 available that meets the criteria, the task is not scheduled, but remains in a 805 pending state. 806 807 The following example requires that 4GB of memory be available and reservable 808 on a given node before scheduling the service to run on that node. 809 810 ```console 811 $ docker service create --reserve-memory=4GB --name=too-big nginx:alpine 812 ``` 813 814 The managers won't schedule a set of containers on a single node whose combined 815 reservations exceed the memory available on that node. 816 817 After a task is scheduled and running, `--reserve-memory` does not enforce a 818 memory limit. Use `--limit-memory` to ensure that a task uses no more than a 819 given amount of memory on a node. This example limits the amount of memory used 820 by the task to 4GB. The task will be scheduled even if each of your nodes has 821 only 2GB of memory, because `--limit-memory` is an upper limit. 822 823 ```console 824 $ docker service create --limit-memory=4GB --name=too-big nginx:alpine 825 ``` 826 827 Using `--reserve-memory` and `--limit-memory` does not guarantee that Docker 828 will not use more memory on your host than you want. For instance, you could 829 create many services, the sum of whose memory usage could exhaust the available 830 memory. 831 832 You can prevent this scenario from exhausting the available memory by taking 833 into account other (non-containerized) software running on the host as well. If 834 `--reserve-memory` is greater than or equal to `--limit-memory`, Docker won't 835 schedule a service on a host that doesn't have enough memory. `--limit-memory` 836 will limit the service's memory to stay within that limit, so if every service 837 has a memory-reservation and limit set, Docker services will be less likely to 838 saturate the host. Other non-service containers or applications running directly 839 on the Docker host could still exhaust memory. 840 841 There is a downside to this approach. Reserving memory also means that you may 842 not make optimum use of the memory available on the node. Consider a service 843 that under normal circumstances uses 100MB of memory, but depending on load can 844 "peak" at 500MB. Reserving 500MB for that service (to guarantee can have 500MB 845 for those "peaks") results in 400MB of memory being wasted most of the time. 846 847 In short, you can take a more conservative or more flexible approach: 848 849 - **Conservative**: reserve 500MB, and limit to 500MB. Basically you're now 850 treating the service containers as VMs, and you may be losing a big advantage 851 containers, which is greater density of services per host. 852 853 - **Flexible**: limit to 500MB in the assumption that if the service requires 854 more than 500MB, it is malfunctioning. Reserve something between the 100MB 855 "normal" requirement and the 500MB "peak" requirement". This assumes that when 856 this service is at "peak", other services or non-container workloads probably 857 won't be. 858 859 The approach you take depends heavily on the memory-usage patterns of your 860 workloads. You should test under normal and peak conditions before settling 861 on an approach. 862 863 On Linux, you can also limit a service's overall memory footprint on a given 864 host at the level of the host operating system, using `cgroups` or other 865 relevant operating system tools. 866 867 ### <a name="replicas-max-per-node"></a> Specify maximum replicas per node (--replicas-max-per-node) 868 869 Use the `--replicas-max-per-node` flag to set the maximum number of replica tasks that can run on a node. 870 The following command creates a nginx service with 2 replica tasks but only one replica task per node. 871 872 One example where this can be useful is to balance tasks over a set of data centers together with `--placement-pref` 873 and let `--replicas-max-per-node` setting make sure that replicas are not migrated to another datacenter during 874 maintenance or datacenter failure. 875 876 The example below illustrates this: 877 878 ```console 879 $ docker service create \ 880 --name nginx \ 881 --replicas 2 \ 882 --replicas-max-per-node 1 \ 883 --placement-pref 'spread=node.labels.datacenter' \ 884 nginx 885 ``` 886 887 ### <a name="network"></a> Attach a service to an existing network (--network) 888 889 You can use overlay networks to connect one or more services within the swarm. 890 891 First, create an overlay network on a manager node the docker network create 892 command: 893 894 ```console 895 $ docker network create --driver overlay my-network 896 897 etjpu59cykrptrgw0z0hk5snf 898 ``` 899 900 After you create an overlay network in swarm mode, all manager nodes have 901 access to the network. 902 903 When you create a service and pass the `--network` flag to attach the service to 904 the overlay network: 905 906 ```console 907 $ docker service create \ 908 --replicas 3 \ 909 --network my-network \ 910 --name my-web \ 911 nginx 912 913 716thylsndqma81j6kkkb5aus 914 ``` 915 916 The swarm extends my-network to each node running the service. 917 918 Containers on the same network can access each other using 919 [service discovery](https://docs.docker.com/network/overlay/#container-discovery). 920 921 Long form syntax of `--network` allows to specify list of aliases and driver options: 922 `--network name=my-network,alias=web1,driver-opt=field1=value1` 923 924 ### <a name="publish"></a> Publish service ports externally to the swarm (-p, --publish) 925 926 You can publish service ports to make them available externally to the swarm 927 using the `--publish` flag. The `--publish` flag can take two different styles 928 of arguments. The short version is positional, and allows you to specify the 929 published port and target port separated by a colon (`:`). 930 931 ```console 932 $ docker service create --name my_web --replicas 3 --publish 8080:80 nginx 933 ``` 934 935 There is also a long format, which is easier to read and allows you to specify 936 more options. The long format is preferred. You cannot specify the service's 937 mode when using the short format. Here is an example of using the long format 938 for the same service as above: 939 940 ```console 941 $ docker service create --name my_web --replicas 3 --publish published=8080,target=80 nginx 942 ``` 943 944 The options you can specify are: 945 946 <table> 947 <thead> 948 <tr> 949 <th>Option</th> 950 <th>Short syntax</th> 951 <th>Long syntax</th> 952 <th>Description</th> 953 </tr> 954 </thead> 955 <tr> 956 <td>published and target port</td> 957 <td><tt>--publish 8080:80</tt></td> 958 <td><tt>--publish published=8080,target=80</tt></td> 959 <td><p> 960 The target port within the container and the port to map it to on the 961 nodes, using the routing mesh (<tt>ingress</tt>) or host-level networking. 962 More options are available, later in this table. The key-value syntax is 963 preferred, because it is somewhat self-documenting. 964 </p></td> 965 </tr> 966 <tr> 967 <td>mode</td> 968 <td>Not possible to set using short syntax.</td> 969 <td><tt>--publish published=8080,target=80,mode=host</tt></td> 970 <td><p> 971 The mode to use for binding the port, either <tt>ingress</tt> or <tt>host</tt>. 972 Defaults to <tt>ingress</tt> to use the routing mesh. 973 </p></td> 974 </tr> 975 <tr> 976 <td>protocol</td> 977 <td><tt>--publish 8080:80/tcp</tt></td> 978 <td><tt>--publish published=8080,target=80,protocol=tcp</tt></td> 979 <td><p> 980 The protocol to use, <tt>tcp</tt> , <tt>udp</tt>, or <tt>sctp</tt>. Defaults to 981 <tt>tcp</tt>. To bind a port for both protocols, specify the <tt>-p</tt> or 982 <tt>--publish</tt> flag twice. 983 </p></td> 984 </tr> 985 </table> 986 987 When you publish a service port using `ingress` mode, the swarm routing mesh 988 makes the service accessible at the published port on every node regardless if 989 there is a task for the service running on the node. If you use `host` mode, 990 the port is only bound on nodes where the service is running, and a given port 991 on a node can only be bound once. You can only set the publication mode using 992 the long syntax. For more information refer to 993 [Use swarm mode routing mesh](https://docs.docker.com/engine/swarm/ingress/). 994 995 ### <a name="credentials-spec"></a> Provide credential specs for managed service accounts (--credentials-spec) 996 997 This option is only used for services using Windows containers. The 998 `--credential-spec` must be in the format `file://<filename>` or 999 `registry://<value-name>`. 1000 1001 When using the `file://<filename>` format, the referenced file must be 1002 present in the `CredentialSpecs` subdirectory in the docker data directory, 1003 which defaults to `C:\ProgramData\Docker\` on Windows. For example, 1004 specifying `file://spec.json` loads `C:\ProgramData\Docker\CredentialSpecs\spec.json`. 1005 1006 When using the `registry://<value-name>` format, the credential spec is 1007 read from the Windows registry on the daemon's host. The specified 1008 registry value must be located in: 1009 1010 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\Containers\CredentialSpecs 1011 1012 1013 ### Create services using templates 1014 1015 You can use templates for some flags of `service create`, using the syntax 1016 provided by the Go's [text/template](https://golang.org/pkg/text/template/) package. 1017 1018 The supported flags are the following : 1019 1020 - `--hostname` 1021 - `--mount` 1022 - `--env` 1023 1024 Valid placeholders for the Go template are listed below: 1025 1026 1027 <table> 1028 <tr> 1029 <th>Placeholder</th> 1030 <th>Description</th> 1031 </tr> 1032 <tr> 1033 <td><tt>.Service.ID</tt></td> 1034 <td>Service ID</td> 1035 </tr> 1036 <tr> 1037 <td><tt>.Service.Name</tt></td> 1038 <td>Service name</td> 1039 </tr> 1040 <tr> 1041 <td><tt>.Service.Labels</tt></td> 1042 <td>Service labels</td> 1043 </tr> 1044 <tr> 1045 <td><tt>.Node.ID</tt></td> 1046 <td>Node ID</td> 1047 </tr> 1048 <tr> 1049 <td><tt>.Node.Hostname</tt></td> 1050 <td>Node Hostname</td> 1051 </tr> 1052 <tr> 1053 <td><tt>.Task.ID</tt></td> 1054 <td>Task ID</td> 1055 </tr> 1056 <tr> 1057 <td><tt>.Task.Name</tt></td> 1058 <td>Task name</td> 1059 </tr> 1060 <tr> 1061 <td><tt>.Task.Slot</tt></td> 1062 <td>Task slot</td> 1063 </tr> 1064 </table> 1065 1066 1067 #### Template example 1068 1069 In this example, we are going to set the template of the created containers based on the 1070 service's name, the node's ID and hostname where it sits. 1071 1072 ```console 1073 $ docker service create \ 1074 --name hosttempl \ 1075 --hostname="{{.Node.Hostname}}-{{.Node.ID}}-{{.Service.Name}}"\ 1076 busybox top 1077 1078 va8ew30grofhjoychbr6iot8c 1079 1080 $ docker service ps va8ew30grofhjoychbr6iot8c 1081 1082 ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 1083 wo41w8hg8qan hosttempl.1 busybox:latest@sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912 2e7a8a9c4da2 Running Running about a minute ago 1084 1085 $ docker inspect --format="{{.Config.Hostname}}" 2e7a8a9c4da2-wo41w8hg8qanxwjwsg4kxpprj-hosttempl 1086 1087 x3ti0erg11rjpg64m75kej2mz-hosttempl 1088 ``` 1089 1090 ### <a name="isolation"></a> Specify isolation mode on Windows (--isolation) 1091 1092 By default, tasks scheduled on Windows nodes are run using the default isolation mode 1093 configured for this particular node. To force a specific isolation mode, you can use 1094 the `--isolation` flag: 1095 1096 ```console 1097 $ docker service create --name myservice --isolation=process microsoft/nanoserver 1098 ``` 1099 1100 Supported isolation modes on Windows are: 1101 - `default`: use default settings specified on the node running the task 1102 - `process`: use process isolation (Windows server only) 1103 - `hyperv`: use Hyper-V isolation 1104 1105 ### <a name="generic-resources"></a> Create services requesting Generic Resources (--generic-resources) 1106 1107 You can narrow the kind of nodes your task can land on through the using the 1108 `--generic-resource` flag (if the nodes advertise these resources): 1109 1110 ```console 1111 $ docker service create \ 1112 --name cuda \ 1113 --generic-resource "NVIDIA-GPU=2" \ 1114 --generic-resource "SSD=1" \ 1115 nvidia/cuda 1116 ``` 1117 1118 ### Running as a job 1119 1120 Jobs are a special kind of service designed to run an operation to completion 1121 and then stop, as opposed to running long-running daemons. When a Task 1122 belonging to a job exits successfully (return value 0), the Task is marked as 1123 "Completed", and is not run again. 1124 1125 Jobs are started by using one of two modes, `replicated-job` or `global-job` 1126 1127 ```console 1128 $ docker service create --name myjob \ 1129 --mode replicated-job \ 1130 bash "true" 1131 ``` 1132 1133 This command will run one Task, which will, using the `bash` image, execute the 1134 command `true`, which will return 0 and then exit. 1135 1136 Though Jobs are ultimately a different kind of service, they a couple of 1137 caveats compared to other services: 1138 1139 - None of the update or rollback configuration options are valid. Jobs can be 1140 updated, but cannot be rolled out or rolled back, making these configuration 1141 options moot. 1142 - Jobs are never restarted on reaching the `Complete` state. This means that 1143 for jobs, setting `--restart-condition` to `any` is the same as setting it to 1144 `on-failure`. 1145 1146 Jobs are available in both replicated and global modes. 1147 1148 #### Replicated Jobs 1149 1150 A replicated job is like a replicated service. Setting the `--replicas` flag 1151 will specify total number of iterations of a job to execute. 1152 1153 By default, all replicas of a replicated job will launch at once. To control 1154 the total number of replicas that are executing simultaneously at any one time, 1155 the `--max-concurrent` flag can be used: 1156 1157 ```console 1158 $ docker service create \ 1159 --name mythrottledjob \ 1160 --mode replicated-job \ 1161 --replicas 10 \ 1162 --max-concurrent 2 \ 1163 bash "true" 1164 ``` 1165 1166 The above command will execute 10 Tasks in total, but only 2 of them will be 1167 run at any given time. 1168 1169 #### Global Jobs 1170 1171 Global jobs are like global services, in that a Task is executed once on each node 1172 matching placement constraints. Global jobs are represented by the mode `global-job`. 1173 1174 Note that after a Global job is created, any new Nodes added to the cluster 1175 will have a Task from that job started on them. The Global Job does not as a 1176 whole have a "done" state, except insofar as every Node meeting the job's 1177 constraints has a Completed task. 1178 1179 ## Related commands 1180 1181 * [service inspect](service_inspect.md) 1182 * [service logs](service_logs.md) 1183 * [service ls](service_ls.md) 1184 * [service ps](service_ps.md) 1185 * [service rm](service_rm.md) 1186 * [service rollback](service_rollback.md) 1187 * [service scale](service_scale.md) 1188 * [service update](service_update.md) 1189 1190 <style>table tr > td:first-child { white-space: nowrap;}</style>