github.com/sld880311/docker@v0.0.0-20200524143708-d5593973a475/docs/reference/commandline/service_create.md (about)

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