github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/cli/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/cli GitHub
     8       repository at https://github.com/docker/cli/. 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        --config config                      Specify configurations to expose to the service
    25        --constraint list                    Placement constraints
    26        --container-label list               Container labels
    27        --credential-spec credential-spec    Credential spec for managed service account (Windows only)
    28    -d, --detach                             Exit immediately instead of waiting for the service to converge (default true)
    29        --dns list                           Set custom DNS servers
    30        --dns-option list                    Set DNS options
    31        --dns-search list                    Set custom DNS search domains
    32        --endpoint-mode string               Endpoint mode (vip or dnsrr) (default "vip")
    33        --entrypoint command                 Overwrite the default ENTRYPOINT of the image
    34    -e, --env list                           Set environment variables
    35        --env-file list                      Read in a file of environment variables
    36        --generic-resource list              User defined resources request
    37        --group list                         Set one or more supplementary user groups for the container
    38        --health-cmd string                  Command to run to check health
    39        --health-interval duration           Time between running the check (ms|s|m|h)
    40        --health-retries int                 Consecutive failures needed to report unhealthy
    41        --health-start-period duration       Start period for the container to initialize before counting retries towards unstable (ms|s|m|h)
    42        --health-timeout duration            Maximum time to allow one check to run (ms|s|m|h)
    43        --help                               Print usage
    44        --host list                          Set one or more custom host-to-IP mappings (host:ip)
    45        --hostname string                    Container hostname
    46        --isolation string                   Service container isolation mode
    47    -l, --label list                         Service labels
    48        --limit-cpu decimal                  Limit CPUs
    49        --limit-memory bytes                 Limit Memory
    50        --log-driver string                  Logging driver for service
    51        --log-opt list                       Logging driver options
    52        --mode string                        Service mode (replicated or global) (default "replicated")
    53        --mount mount                        Attach a filesystem mount to the service
    54        --name string                        Service name
    55        --network network                    Network attachments
    56        --no-healthcheck                     Disable any container-specified HEALTHCHECK
    57        --no-resolve-image                   Do not query the registry to resolve image digest and supported platforms
    58        --placement-pref pref                Add a placement preference
    59    -p, --publish port                       Publish a port as a node port
    60    -q, --quiet                              Suppress progress output
    61        --read-only                          Mount the container's root filesystem as read only
    62        --replicas uint                      Number of tasks
    63        --reserve-cpu decimal                Reserve CPUs
    64        --reserve-memory bytes               Reserve Memory
    65        --restart-condition string           Restart when condition is met ("none"|"on-failure"|"any") (default "any")
    66        --restart-delay duration             Delay between restart attempts (ns|us|ms|s|m|h) (default 5s)
    67        --restart-max-attempts uint          Maximum number of restarts before giving up
    68        --restart-window duration            Window used to evaluate the restart policy (ns|us|ms|s|m|h)
    69        --rollback-delay duration            Delay between task rollbacks (ns|us|ms|s|m|h) (default 0s)
    70        --rollback-failure-action string     Action on rollback failure ("pause"|"continue") (default "pause")
    71        --rollback-max-failure-ratio float   Failure rate to tolerate during a rollback (default 0)
    72        --rollback-monitor duration          Duration after each task rollback to monitor for failure (ns|us|ms|s|m|h) (default 5s)
    73        --rollback-order string              Rollback order ("start-first"|"stop-first") (default "stop-first")
    74        --rollback-parallelism uint          Maximum number of tasks rolled back simultaneously (0 to roll back all at once) (default 1)
    75        --secret secret                      Specify secrets to expose to the service
    76        --stop-grace-period duration         Time to wait before force killing a container (ns|us|ms|s|m|h) (default 10s)
    77        --stop-signal string                 Signal to stop the container
    78    -t, --tty                                Allocate a pseudo-TTY
    79        --update-delay duration              Delay between updates (ns|us|ms|s|m|h) (default 0s)
    80        --update-failure-action string       Action on update failure ("pause"|"continue"|"rollback") (default "pause")
    81        --update-max-failure-ratio float     Failure rate to tolerate during an update (default 0)
    82        --update-monitor duration            Duration after each task update to monitor for failure (ns|us|ms|s|m|h) (default 5s)
    83        --update-order string                Update order ("start-first"|"stop-first") (default "stop-first")
    84        --update-parallelism uint            Maximum number of tasks updated simultaneously (0 to update all at once) (default 1)
    85    -u, --user string                        Username or UID (format: <name|uid>[:<group|gid>])
    86        --with-registry-auth                 Send registry authentication details to swarm agents
    87    -w, --workdir string                     Working directory inside the container
    88  ```
    89  
    90  ## Description
    91  
    92  Creates a service as described by the specified parameters. You must run this
    93  command on a manager node.
    94  
    95  ## Examples
    96  
    97  ### Create a service
    98  
    99  ```bash
   100  $ docker service create --name redis redis:3.0.6
   101  
   102  dmu1ept4cxcfe8k8lhtux3ro3
   103  
   104  $ docker service create --mode global --name redis2 redis:3.0.6
   105  
   106  a8q9dasaafudfs8q8w32udass
   107  
   108  $ docker service ls
   109  
   110  ID            NAME    MODE        REPLICAS  IMAGE
   111  dmu1ept4cxcf  redis   replicated  1/1       redis:3.0.6
   112  a8q9dasaafud  redis2  global      1/1       redis:3.0.6
   113  ```
   114  
   115  ### Create a service with 5 replica tasks (--replicas)
   116  
   117  Use the `--replicas` flag to set the number of replica tasks for a replicated
   118  service. The following command creates a `redis` service with `5` replica tasks:
   119  
   120  ```bash
   121  $ docker service create --name redis --replicas=5 redis:3.0.6
   122  
   123  4cdgfyky7ozwh3htjfw0d12qv
   124  ```
   125  
   126  The above command sets the *desired* number of tasks for the service. Even
   127  though the command returns immediately, actual scaling of the service may take
   128  some time. The `REPLICAS` column shows both the *actual* and *desired* number
   129  of replica tasks for the service.
   130  
   131  In the following example the desired state is  `5` replicas, but the current
   132  number of `RUNNING` tasks is `3`:
   133  
   134  ```bash
   135  $ docker service ls
   136  
   137  ID            NAME   MODE        REPLICAS  IMAGE
   138  4cdgfyky7ozw  redis  replicated  3/5       redis:3.0.7
   139  ```
   140  
   141  Once all the tasks are created and `RUNNING`, the actual number of tasks is
   142  equal to the desired number:
   143  
   144  ```bash
   145  $ docker service ls
   146  
   147  ID            NAME   MODE        REPLICAS  IMAGE
   148  4cdgfyky7ozw  redis  replicated  5/5       redis:3.0.7
   149  ```
   150  
   151  ### Create a service with secrets
   152  
   153  Use the `--secret` flag to give a container access to a
   154  [secret](secret_create.md).
   155  
   156  Create a service specifying a secret:
   157  
   158  ```bash
   159  $ docker service create --name redis --secret secret.json redis:3.0.6
   160  
   161  4cdgfyky7ozwh3htjfw0d12qv
   162  ```
   163  
   164  Create a service specifying the secret, target, user/group ID, and mode:
   165  
   166  ```bash
   167  $ docker service create --name redis \
   168      --secret source=ssh-key,target=ssh \
   169      --secret source=app-key,target=app,uid=1000,gid=1001,mode=0400 \
   170      redis:3.0.6
   171  
   172  4cdgfyky7ozwh3htjfw0d12qv
   173  ```
   174  
   175  To grant a service access to multiple secrets, use multiple `--secret` flags.
   176  
   177  Secrets are located in `/run/secrets` in the container.  If no target is
   178  specified, the name of the secret will be used as the in memory file in the
   179  container.  If a target is specified, that will be the filename.  In the
   180  example above, two files will be created: `/run/secrets/ssh` and
   181  `/run/secrets/app` for each of the secret targets specified.
   182  
   183  ### Create a service with a rolling update policy
   184  
   185  ```bash
   186  $ docker service create \
   187    --replicas 10 \
   188    --name redis \
   189    --update-delay 10s \
   190    --update-parallelism 2 \
   191    redis:3.0.6
   192  ```
   193  
   194  When you run a [service update](service_update.md), the scheduler updates a
   195  maximum of 2 tasks at a time, with `10s` between updates. For more information,
   196  refer to the [rolling updates
   197  tutorial](https://docs.docker.com/engine/swarm/swarm-tutorial/rolling-update/).
   198  
   199  ### Set environment variables (-e, --env)
   200  
   201  This sets an environmental variable for all tasks in a service. For example:
   202  
   203  ```bash
   204  $ docker service create \
   205    --name redis_2 \
   206    --replicas 5 \
   207    --env MYVAR=foo \
   208    redis:3.0.6
   209  ```
   210  
   211  To specify multiple environment variables, specify multiple `--env` flags, each
   212  with a separate key-value pair.
   213  
   214  ```bash
   215  $ docker service create \
   216    --name redis_2 \
   217    --replicas 5 \
   218    --env MYVAR=foo \
   219    --env MYVAR2=bar \
   220    redis:3.0.6
   221  ```
   222  
   223  ### Create a service with specific hostname (--hostname)
   224  
   225  This option sets the docker service containers hostname to a specific string.
   226  For example:
   227  
   228  ```bash
   229  $ docker service create --name redis --hostname myredis redis:3.0.6
   230  ```
   231  
   232  ### Set metadata on a service (-l, --label)
   233  
   234  A label is a `key=value` pair that applies metadata to a service. To label a
   235  service with two labels:
   236  
   237  ```bash
   238  $ docker service create \
   239    --name redis_2 \
   240    --label com.example.foo="bar"
   241    --label bar=baz \
   242    redis:3.0.6
   243  ```
   244  
   245  For more information about labels, refer to [apply custom
   246  metadata](https://docs.docker.com/engine/userguide/labels-custom-metadata/).
   247  
   248  ### Add bind mounts, volumes or memory filesystems
   249  
   250  Docker supports three different kinds of mounts, which allow containers to read
   251  from or write to files or directories, either on the host operating system, or
   252  on memory filesystems. These types are _data volumes_ (often referred to simply
   253  as volumes), _bind mounts_, and _tmpfs_.
   254  
   255  A **bind mount** makes a file or directory on the host available to the
   256  container it is mounted within. A bind mount may be either read-only or
   257  read-write. For example, a container might share its host's DNS information by
   258  means of a bind mount of the host's `/etc/resolv.conf` or a container might
   259  write logs to its host's `/var/log/myContainerLogs` directory. If you use
   260  bind mounts and your host and containers have different notions of permissions,
   261  access controls, or other such details, you will run into portability issues.
   262  
   263  A **named volume** is a mechanism for decoupling persistent data needed by your
   264  container from the image used to create the container and from the host machine.
   265  Named volumes are created and managed by Docker, and a named volume persists
   266  even when no container is currently using it. Data in named volumes can be
   267  shared between a container and the host machine, as well as between multiple
   268  containers. Docker uses a _volume driver_ to create, manage, and mount volumes.
   269  You can back up or restore volumes using Docker commands.
   270  
   271  A **tmpfs** mounts a tmpfs inside a container for volatile data.
   272  
   273  Consider a situation where your image starts a lightweight web server. You could
   274  use that image as a base image, copy in your website's HTML files, and package
   275  that into another image. Each time your website changed, you'd need to update
   276  the new image and redeploy all of the containers serving your website. A better
   277  solution is to store the website in a named volume which is attached to each of
   278  your web server containers when they start. To update the website, you just
   279  update the named volume.
   280  
   281  For more information about named volumes, see
   282  [Data Volumes](https://docs.docker.com/engine/tutorials/dockervolumes/).
   283  
   284  The following table describes options which apply to both bind mounts and named
   285  volumes in a service:
   286  
   287  <table>
   288    <tr>
   289      <th>Option</th>
   290      <th>Required</th>
   291      <th>Description</th>
   292    </tr>
   293    <tr>
   294      <td><b>types</b></td>
   295      <td></td>
   296      <td>
   297        <p>The type of mount, can be either <tt>volume</tt>, <tt>bind</tt>, or <tt>tmpfs</tt>. Defaults to <tt>volume</tt> if no type is specified.
   298        <ul>
   299          <li><tt>volume</tt>: mounts a <a href="https://docs.docker.com/engine/reference/commandline/volume_create/">managed volume</a>
   300          into the container.</li> <li><tt>bind</tt>:
   301          bind-mounts a directory or file from the host into the container.</li>
   302          <li><tt>tmpfs</tt>: mount a tmpfs in the container</li>
   303        </ul></p>
   304      </td>
   305    </tr>
   306    <tr>
   307      <td><b>src</b> or <b>source</b></td>
   308      <td>for <tt>type=bind</tt> only></td>
   309      <td>
   310        <ul>
   311          <li>
   312           <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>).
   313            If the named volume does not exist, it is automatically created. If no <tt>src</tt> is specified, the volume is
   314            assigned a random name which is guaranteed to be unique on the host, but may not be unique cluster-wide.
   315            A randomly-named volume has the same lifecycle as its container and is destroyed when the <i>container</i>
   316            is destroyed (which is upon <tt>service update</tt>, or when scaling or re-balancing the service)
   317          </li>
   318          <li>
   319            <tt>type=bind</tt>: <tt>src</tt> is required, and specifies an absolute path to the file or directory to bind-mount
   320            (for example, <tt>src=/path/on/host/</tt>). An error is produced if the file or directory does not exist.
   321          </li>
   322          <li>
   323            <tt>type=tmpfs</tt>: <tt>src</tt> is not supported.
   324          </li>
   325        </ul>
   326      </td>
   327    </tr>
   328    <tr>
   329      <td><p><b>dst</b> or <b>destination</b> or <b>target</b></p></td>
   330      <td>yes</td>
   331      <td>
   332        <p>Mount path inside the container, for example <tt>/some/path/in/container/</tt>.
   333        If the path does not exist in the container's filesystem, the Engine creates
   334        a directory at the specified location before mounting the volume or bind mount.</p>
   335      </td>
   336    </tr>
   337    <tr>
   338      <td><p><b>readonly</b> or <b>ro</b></p></td>
   339      <td></td>
   340      <td>
   341        <p>The Engine mounts binds and volumes <tt>read-write</tt> unless <tt>readonly</tt> option
   342        is given when mounting the bind or volume.
   343        <ul>
   344          <li><tt>true</tt> or <tt>1</tt> or no value: Mounts the bind or volume read-only.</li>
   345          <li><tt>false</tt> or <tt>0</tt>: Mounts the bind or volume read-write.</li>
   346        </ul></p>
   347      </td>
   348    </tr>
   349    <tr>
   350      <td><b>consistency</b></td>
   351      <td></td>
   352      <td>
   353        <p>The consistency requirements for the mount; one of
   354           <ul>
   355             <li><tt>default</tt>: Equivalent to <tt>consistent</tt>.</li>
   356             <li><tt>consistent</tt>: Full consistency.  The container runtime and the host maintain an identical view of the mount at all times.</li>
   357             <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>
   358             <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>
   359          </ul>
   360       </p>
   361      </td>
   362    </tr>
   363  </table>
   364  
   365  #### Bind Propagation
   366  
   367  Bind propagation refers to whether or not mounts created within a given
   368  bind mount or named volume can be propagated to replicas of that mount. Consider
   369  a mount point `/mnt`, which is also mounted on `/tmp`. The propation settings
   370  control whether a mount on `/tmp/a` would also be available on `/mnt/a`. Each
   371  propagation setting has a recursive counterpoint. In the case of recursion,
   372  consider that `/tmp/a` is also mounted as `/foo`. The propagation settings
   373  control whether `/mnt/a` and/or `/tmp/a` would exist.
   374  
   375  The `bind-propagation` option defaults to `rprivate` for both bind mounts and
   376  volume mounts, and is only configurable for bind mounts. In other words, named
   377  volumes do not support bind propagation.
   378  
   379  - **`shared`**: Sub-mounts of the original mount are exposed to replica mounts,
   380                  and sub-mounts of replica mounts are also propagated to the
   381                  original mount.
   382  - **`slave`**: similar to a shared mount, but only in one direction. If the
   383                 original mount exposes a sub-mount, the replica mount can see it.
   384                 However, if the replica mount exposes a sub-mount, the original
   385                 mount cannot see it.
   386  - **`private`**: The mount is private. Sub-mounts within it are not exposed to
   387                   replica mounts, and sub-mounts of replica mounts are not
   388                   exposed to the original mount.
   389  - **`rshared`**: The same as shared, but the propagation also extends to and from
   390                   mount points nested within any of the original or replica mount
   391                   points.
   392  - **`rslave`**: The same as `slave`, but the propagation also extends to and from
   393                   mount points nested within any of the original or replica mount
   394                   points.
   395  - **`rprivate`**: The default. The same as `private`, meaning that no mount points
   396                    anywhere within the original or replica mount points propagate
   397                    in either direction.
   398  
   399  For more information about bind propagation, see the
   400  [Linux kernel documentation for shared subtree](https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt).
   401  
   402  #### Options for Named Volumes
   403  
   404  The following options can only be used for named volumes (`type=volume`):
   405  
   406  
   407  <table>
   408    <tr>
   409      <th>Option</th>
   410      <th>Description</th>
   411    </tr>
   412    <tr>
   413      <td><b>volume-driver</b></td>
   414      <td>
   415        <p>Name of the volume-driver plugin to use for the volume. Defaults to
   416        <tt>"local"</tt>, to use the local volume driver to create the volume if the
   417        volume does not exist.</p>
   418      </td>
   419    </tr>
   420    <tr>
   421      <td><b>volume-label</b></td>
   422      <td>
   423        One or more custom metadata ("labels") to apply to the volume upon
   424        creation. For example,
   425        <tt>volume-label=mylabel=hello-world,my-other-label=hello-mars</tt>. For more
   426        information about labels, refer to
   427        <a href="https://docs.docker.com/engine/userguide/labels-custom-metadata/">apply custom metadata</a>.
   428      </td>
   429    </tr>
   430    <tr>
   431      <td><b>volume-nocopy</b></td>
   432      <td>
   433        By default, if you attach an empty volume to a container, and files or
   434        directories already existed at the mount-path in the container (<tt>dst</tt>),
   435        the Engine copies those files and directories into the volume, allowing
   436        the host to access them. Set <tt>volume-nocopy</tt> to disable copying files
   437        from the container's filesystem to the volume and mount the empty volume.<br />
   438  
   439        A value is optional:
   440  
   441        <ul>
   442          <li><tt>true</tt> or <tt>1</tt>: Default if you do not provide a value. Disables copying.</li>
   443          <li><tt>false</tt> or <tt>0</tt>: Enables copying.</li>
   444        </ul>
   445      </td>
   446    </tr>
   447    <tr>
   448      <td><b>volume-opt</b></td>
   449      <td>
   450        Options specific to a given volume driver, which will be passed to the
   451        driver when creating the volume. Options are provided as a comma-separated
   452        list of key/value pairs, for example,
   453        <tt>volume-opt=some-option=some-value,volume-opt=some-other-option=some-other-value</tt>.
   454        For available options for a given driver, refer to that driver's
   455        documentation.
   456      </td>
   457    </tr>
   458  </table>
   459  
   460  
   461  #### Options for tmpfs
   462  
   463  The following options can only be used for tmpfs mounts (`type=tmpfs`);
   464  
   465  
   466  <table>
   467    <tr>
   468      <th>Option</th>
   469      <th>Description</th>
   470    </tr>
   471    <tr>
   472      <td><b>tmpfs-size</b></td>
   473      <td>Size of the tmpfs mount in bytes. Unlimited by default in Linux.</td>
   474    </tr>
   475    <tr>
   476      <td><b>tmpfs-mode</b></td>
   477      <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>
   478    </tr>
   479  </table>
   480  
   481  
   482  #### Differences between "--mount" and "--volume"
   483  
   484  The `--mount` flag supports most options that are supported by the `-v`
   485  or `--volume` flag for `docker run`, with some important exceptions:
   486  
   487  - The `--mount` flag allows you to specify a volume driver and volume driver
   488    options *per volume*, without creating the volumes in advance. In contrast,
   489    `docker run` allows you to specify a single volume driver which is shared
   490    by all volumes, using the `--volume-driver` flag.
   491  
   492  - The `--mount` flag allows you to specify custom metadata ("labels") for a volume,
   493    before the volume is created.
   494  
   495  - When you use `--mount` with `type=bind`, the host-path must refer to an *existing*
   496    path on the host. The path will not be created for you and the service will fail
   497    with an error if the path does not exist.
   498  
   499  - The `--mount` flag does not allow you to relabel a volume with `Z` or `z` flags,
   500    which are used for `selinux` labeling.
   501  
   502  #### Create a service using a named volume
   503  
   504  The following example creates a service that uses a named volume:
   505  
   506  ```bash
   507  $ docker service create \
   508    --name my-service \
   509    --replicas 3 \
   510    --mount type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round" \
   511    nginx:alpine
   512  ```
   513  
   514  For each replica of the service, the engine requests a volume named "my-volume"
   515  from the default ("local") volume driver where the task is deployed. If the
   516  volume does not exist, the engine creates a new volume and applies the "color"
   517  and "shape" labels.
   518  
   519  When the task is started, the volume is mounted on `/path/in/container/` inside
   520  the container.
   521  
   522  Be aware that the default ("local") volume is a locally scoped volume driver.
   523  This means that depending on where a task is deployed, either that task gets a
   524  *new* volume named "my-volume", or shares the same "my-volume" with other tasks
   525  of the same service. Multiple containers writing to a single shared volume can
   526  cause data corruption if the software running inside the container is not
   527  designed to handle concurrent processes writing to the same location. Also take
   528  into account that containers can be re-scheduled by the Swarm orchestrator and
   529  be deployed on a different node.
   530  
   531  #### Create a service that uses an anonymous volume
   532  
   533  The following command creates a service with three replicas with an anonymous
   534  volume on `/path/in/container`:
   535  
   536  ```bash
   537  $ docker service create \
   538    --name my-service \
   539    --replicas 3 \
   540    --mount type=volume,destination=/path/in/container \
   541    nginx:alpine
   542  ```
   543  
   544  In this example, no name (`source`) is specified for the volume, so a new volume
   545  is created for each task. This guarantees that each task gets its own volume,
   546  and volumes are not shared between tasks. Anonymous volumes are removed after
   547  the task using them is complete.
   548  
   549  #### Create a service that uses a bind-mounted host directory
   550  
   551  The following example bind-mounts a host directory at `/path/in/container` in
   552  the containers backing the service:
   553  
   554  ```bash
   555  $ docker service create \
   556    --name my-service \
   557    --mount type=bind,source=/path/on/host,destination=/path/in/container \
   558    nginx:alpine
   559  ```
   560  
   561  ### Set service mode (--mode)
   562  
   563  The service mode determines whether this is a _replicated_ service or a _global_
   564  service. A replicated service runs as many tasks as specified, while a global
   565  service runs on each active node in the swarm.
   566  
   567  The following command creates a global service:
   568  
   569  ```bash
   570  $ docker service create \
   571   --name redis_2 \
   572   --mode global \
   573   redis:3.0.6
   574  ```
   575  
   576  ### Specify service constraints (--constraint)
   577  
   578  You can limit the set of nodes where a task can be scheduled by defining
   579  constraint expressions. Multiple constraints find nodes that satisfy every
   580  expression (AND match). Constraints can match node or Docker Engine labels as
   581  follows:
   582  
   583  
   584  <table>
   585    <tr>
   586      <th>node attribute</th>
   587      <th>matches</th>
   588      <th>example</th>
   589    </tr>
   590    <tr>
   591      <td><tt>node.id</tt></td>
   592      <td>Node ID</td>
   593      <td><tt>node.id==2ivku8v2gvtg4</tt></td>
   594    </tr>
   595    <tr>
   596      <td><tt>node.hostname</tt></td>
   597      <td>Node hostname</td>
   598      <td><tt>node.hostname!=node-2</tt></td>
   599    </tr>
   600    <tr>
   601      <td><tt>node.role</tt></td>
   602      <td>Node role</td>
   603      <td><tt>node.role==manager</tt></td>
   604    </tr>
   605    <tr>
   606      <td><tt>node.labels</tt></td>
   607      <td>user defined node labels</td>
   608      <td><tt>node.labels.security==high</tt></td>
   609    </tr>
   610    <tr>
   611      <td><tt>engine.labels</tt></td>
   612      <td>Docker Engine's labels</td>
   613      <td><tt>engine.labels.operatingsystem==ubuntu 14.04</tt></td>
   614    </tr>
   615  </table>
   616  
   617  
   618  `engine.labels` apply to Docker Engine labels like operating system,
   619  drivers, etc. Swarm administrators add `node.labels` for operational purposes by
   620  using the [`docker node update`](node_update.md) command.
   621  
   622  For example, the following limits tasks for the redis service to nodes where the
   623  node type label equals queue:
   624  
   625  ```bash
   626  $ docker service create \
   627    --name redis_2 \
   628    --constraint 'node.labels.type == queue' \
   629    redis:3.0.6
   630  ```
   631  
   632  ### Specify service placement preferences (--placement-pref)
   633  
   634  You can set up the service to divide tasks evenly over different categories of
   635  nodes. One example of where this can be useful is to balance tasks over a set
   636  of datacenters or availability zones. The example below illustrates this:
   637  
   638  ```bash
   639  $ docker service create \
   640    --replicas 9 \
   641    --name redis_2 \
   642    --placement-pref 'spread=node.labels.datacenter' \
   643    redis:3.0.6
   644  ```
   645  
   646  This uses `--placement-pref` with a `spread` strategy (currently the only
   647  supported strategy) to spread tasks evenly over the values of the `datacenter`
   648  node label. In this example, we assume that every node has a `datacenter` node
   649  label attached to it. If there are three different values of this label among
   650  nodes in the swarm, one third of the tasks will be placed on the nodes
   651  associated with each value. This is true even if there are more nodes with one
   652  value than another. For example, consider the following set of nodes:
   653  
   654  - Three nodes with `node.labels.datacenter=east`
   655  - Two nodes with `node.labels.datacenter=south`
   656  - One node with `node.labels.datacenter=west`
   657  
   658  Since we are spreading over the values of the `datacenter` label and the
   659  service has 9 replicas, 3 replicas will end up in each datacenter. There are
   660  three nodes associated with the value `east`, so each one will get one of the
   661  three replicas reserved for this value. There are two nodes with the value
   662  `south`, and the three replicas for this value will be divided between them,
   663  with one receiving two replicas and another receiving just one. Finally, `west`
   664  has a single node that will get all three replicas reserved for `west`.
   665  
   666  If the nodes in one category (for example, those with
   667  `node.labels.datacenter=south`) can't handle their fair share of tasks due to
   668  constraints or resource limitations, the extra tasks will be assigned to other
   669  nodes instead, if possible.
   670  
   671  Both engine labels and node labels are supported by placement preferences. The
   672  example above uses a node label, because the label is referenced with
   673  `node.labels.datacenter`. To spread over the values of an engine label, use
   674  `--placement-pref spread=engine.labels.<labelname>`.
   675  
   676  It is possible to add multiple placement preferences to a service. This
   677  establishes a hierarchy of preferences, so that tasks are first divided over
   678  one category, and then further divided over additional categories. One example
   679  of where this may be useful is dividing tasks fairly between datacenters, and
   680  then splitting the tasks within each datacenter over a choice of racks. To add
   681  multiple placement preferences, specify the `--placement-pref` flag multiple
   682  times. The order is significant, and the placement preferences will be applied
   683  in the order given when making scheduling decisions.
   684  
   685  The following example sets up a service with multiple placement preferences.
   686  Tasks are spread first over the various datacenters, and then over racks
   687  (as indicated by the respective labels):
   688  
   689  ```bash
   690  $ docker service create \
   691    --replicas 9 \
   692    --name redis_2 \
   693    --placement-pref 'spread=node.labels.datacenter' \
   694    --placement-pref 'spread=node.labels.rack' \
   695    redis:3.0.6
   696  ```
   697  
   698  When updating a service with `docker service update`, `--placement-pref-add`
   699  appends a new placement preference after all existing placement preferences.
   700  `--placement-pref-rm` removes an existing placement preference that matches the
   701  argument.
   702  
   703  ### Attach a service to an existing network (--network)
   704  
   705  You can use overlay networks to connect one or more services within the swarm.
   706  
   707  First, create an overlay network on a manager node the docker network create
   708  command:
   709  
   710  ```bash
   711  $ docker network create --driver overlay my-network
   712  
   713  etjpu59cykrptrgw0z0hk5snf
   714  ```
   715  
   716  After you create an overlay network in swarm mode, all manager nodes have
   717  access to the network.
   718  
   719  When you create a service and pass the --network flag to attach the service to
   720  the overlay network:
   721  
   722  ```bash
   723  $ docker service create \
   724    --replicas 3 \
   725    --network my-network \
   726    --name my-web \
   727    nginx
   728  
   729  716thylsndqma81j6kkkb5aus
   730  ```
   731  
   732  The swarm extends my-network to each node running the service.
   733  
   734  Containers on the same network can access each other using
   735  [service discovery](https://docs.docker.com/engine/swarm/networking/#use-swarm-mode-service-discovery).
   736  
   737  ### Publish service ports externally to the swarm (-p, --publish)
   738  
   739  You can publish service ports to make them available externally to the swarm
   740  using the `--publish` flag. The `--publish` flag can take two different styles
   741  of arguments. The short version is positional, and allows you to specify the
   742  published port and target port separated by a colon.
   743  
   744  ```bash
   745  $ docker service create --name my_web --replicas 3 --publish 8080:80 nginx
   746  ```
   747  
   748  There is also a long format, which is easier to read and allows you to specify
   749  more options. The long format is preferred. You cannot specify the service's
   750  mode when using the short format. Here is an example of using the long format
   751  for the same service as above:
   752  
   753  ```bash
   754  $ docker service create --name my_web --replicas 3 --publish published=8080,target=80 nginx
   755  ```
   756  
   757  The options you can specify are:
   758  
   759  <table>
   760  <thead>
   761  <tr>
   762    <th>Option</th>
   763    <th>Short syntax</th>
   764    <th>Long syntax</th>
   765    <th>Description</th>
   766  </tr>
   767  <tr>
   768    <td>published and target port </td>
   769    <td><tt></tt></td>
   770    <td><tt></tt></td>
   771    <td></td>
   772  </tr>
   773  <tr>
   774    <td>protocol</td>
   775    <td><tt>--publish 8080:80</tt></td>
   776    <td><tt>--publish published=8080,target=80</tt></td>
   777    <td><p>
   778      The port to publish the service to on the routing mesh or directly on
   779      the node, and the target port on the container.
   780    </p></td>
   781  </tr>
   782  <tr>
   783    <td>mode</td>
   784    <td>Not possible to set using short syntax.</td>
   785    <td><tt>--publish published=8080,target=80,mode=host</tt></td>
   786    <td><p>
   787      The mode to use for binding the port, either `ingress` or `host`. Defaults
   788      to `ingress` to use the routing mesh.
   789    </p></td>
   790  </tr>
   791  <tr>
   792    <td>protocol</td>
   793    <td><tt>--publish 8080:80/tcp</tt></td>
   794    <td><tt>--publish published=8080,target=80,protocol=tcp</tt></td>
   795    <td><p>
   796      The protocol to use, either `tcp` or `udp`. Defaults to `tcp`. To bind a
   797      port for both protocols, specify the `-p` or `--publish` flag twice.
   798    </p></td>
   799  </tr>
   800  </table>
   801  
   802  When you publish a service port using `ingres` mode, the swarm routing mesh
   803  makes the service accessible at the published port on every node regardless if
   804  there is a task for the service running on the node. If you use `host` mode,
   805  the port is only bound on nodes where the service is running, and a given port
   806  on a node can only be bound once. You can only set the publication mode using
   807  the long syntax. For more information refer to
   808  [Use swarm mode routing mesh](https://docs.docker.com/engine/swarm/ingress/).
   809  
   810  ### Provide credential specs for managed service accounts (Windows only)
   811  
   812  This option is only used for services using Windows containers. The
   813  `--credential-spec` must be in the format `file://<filename>` or
   814  `registry://<value-name>`.
   815  
   816  When using the `file://<filename>` format, the referenced file must be
   817  present in the `CredentialSpecs` subdirectory in the docker data directory,
   818  which defaults to `C:\ProgramData\Docker\` on Windows. For example,
   819  specifying `file://spec.json` loads `C:\ProgramData\Docker\CredentialSpecs\spec.json`.
   820  
   821  When using the `registry://<value-name>` format, the credential spec is
   822  read from the Windows registry on the daemon's host. The specified
   823  registry value must be located in:
   824  
   825      HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\Containers\CredentialSpecs
   826  
   827  
   828  ### Create services using templates
   829  
   830  You can use templates for some flags of `service create`, using the syntax
   831  provided by the Go's [text/template](http://golang.org/pkg/text/template/) package.
   832  
   833  The supported flags are the following :
   834  
   835  - `--hostname`
   836  - `--mount`
   837  - `--env`
   838  
   839  Valid placeholders for the Go template are listed below:
   840  
   841  
   842  <table>
   843    <tr>
   844      <th>Placeholder</th>
   845      <th>Description</th>
   846    </tr>
   847    <tr>
   848      <td><tt>.Service.ID</tt></td>
   849      <td>Service ID</td>
   850    </tr>
   851    <tr>
   852      <td><tt>.Service.Name</tt></td>
   853      <td>Service name</td>
   854    </tr>
   855    <tr>
   856      <td><tt>.Service.Labels</tt></td>
   857      <td>Service labels</td>
   858    </tr>
   859    <tr>
   860      <td><tt>.Node.ID</tt></td>
   861      <td>Node ID</td>
   862    </tr>
   863    <tr>
   864      <td><tt>.Node.Hostname</tt></td>
   865      <td>Node Hostname</td>
   866    </tr>
   867    <tr>
   868      <td><tt>.Task.ID</tt></td>
   869      <td>Task ID</td>
   870    </tr>
   871    <tr>
   872      <td><tt>.Task.Name</tt></td>
   873      <td>Task name</td>
   874    </tr>
   875    <tr>
   876      <td><tt>.Task.Slot</tt></td>
   877      <td>Task slot</td>
   878    </tr>
   879  </table>
   880  
   881  
   882  #### Template example
   883  
   884  In this example, we are going to set the template of the created containers based on the
   885  service's name, the node's ID and hostname where it sits.
   886  
   887  ```bash
   888  $ docker service create --name hosttempl \
   889                          --hostname="{{.Node.Hostname}}-{{.Node.ID}}-{{.Service.Name}}"\
   890                           busybox top
   891  
   892  va8ew30grofhjoychbr6iot8c
   893  
   894  $ docker service ps va8ew30grofhjoychbr6iot8c
   895  
   896  ID            NAME         IMAGE                                                                                   NODE          DESIRED STATE  CURRENT STATE               ERROR  PORTS
   897  wo41w8hg8qan  hosttempl.1  busybox:latest@sha256:29f5d56d12684887bdfa50dcd29fc31eea4aaf4ad3bec43daf19026a7ce69912  2e7a8a9c4da2  Running        Running about a minute ago
   898  
   899  $ docker inspect --format="{{.Config.Hostname}}" 2e7a8a9c4da2-wo41w8hg8qanxwjwsg4kxpprj-hosttempl
   900  
   901  x3ti0erg11rjpg64m75kej2mz-hosttempl
   902  ```
   903  
   904  ### Specify isolation mode (Windows)
   905  
   906  By default, tasks scheduled on Windows nodes are run using the default isolation mode 
   907  configured for this particular node. To force a specific isolation mode, you can use 
   908  the `--isolation` flag: 
   909  
   910  ```bash
   911  $ docker service create --name myservice --isolation=process microsoft/nanoserver
   912  ```
   913  
   914  Supported isolation modes on Windows are:
   915  - `default`: use default settings specified on the node running the task
   916  - `process`: use process isolation (Windows server only)
   917  - `hyperv`: use Hyper-V isolation
   918  
   919  ### Create services requesting Generic Resources
   920  
   921  You can narrow the kind of nodes your task can land on through the using the
   922  `--generic-resource` flag (if the nodes advertise these resources):
   923  
   924  ```bash
   925  $ docker service create --name cuda \
   926                          --generic-resource "NVIDIA-GPU=2" \
   927                          --generic-resource "SSD=1" \
   928                          nvidia/cuda
   929  ```
   930  
   931  ## Related commands
   932  
   933  * [service inspect](service_inspect.md)
   934  * [service logs](service_logs.md)
   935  * [service ls](service_ls.md)
   936  * [service ps](service_ps.md)
   937  * [service rm](service_rm.md)
   938  * [service rollback](service_rollback.md)
   939  * [service scale](service_scale.md)
   940  * [service update](service_update.md)
   941  
   942  <style>table tr > td:first-child { white-space: nowrap;}</style>