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