github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/task.mdx (about)

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