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