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