github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/website/source/docs/drivers/docker.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Drivers: Docker" 4 sidebar_current: "docs-drivers-docker" 5 description: |- 6 The Docker task driver is used to run Docker based tasks. 7 --- 8 9 # Docker Driver 10 11 Name: `docker` 12 13 The `docker` driver provides a first-class Docker workflow on Nomad. The Docker 14 driver handles downloading containers, mapping ports, and starting, watching, 15 and cleaning up after containers. 16 17 ## Task Configuration 18 19 ```hcl 20 task "webservice" { 21 driver = "docker" 22 23 config { 24 image = "redis:3.2" 25 labels { 26 group = "webservice-cache" 27 } 28 } 29 } 30 ``` 31 32 The `docker` driver supports the following configuration in the job spec. Only 33 `image` is required. 34 35 * `image` - The Docker image to run. The image may include a tag or custom URL 36 and should include `https://` if required. By default it will be fetched from 37 Docker Hub. If the tag is omitted or equal to `latest` the driver will always 38 try to pull the image. If the image to be pulled exists in a registry that 39 requires authentication credentials must be provided to Nomad. Please see the 40 [Authentication section](#authentication). 41 42 ```hcl 43 config { 44 image = "https://hub.docker.internal/redis:3.2" 45 } 46 ``` 47 48 * `args` - (Optional) A list of arguments to the optional `command`. If no 49 `command` is specified, the arguments are passed directly to the container. 50 References to environment variables or any [interpretable Nomad 51 variables](/docs/runtime/interpolation.html) will be interpreted before 52 launching the task. For example: 53 54 ```hcl 55 config { 56 args = [ 57 "-bind", "${NOMAD_PORT_http}", 58 "${nomad.datacenter}", 59 "${MY_ENV}", 60 "${meta.foo}", 61 ] 62 } 63 ``` 64 65 * `auth` - (Optional) Provide authentication for a private registry (see below). 66 67 * `auth_soft_fail` `(bool: false)` - Don't fail the task on an auth failure. 68 Attempt to continue without auth. 69 70 * `command` - (Optional) The command to run when starting the container. 71 72 ```hcl 73 config { 74 command = "my-command" 75 } 76 ``` 77 78 * `dns_search_domains` - (Optional) A list of DNS search domains for the container 79 to use. 80 81 * `dns_servers` - (Optional) A list of DNS servers for the container to use 82 (e.g. ["8.8.8.8", "8.8.4.4"]). Requires Docker v1.10 or greater. 83 84 * `extra_hosts` - (Optional) A list of hosts, given as host:IP, to be added to 85 `/etc/hosts`. 86 87 * `force_pull` - (Optional) `true` or `false` (default). Always pull latest image 88 instead of using existing local image. Should be set to `true` if repository tags 89 are mutable. 90 91 * `hostname` - (Optional) The hostname to assign to the container. When 92 launching more than one of a task (using `count`) with this option set, every 93 container the task starts will have the same hostname. 94 95 * `interactive` - (Optional) `true` or `false` (default). Keep STDIN open on 96 the container. 97 98 * `ipc_mode` - (Optional) The IPC mode to be used for the container. The default 99 is `none` for a private IPC namespace. Other values are `host` for sharing 100 the host IPC namespace or the name or id of an existing container. Note that 101 it is not possible to refer to Docker containers started by Nomad since their 102 names are not known in advance. Note that setting this option also requires the 103 Nomad agent to be configured to allow privileged containers. 104 105 * `ipv4_address` - (Optional) The IPv4 address to be used for the container when 106 using user defined networks. Requires Docker 1.13 or greater. 107 108 * `ipv6_address` - (Optional) The IPv6 address to be used for the container when 109 using user defined networks. Requires Docker 1.13 or greater. 110 111 * `labels` - (Optional) A key-value map of labels to set to the containers on 112 start. 113 114 ```hcl 115 config { 116 labels { 117 foo = "bar" 118 zip = "zap" 119 } 120 } 121 ``` 122 123 * `load` - (Optional) A list of paths to image archive files. If 124 this key is not specified, Nomad assumes the `image` is hosted on a repository 125 and attempts to pull the image. The `artifact` blocks can be specified to 126 download each of the archive files. The equivalent of `docker load -i path` 127 would be run on each of the archive files. 128 129 ```hcl 130 artifact { 131 source = "http://path.to/redis.tar" 132 } 133 config { 134 load = ["redis.tar"] 135 image = "redis" 136 } 137 ``` 138 139 * `logging` - (Optional) A key-value map of Docker logging options. The default 140 value is `syslog`. 141 142 ```hcl 143 config { 144 logging { 145 type = "fluentd" 146 config { 147 fluentd-address = "localhost:24224" 148 tag = "your_tag" 149 } 150 } 151 } 152 ``` 153 154 * `mac_address` - (Optional) The MAC address for the container to use (e.g. 155 "02:68:b3:29:da:98"). 156 157 * `network_aliases` - (Optional) A list of network-scoped aliases, provide a way for a 158 container to be discovered by an alternate name by any other container within 159 the scope of a particular network. Network-scoped alias is supported only for 160 containers in user defined networks 161 162 ```hcl 163 config { 164 network_mode = "user-network" 165 network_aliases = [ 166 "${NOMAD_TASK_NAME}", 167 "${NOMAD_TASK_NAME}-${NOMAD_ALLOC_INDEX}" 168 ] 169 } 170 ``` 171 172 * `network_mode` - (Optional) The network mode to be used for the container. In 173 order to support userspace networking plugins in Docker 1.9 this accepts any 174 value. The default is `bridge` for all operating systems but Windows, which 175 defaults to `nat`. Other networking modes may not work without additional 176 configuration on the host (which is outside the scope of Nomad). Valid values 177 pre-docker 1.9 are `default`, `bridge`, `host`, `none`, or `container:name`. 178 179 * `pid_mode` - (Optional) `host` or not set (default). Set to `host` to share 180 the PID namespace with the host. Note that this also requires the Nomad agent 181 to be configured to allow privileged containers. 182 See below for more details. 183 184 * `port_map` - (Optional) A key-value map of port labels (see below). 185 186 * `privileged` - (Optional) `true` or `false` (default). Privileged mode gives 187 the container access to devices on the host. Note that this also requires the 188 nomad agent and docker daemon to be configured to allow privileged 189 containers. 190 191 * `security_opt` - (Optional) A list of string flags to pass directly to 192 [`--security-opt`](https://docs.docker.com/engine/reference/run/#security-configuration). 193 For example: 194 195 196 ```hcl 197 config { 198 security_opt = [ 199 "credentialspec=file://gmsaUser.json", 200 ] 201 } 202 ``` 203 204 * `shm_size` - (Optional) The size (bytes) of /dev/shm for the container. 205 206 * `SSL` - (Optional) If this is set to true, Nomad uses SSL to talk to the 207 repository. The default value is `true`. **Deprecated as of 0.5.3** 208 209 * `tty` - (Optional) `true` or `false` (default). Allocate a pseudo-TTY for the 210 container. 211 212 * `uts_mode` - (Optional) `host` or not set (default). Set to `host` to share 213 the UTS namespace with the host. Note that this also requires the Nomad agent 214 to be configured to allow privileged containers. 215 216 * `userns_mode` - (Optional) `host` or not set (default). Set to `host` to use 217 the host's user namespace when user namespace remapping is enabled on the 218 docker daemon. 219 220 * `volumes` - (Optional) A list of `host_path:container_path` strings to bind 221 host paths to container paths. Mounting host paths outside of the allocation 222 directory can be disabled on clients by setting the `docker.volumes.enabled` 223 option set to false. This will limit volumes to directories that exist inside 224 the allocation directory. 225 226 ```hcl 227 config { 228 volumes = [ 229 # Use absolute paths to mount arbitrary paths on the host 230 "/path/on/host:/path/in/container", 231 232 # Use relative paths to rebind paths already in the allocation dir 233 "relative/to/task:/also/in/container" 234 ] 235 } 236 ``` 237 238 * `volume_driver` - (Optional) The name of the volume driver used to mount 239 volumes. Must be used along with `volumes`. 240 Using a `volume_driver` also allows to use `volumes` with a named volume as 241 well as absolute paths. If `docker.volumes.enabled` is false then volume 242 drivers are disallowed. 243 244 ```hcl 245 config { 246 volumes = [ 247 # Use named volume created outside nomad. 248 "name-of-the-volume:/path/in/container" 249 ] 250 # Name of the Docker Volume Driver used by the container 251 volume_driver = "flocker" 252 } 253 ``` 254 255 * `work_dir` - (Optional) The working directory inside the container. 256 257 ### Container Name 258 259 Nomad creates a container after pulling an image. Containers are named 260 `{taskName}-{allocId}`. This is necessary in order to place more than one 261 container from the same task on a host (e.g. with count > 1). This also means 262 that each container's name is unique across the cluster. 263 264 This is not configurable. 265 266 ### Authentication 267 268 If you want to pull from a private repo (for example on dockerhub or quay.io), 269 you will need to specify credentials in your job via: 270 271 * the `auth` option in the task config. 272 273 * by storing explicit repository credentials or by specifying Docker 274 `credHelpers` in a file and setting the [docker.auth.config](#auth_file) 275 value on the client. 276 277 * by specifying a [docker.auth.helper](#auth_helper) on the client 278 279 The `auth` object supports the following keys: 280 281 * `username` - (Optional) The account username. 282 283 * `password` - (Optional) The account password. 284 285 * `email` - (Optional) The account email. 286 287 * `server_address` - (Optional) The server domain/IP without the protocol. 288 Docker Hub is used by default. 289 290 Example task-config: 291 292 ```hcl 293 task "example" { 294 driver = "docker" 295 296 config { 297 image = "secret/service" 298 299 auth { 300 username = "dockerhub_user" 301 password = "dockerhub_password" 302 } 303 } 304 } 305 ``` 306 307 Example docker-config, using two helper scripts in $PATH, 308 "docker-credential-ecr" and "docker-credential-vault": 309 310 ```json 311 { 312 "auths": { 313 "internal.repo": { "auth": "`echo -n '<username>:<password>' | base64 -w0`" } 314 }, 315 "credHelpers": { 316 "<XYZ>.dkr.ecr.<region>.amazonaws.com": "ecr-login" 317 }, 318 "credsStore": "secretservice" 319 } 320 ``` 321 322 323 !> **Be Careful!** At this time these credentials are stored in Nomad in plain 324 text. Secrets management will be added in a later release. 325 326 ## Networking 327 328 Docker supports a variety of networking configurations, including using host 329 interfaces, SDNs, etc. Nomad uses `bridged` networking by default, like Docker. 330 331 You can specify other networking options, including custom networking plugins 332 in Docker 1.9. **You may need to perform additional configuration on the host 333 in order to make these work.** This additional configuration is outside the 334 scope of Nomad. 335 336 ### Allocating Ports 337 338 You can allocate ports to your task using the port syntax described on the 339 [networking page](/docs/job-specification/network.html). Here is a recap: 340 341 ```hcl 342 task "example" { 343 driver = "docker" 344 345 resources { 346 network { 347 port "http" {} 348 port "https" {} 349 } 350 } 351 } 352 ``` 353 354 ### Forwarding and Exposing Ports 355 356 A Docker container typically specifies which port a service will listen on by 357 specifying the `EXPOSE` directive in the `Dockerfile`. 358 359 Because dynamic ports will not match the ports exposed in your Dockerfile, 360 Nomad will automatically expose all of the ports it allocates to your 361 container. 362 363 These ports will be identified via environment variables. For example: 364 365 ```hcl 366 port "http" {} 367 ``` 368 369 If Nomad allocates port `23332` to your task for `http`, `23332` will be 370 automatically exposed and forwarded to your container, and the driver will set 371 an environment variable `NOMAD_PORT_http` with the value `23332` that you can 372 read inside your container. 373 374 This provides an easy way to use the `host` networking option for better 375 performance. 376 377 ### Using the Port Map 378 379 If you prefer to use the traditional port-mapping method, you can specify the 380 `port_map` option in your job specification. It looks like this: 381 382 ```hcl 383 task "example" { 384 driver = "docker" 385 386 config { 387 image = "redis" 388 389 port_map { 390 redis = 6379 391 } 392 } 393 394 resources { 395 network { 396 mbits = 20 397 port "redis" {} 398 } 399 } 400 } 401 ``` 402 403 If Nomad allocates port `23332` to your task, the Docker driver will 404 automatically setup the port mapping from `23332` on the host to `6379` in your 405 container, so it will just work! 406 407 Note that by default this only works with `bridged` networking mode. It may 408 also work with custom networking plugins which implement the same API for 409 expose and port forwarding. 410 411 ### Advertising Container IPs 412 413 *New in Nomad 0.6.* 414 415 When using network plugins like `weave` that assign containers a routable IP 416 address, that address will automatically be used in any `service` 417 advertisements for the task. You may override what address is advertised by 418 using the `address_mode` parameter on a `service`. See 419 [service](/docs/job-specification/service.html) for details. 420 421 ### Networking Protocols 422 423 The Docker driver configures ports on both the `tcp` and `udp` protocols. 424 425 This is not configurable. 426 427 ### Other Networking Modes 428 429 Some networking modes like `container` or `none` will require coordination 430 outside of Nomad. First-class support for these options may be improved later 431 through Nomad plugins or dynamic job configuration. 432 433 ## Client Requirements 434 435 Nomad requires Docker to be installed and running on the host alongside the 436 Nomad agent. Nomad was developed against Docker `1.8.2` and `1.9`. 437 438 By default Nomad communicates with the Docker daemon using the daemon's Unix 439 socket. Nomad will need to be able to read/write to this socket. If you do not 440 run Nomad as root, make sure you add the Nomad user to the Docker group so 441 Nomad can communicate with the Docker daemon. 442 443 For example, on Ubuntu you can use the `usermod` command to add the `vagrant` 444 user to the `docker` group so you can run Nomad without root: 445 446 sudo usermod -G docker -a vagrant 447 448 For the best performance and security features you should use recent versions 449 of the Linux Kernel and Docker daemon. 450 451 ## Client Configuration 452 453 The `docker` driver has the following [client configuration 454 options](/docs/agent/configuration/client.html#options): 455 456 * `docker.endpoint` - Defaults to `unix:///var/run/docker.sock`. You will need 457 to customize this if you use a non-standard socket (HTTP or another 458 location). 459 460 * `docker.auth.config` <a id="auth_file"></a>- Allows an operator to specify a 461 JSON file which is in the dockercfg format containing authentication 462 information for a private registry, from either (in order) `auths`, 463 `credHelpers` or `credsStore`. 464 465 * `docker.auth.helper` <a id="auth_helper"></a>- Allows an operator to specify 466 a [credsStore](https://docs.docker.com/engine/reference/commandline/login/#credential-helper-protocol) 467 -like script on $PATH to lookup authentication information from external 468 sources. 469 470 * `docker.tls.cert` - Path to the server's certificate file (`.pem`). Specify 471 this along with `docker.tls.key` and `docker.tls.ca` to use a TLS client to 472 connect to the docker daemon. `docker.endpoint` must also be specified or 473 this setting will be ignored. 474 475 * `docker.tls.key` - Path to the client's private key (`.pem`). Specify this 476 along with `docker.tls.cert` and `docker.tls.ca` to use a TLS client to 477 connect to the docker daemon. `docker.endpoint` must also be specified or 478 this setting will be ignored. 479 480 * `docker.tls.ca` - Path to the server's CA file (`.pem`). Specify this along 481 with `docker.tls.cert` and `docker.tls.key` to use a TLS client to connect to 482 the docker daemon. `docker.endpoint` must also be specified or this setting 483 will be ignored. 484 485 * `docker.cleanup.image` Defaults to `true`. Changing this to `false` will 486 prevent Nomad from removing images from stopped tasks. 487 488 * `docker.cleanup.image.delay` A time duration that defaults to `3m`. The delay 489 controls how long Nomad will wait between an image being unused and deleting 490 it. If a tasks is received that uses the same image within the delay, the 491 image will be reused. 492 493 * `docker.volumes.enabled`: Defaults to `true`. Allows tasks to bind host paths 494 (`volumes`) inside their container and use volume drivers (`volume_driver`). 495 Binding relative paths is always allowed and will be resolved relative to the 496 allocation's directory. 497 498 * `docker.volumes.selinuxlabel`: Allows the operator to set a SELinux 499 label to the allocation and task local bind-mounts to containers. If used 500 with `docker.volumes.enabled` set to false, the labels will still be applied 501 to the standard binds in the container. 502 503 * `docker.privileged.enabled` Defaults to `false`. Changing this to `true` will 504 allow containers to use `privileged` mode, which gives the containers full 505 access to the host's devices. Note that you must set a similar setting on the 506 Docker daemon for this to work. 507 508 Note: When testing or using the `-dev` flag you can use `DOCKER_HOST`, 509 `DOCKER_TLS_VERIFY`, and `DOCKER_CERT_PATH` to customize Nomad's behavior. If 510 `docker.endpoint` is set Nomad will **only** read client configuration from the 511 config file. 512 513 An example is given below: 514 515 ```hcl 516 client { 517 options { 518 "docker.cleanup.image" = "false" 519 } 520 } 521 ``` 522 523 ## Client Attributes 524 525 The `docker` driver will set the following client attributes: 526 527 * `driver.docker` - This will be set to "1", indicating the driver is 528 available. 529 * `driver.docker.bridge_ip` - The IP of the Docker bridge network if one 530 exists. 531 * `driver.docker.version` - This will be set to version of the docker server. 532 533 Here is an example of using these properties in a job file: 534 535 ```hcl 536 job "docs" { 537 # Require docker version higher than 1.2. 538 constraint { 539 attribute = "${driver.docker.version}" 540 operator = ">" 541 version = "1.2" 542 } 543 } 544 ``` 545 546 ## Resource Isolation 547 548 ### CPU 549 550 Nomad limits containers' CPU based on CPU shares. CPU shares allow containers 551 to burst past their CPU limits. CPU limits will only be imposed when there is 552 contention for resources. When the host is under load your process may be 553 throttled to stabilize QoS depending on how many shares it has. You can see how 554 many CPU shares are available to your process by reading `NOMAD_CPU_LIMIT`. 555 1000 shares are approximately equal to 1 GHz. 556 557 Please keep the implications of CPU shares in mind when you load test workloads 558 on Nomad. 559 560 ### Memory 561 562 Nomad limits containers' memory usage based on total virtual memory. This means 563 that containers scheduled by Nomad cannot use swap. This is to ensure that a 564 swappy process does not degrade performance for other workloads on the same 565 host. 566 567 Since memory is not an elastic resource, you will need to make sure your 568 container does not exceed the amount of memory allocated to it, or it will be 569 terminated or crash when it tries to malloc. A process can inspect its memory 570 limit by reading `NOMAD_MEMORY_LIMIT`, but will need to track its own memory 571 usage. Memory limit is expressed in megabytes so 1024 = 1 GB. 572 573 ### IO 574 575 Nomad's Docker integration does not currently provide QoS around network or 576 filesystem IO. These will be added in a later release. 577 578 ### Security 579 580 Docker provides resource isolation by way of 581 [cgroups and namespaces](https://docs.docker.com/introduction/understanding-docker/#the-underlying-technology). 582 Containers essentially have a virtual file system all to themselves. If you 583 need a higher degree of isolation between processes for security or other 584 reasons, it is recommended to use full virtualization like 585 [QEMU](/docs/drivers/qemu.html). 586 587 ## Docker For Mac Caveats 588 589 Docker For Mac runs docker inside a small VM and then allows access to parts of 590 the host filesystem into that VM. At present, nomad uses a syslog server bound to 591 a Unix socket within a path that both the host and the VM can access to forward 592 log messages back to nomad. But at present, Docker For Mac does not work for 593 Unix domain sockets (https://github.com/docker/for-mac/issues/483) in one of 594 these shared paths. 595 596 As a result, using nomad with the docker driver on OS X/macOS will work, but no 597 logs will be available to nomad. Users must use the native docker facilities to 598 examine the logs of any jobs running under docker. 599 600 In the future, we will resolve this issue, one way or another.