github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/task.mdx (about)

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