github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/website/source/docs/job-specification/task.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "task Stanza - Job Specification"
     4  sidebar_current: "docs-job-specification-task"
     5  description: |-
     6    The "task" stanza creates an individual unit of work, such as a Docker
     7    container, web application, or batch processing.
     8  ---
     9  
    10  # `task` Stanza
    11  
    12  <table class="table table-bordered table-striped">
    13    <tr>
    14      <th width="120">Placement</th>
    15      <td>
    16        <code>job -> group -> **task**</code>
    17      </td>
    18    </tr>
    19  </table>
    20  The `task` stanza creates an individual unit of work, such as a Docker
    21  container, web application, or batch processing.
    22  
    23  ```hcl
    24  job "docs" {
    25    group "example" {
    26      task "server" {
    27        # ...
    28      }
    29    }
    30  }
    31  ```
    32  
    33  ## `task` Parameters
    34  
    35  - `artifact` <code>([Artifact][]: nil)</code> - Defines an artifact to download
    36    before running the task. This may be specified multiple times to download
    37    multiple artifacts.
    38  
    39  - `config` `(map<string|string>: nil)` - Specifies the driver configuration,
    40    which is passed directly to the driver to start the task. The details of
    41    configurations are specific to each driver, so please see specific driver
    42    documentation for more information.
    43  
    44  - `constraint` <code>([Constraint][]: nil)</code> - Specifies user-defined
    45    constraints on the task. This can be provided multiple times to define
    46    additional constraints.
    47  
    48  - `affinity` <code>([Affinity][]: nil)</code> - This can be provided
    49    multiple times to define preferred placement criteria.
    50  
    51  - `dispatch_payload` <code>([DispatchPayload][]: nil)</code> - Configures the
    52    task to have access to dispatch payloads.
    53  
    54  - `driver` - Specifies the task driver that should be used to run the
    55    task. See the [driver documentation](/docs/drivers/index.html) for what
    56    is available. Examples include `docker`, `qemu`, `java` and `exec`.
    57  
    58  - `env` <code>([Env][]: nil)</code> - Specifies environment variables that will
    59    be passed to the running process.
    60  
    61  - `kill_timeout` `(string: "5s")` - Specifies the duration to wait for an
    62    application to gracefully quit before force-killing. Nomad sends an `SIGINT`.
    63    If the task does not exit before the configured timeout, `SIGKILL` is sent to
    64    the task. Note that the value set here is capped at the value set for
    65    [`max_kill_timeout`][max_kill] on the agent running the task, which has a
    66    default value of 30 seconds.
    67  
    68  - `kill_signal` `(string)` - Specifies a configurable kill signal for a task,
    69    where the default is SIGINT. Note that this is only supported for drivers
    70    sending signals (currently `docker`, `exec`, `raw_exec`, and `java` drivers).
    71  
    72  - `leader` `(bool: false)` - Specifies whether the task is the leader task of
    73    the task group. If set to true, when the leader task completes, all other
    74    tasks within the task group will be gracefully shutdown.
    75  
    76  - `logs` <code>([Logs][]: nil)</code> - Specifies logging configuration for the
    77    `stdout` and `stderr` of the task.
    78  
    79  - `meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that annotates
    80    with user-defined metadata.
    81  
    82  - `resources` <code>([Resources][]: <required>)</code> - Specifies the minimum
    83    resource requirements such as RAM, CPU and network.
    84  
    85  - `service` <code>([Service][]: nil)</code> - Specifies integrations with
    86    [Consul][] for service discovery. Nomad automatically registers when a task
    87    is started and de-registers it when the task dies.
    88  
    89  - `shutdown_delay` `(string: "0s")` - Specifies the duration to wait when
    90    killing a task between removing it from Consul and sending it a shutdown
    91    signal. Ideally services would fail healthchecks once they receive a shutdown
    92    signal. Alternatively `shutdown_delay` may be set to give in flight requests
    93    time to complete before shutting down.
    94  
    95  - `user` `(string: <varies>)` - Specifies the user that will run the task.
    96    Defaults to `nobody` for the [`exec`][exec] and [`java`][java] drivers.
    97    [Docker][] and [rkt][] images specify their own default users.  This can only
    98    be set on Linux platforms, and clients can restrict
    99    [which drivers][user_drivers] are allowed to run tasks as
   100    [certain users][user_blacklist].
   101  
   102  - `template` <code>([Template][]: nil)</code> - Specifies the set of templates
   103    to render for the task. Templates can be used to inject both static and
   104    dynamic configuration with data populated from environment variables, Consul
   105    and Vault.
   106  
   107  - `vault` <code>([Vault][]: nil)</code> - Specifies the set of Vault policies
   108    required by the task. This overrides any `vault` block set at the `group` or
   109    `job` level.
   110  
   111  ## `task` Examples
   112  
   113  The following examples only show the `task` stanzas. Remember that the
   114  `task` stanza is only valid in the placements listed above.
   115  
   116  ### Docker Container
   117  
   118  This example defines a task that starts a Docker container as a service. Docker
   119  is just one of many drivers supported by Nomad. Read more about drivers in the
   120  [Nomad drivers documentation](/docs/drivers/index.html).
   121  
   122  ```hcl
   123  task "server" {
   124    driver = "docker"
   125    config {
   126      image = "hashicorp/http-echo"
   127      args  = ["-text", "hello world"]
   128    }
   129  
   130    resources {
   131      network {
   132        mbits = 250
   133      }
   134    }
   135  }
   136  ```
   137  
   138  ### Metadata and Environment Variables
   139  
   140  This example uses custom metadata and environment variables to pass information
   141  to the task.
   142  
   143  ```hcl
   144  task "server" {
   145    driver = "exec"
   146    config {
   147      command = "/bin/env"
   148    }
   149  
   150    meta {
   151      my-key = "my-value"
   152    }
   153  
   154    env {
   155      MY_KEY = "${meta.my-key}"
   156    }
   157  
   158    resources {
   159      cpu = 20
   160    }
   161  }
   162  ```
   163  
   164  ### Service Discovery
   165  
   166  This example creates a service in Consul. To read more about service discovery
   167  in Nomad, please see the [Nomad service discovery documentation][service_discovery].
   168  
   169  ```hcl
   170  task "server" {
   171    driver = "docker"
   172    config {
   173      image = "hashicorp/http-echo"
   174      args  = ["-text", "hello world"]
   175    }
   176  
   177    service {
   178      tags = ["default"]
   179  
   180      check {
   181        type     = "tcp"
   182        interval = "10s"
   183        timeout  = "2s"
   184      }
   185    }
   186  
   187    resources {
   188      cpu = 20
   189    }
   190  }
   191  ```
   192  
   193  [artifact]: /docs/job-specification/artifact.html "Nomad artifact Job Specification"
   194  [consul]: https://www.consul.io/ "Consul by HashiCorp"
   195  [constraint]: /docs/job-specification/constraint.html "Nomad constraint Job Specification"
   196  [affinity]: /docs/job-specification/affinity.html "Nomad affinity Job Specification"
   197  [dispatchpayload]: /docs/job-specification/dispatch_payload.html "Nomad dispatch_payload Job Specification"
   198  [env]: /docs/job-specification/env.html "Nomad env Job Specification"
   199  [meta]: /docs/job-specification/meta.html "Nomad meta Job Specification"
   200  [resources]: /docs/job-specification/resources.html "Nomad resources Job Specification"
   201  [logs]: /docs/job-specification/logs.html "Nomad logs Job Specification"
   202  [service]: /docs/job-specification/service.html "Nomad service Job Specification"
   203  [vault]: /docs/job-specification/vault.html "Nomad vault Job Specification"
   204  [exec]: /docs/drivers/exec.html "Nomad exec Driver"
   205  [java]: /docs/drivers/java.html "Nomad Java Driver"
   206  [Docker]: /docs/drivers/docker.html "Nomad Docker Driver"
   207  [rkt]: /docs/drivers/rkt.html "Nomad rkt Driver"
   208  [service_discovery]: /guides/integrations/consul-integration/index.html#service-discovery/index.html "Nomad Service Discovery"
   209  [template]: /docs/job-specification/template.html "Nomad template Job Specification"
   210  [user_drivers]: /docs/configuration/client.html#_quot_user_checked_drivers_quot_
   211  [user_blacklist]: /docs/configuration/client.html#_quot_user_blacklist_quot_
   212  [max_kill]: /docs/configuration/client.html#max_kill_timeout