github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/drivers/docker.mdx (about) 1 --- 2 layout: docs 3 page_title: 'Drivers: Docker' 4 description: The Docker task driver is used to run Docker based tasks. 5 --- 6 7 # Docker Driver 8 9 Name: `docker` 10 11 The `docker` driver provides a first-class Docker workflow on Nomad. The Docker 12 driver handles downloading containers, mapping ports, and starting, watching, 13 and cleaning up after containers. 14 15 -> **Note:** If you are using Docker Desktop for Windows or MacOS, please check 16 [our FAQ][faq-win-mac]. 17 18 ## Task Configuration 19 20 ```hcl 21 task "webservice" { 22 driver = "docker" 23 24 config { 25 image = "redis:7" 26 labels { 27 group = "webservice-cache" 28 } 29 } 30 } 31 ``` 32 33 The `docker` driver supports the following configuration in the job spec. Only 34 `image` is required. 35 36 - `image` - The Docker image to run. The image may include a tag or custom URL 37 and should include `https://` if required. By default it will be fetched from 38 Docker Hub. If the tag is omitted or equal to `latest` the driver will always 39 try to pull the image. If the image to be pulled exists in a registry that 40 requires authentication credentials must be provided to Nomad. Please see the 41 [Authentication section](#authentication). 42 43 ```hcl 44 config { 45 image = "https://hub.docker.internal/redis:7" 46 } 47 ``` 48 49 - `image_pull_timeout` - (Optional) A time duration that controls how long Nomad 50 will wait before cancelling an in-progress pull of the Docker image as specified 51 in `image`. Defaults to `"5m"`. 52 53 - `args` - (Optional) A list of arguments to the optional `command`. If no 54 `command` is specified, the arguments are passed directly to the container. 55 References to environment variables or any [interpretable Nomad 56 variables](/docs/runtime/interpolation) will be interpreted before 57 launching the task. For example: 58 59 ```hcl 60 config { 61 args = [ 62 "-bind", "${NOMAD_PORT_http}", 63 "${nomad.datacenter}", 64 "${MY_ENV}", 65 "${meta.foo}", 66 ] 67 } 68 ``` 69 70 - `auth` - (Optional) Provide authentication for a private registry (see below). 71 72 - `auth_soft_fail` `(bool: false)` - Don't fail the task on an auth failure. 73 Attempt to continue without auth. If the Nomad client configuration has an 74 [`auth.helper`](#plugin_auth_helper) stanza, the helper will be tried for 75 all images, including public images. If you mix private and public images, 76 you will need to include `auth_soft_fail=true` in every job using a public 77 image. 78 79 - `command` - (Optional) The command to run when starting the container. 80 81 ```hcl 82 config { 83 command = "my-command" 84 } 85 ``` 86 87 - `cpuset_cpus` <sup>Beta</sup> - (Optional) CPUs in which to allow execution 88 (0-3, 0,1). Limit the specific CPUs or cores a container can use. A 89 comma-separated list or hyphen-separated range of CPUs a container can use, if 90 you have more than one CPU. The first CPU is numbered 0. A valid value might 91 be 0-3 (to use the first, second, third, and fourth CPU) or 1,3 (to use the 92 second and fourth CPU). 93 94 Note: `cpuset_cpus` pins the workload to the CPUs but doesn't give the workload 95 exclusive access to those CPUs. 96 97 ```hcl 98 config { 99 cpuset_cpus = "0-3" 100 } 101 ``` 102 103 - `dns_search_domains` - (Optional) A list of DNS search domains for 104 the container to use. If you are using bridge networking mode with a 105 `network` block in the task group, you must set all DNS options in 106 the `network.dns` block instead. 107 108 - `dns_options` - (Optional) A list of DNS options for the container 109 to use. If you are using bridge networking mode with a `network` 110 block in the task group, you must set all DNS options in the 111 `network.dns` block instead. 112 113 - `dns_servers` - (Optional) A list of DNS servers for the container 114 to use (e.g. ["8.8.8.8", "8.8.4.4"]). Requires Docker v1.10 or 115 greater. If you are using bridge networking mode with a `network` 116 block in the task group, you must set all DNS options in the 117 `network.dns` block instead. 118 119 - `entrypoint` - (Optional) A string list overriding the image's entrypoint. 120 121 - `extra_hosts` - (Optional) A list of hosts, given as host:IP, to be added to 122 `/etc/hosts`. This option may not work as expected in `bridge` network mode 123 when there is more than one task within the same group. Refer to the 124 [upgrade guide][upgrade_guide_extra_hosts] for more information. 125 126 - `force_pull` - (Optional) `true` or `false` (default). Always pull most recent image 127 instead of using existing local image. Should be set to `true` if repository tags 128 are mutable. If image's tag is `latest` or omitted, the image will always be pulled 129 regardless of this setting. 130 131 - `healthchecks` - (Optional) A configuration block for controlling how the 132 docker driver manages HEALTHCHECK directives built into the container. Set 133 `healthchecks.disable` to disable any built-in healthcheck. 134 135 ```hcl 136 config { 137 healthchecks { 138 disable = true 139 } 140 } 141 ``` 142 143 - `hostname` - (Optional) The hostname to assign to the container. When 144 launching more than one of a task (using `count`) with this option set, every 145 container the task starts will have the same hostname. 146 147 - `init` - (Optional) `true` or `false` (default). Enable init (tini) system when 148 launching your container. When enabled, an init process will be used as the PID1 149 in the container. Specifying an init process ensures the usual responsibilities 150 of an init system, such as reaping zombie processes, are performed inside the 151 created container. 152 153 The default init process used is the first `docker-init` executable found in the 154 system path of the Docker daemon process. This `docker-init` binary, included in 155 the default installation, is backed by [tini][tini]. 156 157 - `interactive` - (Optional) `true` or `false` (default). Keep STDIN open on 158 the container. 159 160 - `sysctl` - (Optional) A key-value map of sysctl configurations to set to the 161 containers on start. 162 163 ```hcl 164 config { 165 sysctl = { 166 "net.core.somaxconn" = "16384" 167 } 168 } 169 ``` 170 171 - `ulimit` - (Optional) A key-value map of ulimit configurations to set to the 172 containers on start. 173 174 ```hcl 175 config { 176 ulimit { 177 nproc = "4242" 178 nofile = "2048:4096" 179 } 180 } 181 ``` 182 183 - `privileged` - (Optional) `true` or `false` (default). Privileged mode gives 184 the container access to devices on the host. Note that this also requires the 185 nomad agent and docker daemon to be configured to allow privileged 186 containers. 187 188 - `ipc_mode` - (Optional) The IPC mode to be used for the container. The default 189 is `none` for a private IPC namespace. Other values are `host` for sharing 190 the host IPC namespace or the name or id of an existing container. Note that 191 it is not possible to refer to Docker containers started by Nomad since their 192 names are not known in advance. Note that setting this option also requires the 193 Nomad agent to be configured to allow privileged containers. 194 195 - `ipv4_address` - (Optional) The IPv4 address to be used for the container when 196 using user defined networks. Requires Docker 1.13 or greater. 197 198 - `ipv6_address` - (Optional) The IPv6 address to be used for the container when 199 using user defined networks. Requires Docker 1.13 or greater. 200 201 - `labels` - (Optional) A key-value map of labels to set to the containers on 202 start. 203 204 ```hcl 205 config { 206 labels { 207 foo = "bar" 208 zip = "zap" 209 } 210 } 211 ``` 212 213 - `load` - (Optional) Load an image from a `tar` archive file instead of from a 214 remote repository. Equivalent to the `docker load -i <filename>` command. If 215 you're using an `artifact` block to fetch the archive file, you'll need to 216 ensure that Nomad keeps the archive intact after download. 217 218 ```hcl 219 artifact { 220 source = "http://path.to/redis.tar" 221 options { 222 archive = false 223 } 224 } 225 config { 226 load = "redis.tar" 227 image = "redis" 228 } 229 ``` 230 231 - `logging` - (Optional) A key-value map of Docker logging options. 232 Defaults to `json-file` with log rotation (`max-file=2` and `max-size=2m`). 233 234 ```hcl 235 config { 236 logging { 237 type = "fluentd" 238 config { 239 fluentd-address = "localhost:24224" 240 tag = "your_tag" 241 } 242 } 243 } 244 ``` 245 246 - `mac_address` - (Optional) The MAC address for the container to use (e.g. 247 "02:68:b3:29:da:98"). 248 249 - `memory_hard_limit` - (Optional) The maximum allowable amount of memory used 250 (megabytes) by the container. If set, the [`memory`](/docs/job-specification/resources#memory) 251 parameter of the task resource configuration becomes a soft limit passed to the 252 docker driver as [`--memory_reservation`](https://docs.docker.com/config/containers/resource_constraints/#limit-a-containers-access-to-memory), 253 and `memory_hard_limit` is passed as the [`--memory`](https://docs.docker.com/config/containers/resource_constraints/#limit-a-containers-access-to-memory) 254 hard limit. When the host is under memory pressure, the behavior of soft limit 255 activation is governed by the [Kernel](https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt). 256 257 - `network_aliases` - (Optional) A list of network-scoped aliases, provide a way for a 258 container to be discovered by an alternate name by any other container within 259 the scope of a particular network. Network-scoped alias is supported only for 260 containers in user defined networks 261 262 ```hcl 263 config { 264 network_mode = "user-network" 265 network_aliases = [ 266 "${NOMAD_TASK_NAME}", 267 "${NOMAD_TASK_NAME}-${NOMAD_ALLOC_INDEX}" 268 ] 269 } 270 ``` 271 272 - `network_mode` - (Optional) The network mode to be used for the container. In 273 order to support userspace networking plugins in Docker 1.9 this accepts any 274 value. The default is `bridge` for all operating systems but Windows, which 275 defaults to `nat`. Other networking modes may not work without additional 276 configuration on the host (which is outside the scope of Nomad). Valid values 277 pre-docker 1.9 are `default`, `bridge`, `host`, `none`, or `container:name`. 278 279 The default `network_mode` for tasks that use group networking in [`bridge`] 280 mode will be `container:<name>`, where the name is the container name of the 281 parent container used to share network namespaces between tasks. If you set 282 the group `network.mode = "bridge"` you should not set the Docker config 283 `network_mode`, or the container will be unable to reach other containers in 284 the task group. This will also prevent [Connect]-enabled tasks from reaching 285 the Envoy sidecar proxy. You must also set any DNS options in the `network.dns` 286 block and not in the task configuration. 287 288 If you are in the process of migrating from the default Docker network to 289 group-wide bridge networking, you may encounter issues preventing your 290 containers from reaching networks outside of the bridge interface on systems with 291 firewalld enabled. This behavior is often caused by the CNI plugin not registering the group 292 network as trusted and can be resolved as described in the [network stanza] documentation. 293 294 - `pid_mode` - (Optional) `host` or not set (default). Set to `host` to share 295 the PID namespace with the host. Note that this also requires the Nomad agent 296 to be configured to allow privileged containers. 297 See below for more details. 298 299 - `ports` - (Optional) A list of port labels to map into the container (see below). 300 301 - `port_map` - (Optional) _Deprecated_ A key-value map of port labels (see below). 302 303 - `security_opt` - (Optional) A list of string flags to pass directly to 304 [`--security-opt`](https://docs.docker.com/engine/reference/run/#security-configuration). 305 For example: 306 307 ```hcl 308 config { 309 security_opt = [ 310 "credentialspec=file://gmsaUser.json", 311 ] 312 } 313 ``` 314 315 - `shm_size` - (Optional) The size (bytes) of /dev/shm for the container. 316 317 - `storage_opt` - (Optional) A key-value map of storage options set to the containers on start. 318 This overrides the [host dockerd configuration](https://docs.docker.com/engine/reference/commandline/dockerd/#options-per-storage-driver). 319 For example: 320 321 ```hcl 322 config { 323 storage_opt = { 324 size = "40G" 325 } 326 } 327 ``` 328 329 - `SSL` - (Optional) If this is set to true, Nomad uses SSL to talk to the 330 repository. The default value is `true`. **Deprecated as of 0.5.3** 331 332 - `tty` - (Optional) `true` or `false` (default). Allocate a pseudo-TTY for the 333 container. 334 335 - `uts_mode` - (Optional) `host` or not set (default). Set to `host` to share 336 the UTS namespace with the host. Note that this also requires the Nomad agent 337 to be configured to allow privileged containers. 338 339 - `userns_mode` - (Optional) `host` or not set (default). Set to `host` to use 340 the host's user namespace (effectively disabling user namespacing) when user 341 namespace remapping is enabled on the docker daemon. This field has no 342 effect if the docker daemon does not have user namespace remapping enabled. 343 344 - `volumes` - (Optional) A list of `host_path:container_path` strings to bind 345 host paths to container paths. Mounting host paths outside of the [allocation 346 working directory] is prevented by default and limits volumes to directories 347 that exist inside the allocation working directory. You can allow mounting 348 host paths outside of the [allocation working directory] on individual clients 349 by setting the `docker.volumes.enabled` option to `true` in the 350 [client's configuration](#client-requirements). We recommend using 351 [`mount`](#mount) if you wish to have more control over volume definitions. 352 353 ```hcl 354 config { 355 volumes = [ 356 # Use absolute paths to mount arbitrary paths on the host 357 "/path/on/host:/path/in/container", 358 359 # Use relative paths to rebind paths already in the allocation dir 360 "relative/to/task:/also/in/container" 361 ] 362 } 363 ``` 364 365 - `volume_driver` - (Optional) The name of the volume driver used to mount 366 volumes. Must be used along with `volumes`. If `volume_driver` is omitted, 367 then relative paths will be mounted from inside the allocation dir. If a 368 `"local"` or other driver is used, then they may be named volumes instead. 369 If `docker.volumes.enabled` is false then volume drivers and paths outside the 370 allocation directory are disallowed. 371 372 ```hcl 373 config { 374 volumes = [ 375 # Use named volume created outside nomad. 376 "name-of-the-volume:/path/in/container" 377 ] 378 # Name of the Docker Volume Driver used by the container 379 volume_driver = "pxd" 380 } 381 ``` 382 383 - `work_dir` - (Optional) The working directory inside the container. 384 385 - `mount` - _Since 1.0.1_ (Optional) Specify a 386 [mount](https://docs.docker.com/engine/reference/commandline/service_create/#add-bind-mounts-volumes-or-memory-filesystems) 387 to be mounted into the container. Volume, bind, and tmpfs type mounts are supported. May be specified multiple times. 388 389 ```hcl 390 config { 391 # sample volume mount 392 mount { 393 type = "volume" 394 target = "/path/in/container" 395 source = "name-of-volume" 396 readonly = false 397 volume_options { 398 no_copy = false 399 labels { 400 foo = "bar" 401 } 402 driver_config { 403 name = "pxd" 404 options { 405 foo = "bar" 406 } 407 } 408 } 409 } 410 411 # sample bind mount 412 mount { 413 type = "bind" 414 target = "/path/in/container" 415 source = "/path/in/host" 416 readonly = false 417 bind_options { 418 propagation = "rshared" 419 } 420 } 421 422 # sample tmpfs mount 423 mount { 424 type = "tmpfs" 425 target = "/path/in/container" 426 readonly = false 427 tmpfs_options { 428 size = 100000 # size in bytes 429 } 430 } 431 } 432 ``` 433 434 - `mounts` - (_deprecated_: Replaced by `mount` in 1.0.1) (Optional) A list of 435 [mounts](https://docs.docker.com/engine/reference/commandline/service_create/#add-bind-mounts-volumes-or-memory-filesystems) 436 to be mounted into the container. Volume, bind, and tmpfs type mounts are supported. 437 438 ```hcl 439 config { 440 mounts = [ 441 # sample volume mount 442 { 443 type = "volume" 444 target = "/path/in/container" 445 source = "name-of-volume" 446 readonly = false 447 volume_options = { 448 no_copy = false 449 labels = { 450 foo = "bar" 451 } 452 driver_config = { 453 name = "pxd" 454 options = { 455 foo = "bar" 456 } 457 } 458 } 459 }, 460 # sample bind mount 461 { 462 type = "bind" 463 target = "/path/in/container" 464 source = "/path/in/host" 465 readonly = false 466 bind_options = { 467 propagation = "rshared" 468 } 469 }, 470 # sample tmpfs mount 471 { 472 type = "tmpfs" 473 target = "/path/in/container" 474 readonly = false 475 tmpfs_options = { 476 size = 100000 # size in bytes 477 } 478 } 479 ] 480 } 481 ``` 482 483 - `devices` - (Optional) A list of 484 [devices](https://docs.docker.com/engine/reference/commandline/run/#add-host-device-to-container-device) 485 to be exposed the container. `host_path` is the only required field. By default, the container will be able to 486 `read`, `write` and `mknod` these devices. Use the optional `cgroup_permissions` field to restrict permissions. 487 488 ```hcl 489 config { 490 devices = [ 491 { 492 host_path = "/dev/sda1" 493 container_path = "/dev/xvdc" 494 cgroup_permissions = "r" 495 }, 496 { 497 host_path = "/dev/sda2" 498 container_path = "/dev/xvdd" 499 } 500 ] 501 } 502 ``` 503 504 - `cap_add` - (Optional) A list of Linux capabilities as strings to pass directly to 505 [`--cap-add`](https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities). 506 Effective capabilities (computed from `cap_add` and `cap_drop`) have to match the configured allowlist. 507 The allowlist can be customized using the [`allow_caps`][allow_caps] plugin option key in the client node's configuration. 508 For example: 509 510 ```hcl 511 config { 512 cap_add = ["net_raw", "sys_time"] 513 } 514 ``` 515 516 - `cap_drop` - (Optional) A list of Linux capabilities as strings to pass directly to 517 [`--cap-drop`](https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities). 518 Effective capabilities (computed from `cap_add` and `cap_drop`) have to match the configured allowlist. 519 The allowlist can be customized using the [`allow_caps`][allow_caps] plugin option key in the client node's configuration. 520 For example: 521 522 ```hcl 523 config { 524 cap_drop = ["mknod"] 525 } 526 ``` 527 528 - `cpu_hard_limit` - (Optional) `true` or `false` (default). Use hard CPU 529 limiting instead of soft limiting. By default this is `false` which means 530 soft limiting is used and containers are able to burst above their CPU limit 531 when there is idle capacity. 532 533 - `cpu_cfs_period` - (Optional) An integer value that specifies the duration in microseconds of the period 534 during which the CPU usage quota is measured. The default is 100000 (0.1 second) and the maximum allowed 535 value is 1000000 (1 second). See [here](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/sec-cpu#sect-cfs) 536 for more details. 537 538 - `advertise_ipv6_address` - (Optional) `true` or `false` (default). Use the container's 539 IPv6 address (GlobalIPv6Address in Docker) when registering services and checks. 540 See [IPv6 Docker containers](/docs/job-specification/service#ipv6-docker-containers) for details. 541 542 - `readonly_rootfs` - (Optional) `true` or `false` (default). Mount 543 the container's filesystem as read only. 544 545 - `runtime` - (Optional) A string representing a configured runtime to pass to docker. 546 This is equivalent to the `--runtime` argument in the docker CLI 547 For example, to use gVisor: 548 549 ```hcl 550 config { 551 # gVisor runtime is runsc 552 runtime = "runsc" 553 } 554 ``` 555 556 - `pids_limit` - (Optional) An integer value that specifies the pid limit for 557 the container. Defaults to unlimited. 558 559 Additionally, the docker driver supports customization of the container's user through the task's [`user` option](/docs/job-specification/task#user). 560 561 ### Container Name 562 563 Nomad creates a container after pulling an image. Containers are named 564 `{taskName}-{allocId}`. This is necessary in order to place more than one 565 container from the same task on a host (e.g. with count > 1). This also means 566 that each container's name is unique across the cluster. 567 568 This is not configurable. 569 570 ### Authentication 571 572 If you want to pull from a private repo (for example on dockerhub or quay.io), 573 you will need to specify credentials in your job via: 574 575 - the `auth` option in the task config. 576 577 - by storing explicit repository credentials or by specifying Docker 578 `credHelpers` in a file and setting the auth [config](#plugin_auth_file) 579 value on the client in the plugin options. 580 581 - by specifying an auth [helper](#plugin_auth_helper) on the client in the 582 plugin options. 583 584 The `auth` object supports the following keys: 585 586 - `username` - (Optional) The account username. 587 588 - `password` - (Optional) The account password. 589 590 - `email` - (Optional) The account email. 591 592 - `server_address` - (Optional) The server domain/IP without the protocol. 593 Docker Hub is used by default. 594 595 Example task-config: 596 597 ```hcl 598 task "example" { 599 driver = "docker" 600 601 config { 602 image = "secret/service" 603 604 auth { 605 username = "dockerhub_user" 606 password = "dockerhub_password" 607 } 608 } 609 } 610 ``` 611 612 Example docker-config, using two helper scripts in `$PATH`, 613 "docker-credential-ecr-login" and "docker-credential-vault": 614 615 ```json 616 { 617 "auths": { 618 "internal.repo": { 619 "auth": "`echo -n '<username>:<password>' | base64 -w0`" 620 } 621 }, 622 "credHelpers": { 623 "<XYZ>.dkr.ecr.<region>.amazonaws.com": "ecr-login" 624 }, 625 "credsStore": "secretservice" 626 } 627 ``` 628 629 Example agent configuration, using a helper script 630 "docker-credential-ecr-login" in `$PATH` 631 632 ```hcl 633 client { 634 enabled = true 635 } 636 637 plugin "docker" { 638 config { 639 auth { 640 # Nomad will prepend "docker-credential-" to the helper value and call 641 # that script name. 642 helper = "ecr-login" 643 } 644 } 645 } 646 ``` 647 648 !> **Be Careful!** At this time these credentials are stored in Nomad in plain 649 text. Secrets management will be added in a later release. 650 651 ## Networking 652 653 Docker supports a variety of networking configurations, including using host 654 interfaces, SDNs, etc. Nomad uses `bridged` networking by default, like Docker. 655 656 You can specify other networking options, including custom networking plugins 657 in Docker 1.9. **You may need to perform additional configuration on the host 658 in order to make these work.** This additional configuration is outside the 659 scope of Nomad. 660 661 ### Allocating Ports 662 663 You can allocate ports to your task using the port syntax described on the 664 [networking page](/docs/job-specification/network). Here is a recap: 665 666 ```hcl 667 group { 668 network { 669 port "http" {} 670 port "https" {} 671 } 672 task "example" { 673 driver = "docker" 674 config { 675 ports = ["http", "https"] 676 } 677 } 678 } 679 ``` 680 681 ### Forwarding and Exposing Ports 682 683 A Docker container typically specifies which port a service will listen on by 684 specifying the `EXPOSE` directive in the `Dockerfile`. 685 686 Because dynamic ports will not match the ports exposed in your Dockerfile, 687 Nomad will automatically expose any ports specified in the `ports` field. 688 689 These ports will be identified via environment variables. For example: 690 691 ```hcl 692 group { 693 network { 694 port "http" {} 695 } 696 task "api" { 697 driver = "docker" 698 config { 699 ports = ["http"] 700 } 701 } 702 } 703 ``` 704 705 If Nomad allocates port `23332` to your api task for `http`, `23332` will be 706 automatically exposed and forwarded to your container, and the driver will set 707 an environment variable `NOMAD_PORT_http` with the value `23332` that you can 708 read inside your container. 709 710 This provides an easy way to use the `host` networking option for better 711 performance. 712 713 ### Using the Port Map 714 715 If you prefer to use the traditional port-mapping method, you can specify the 716 the `to` field in the port configuration. It looks like this: 717 718 ```hcl 719 group "example" { 720 network { 721 port "redis" { to = 6379 } 722 } 723 task "example" { 724 driver = "docker" 725 726 config { 727 image = "redis" 728 ports = ["redis"] 729 } 730 } 731 } 732 ``` 733 734 If Nomad allocates port `23332` to your allocation, the Docker driver will 735 automatically setup the port mapping from `23332` on the host to `6379` in your 736 container, so it will just work. 737 738 Note that by default this only works with `bridged` networking mode. It may 739 also work with custom networking plugins which implement the same API for 740 expose and port forwarding. 741 742 #### Deprecated `port_map` Syntax 743 744 Up until Nomad 0.12, ports could be specified in a task's resource stanza and set using the docker 745 `port_map` field. As more features have been added to the group network resource allocation, task based 746 network resources are deprecated. With it the `port_map` field is also deprecated and can only be used 747 with task network resources. 748 749 Users should migrate their jobs to define ports in the group network stanza and specified which ports 750 a task maps with the `ports` field. 751 752 ### Advertising Container IPs 753 754 _New in Nomad 0.6._ 755 756 When using network plugins like `weave` that assign containers a routable IP 757 address, that address will automatically be used in any `service` 758 advertisements for the task. You may override what address is advertised by 759 using the `address_mode` parameter on a `service`. See 760 [service](/docs/job-specification/service) for details. 761 762 ### Networking Protocols 763 764 The Docker driver configures ports on both the `tcp` and `udp` protocols. 765 766 This is not configurable. 767 768 ### Other Networking Modes 769 770 Some networking modes like `container` or `none` will require coordination 771 outside of Nomad. First-class support for these options may be improved later 772 through Nomad plugins or dynamic job configuration. 773 774 ## Capabilities 775 776 The `docker` driver implements the following [capabilities](/docs/concepts/plugins/task-drivers#capabilities-capabilities-error). 777 778 | Feature | Implementation | 779 | -------------------- | ----------------- | 780 | `nomad alloc signal` | true | 781 | `nomad alloc exec` | true | 782 | filesystem isolation | image | 783 | network isolation | host, group, task | 784 | volume mounting | all | 785 786 ## Client Requirements 787 788 Nomad requires Docker to be installed and running on the host alongside the 789 Nomad agent. Nomad was developed against Docker `1.8.2` and `1.9`. 790 791 By default Nomad communicates with the Docker daemon using the daemon's Unix 792 socket. Nomad will need to be able to read/write to this socket. If you do not 793 run Nomad as root, make sure you add the Nomad user to the Docker group so 794 Nomad can communicate with the Docker daemon. 795 796 For example, on Ubuntu you can use the `usermod` command to add the `nomad` 797 user to the `docker` group so you can run Nomad without root: 798 799 ```shell-session 800 $ sudo usermod -G docker -a nomad 801 ``` 802 803 For the best performance and security features you should use recent versions 804 of the Linux Kernel and Docker daemon. 805 806 If you would like to change any of the options related to the `docker` driver 807 on a Nomad client, you can modify them with the [plugin stanza][plugin-stanza] 808 syntax. Below is an example of a configuration (many of the values are the 809 default). See the next section for more information on the options. 810 811 ```hcl 812 plugin "docker" { 813 config { 814 endpoint = "unix:///var/run/docker.sock" 815 816 auth { 817 config = "/etc/docker-auth.json" 818 helper = "ecr-login" 819 } 820 821 tls { 822 cert = "/etc/nomad/nomad.pub" 823 key = "/etc/nomad/nomad.pem" 824 ca = "/etc/nomad/nomad.cert" 825 } 826 827 extra_labels = ["job_name", "job_id", "task_group_name", "task_name", "namespace", "node_name", "node_id"] 828 829 gc { 830 image = true 831 image_delay = "3m" 832 container = true 833 834 dangling_containers { 835 enabled = true 836 dry_run = false 837 period = "5m" 838 creation_grace = "5m" 839 } 840 } 841 842 volumes { 843 enabled = true 844 selinuxlabel = "z" 845 } 846 847 allow_privileged = false 848 allow_caps = ["chown", "net_raw"] 849 } 850 } 851 ``` 852 853 ## Plugin Options 854 855 - `endpoint` - If using a non-standard socket, HTTP or another location, or if 856 TLS is being used, docker.endpoint must be set. If unset, Nomad will attempt 857 to instantiate a Docker client using the `DOCKER_HOST` environment variable and 858 then fall back to the default listen address for the given operating system. 859 Defaults to `unix:///var/run/docker.sock` on Unix platforms and 860 `npipe:////./pipe/docker_engine` for Windows. 861 862 - `allow_privileged` - Defaults to `false`. Changing this to true will allow 863 containers to use privileged mode, which gives the containers full access to 864 the host's devices. Note that you must set a similar setting on the Docker 865 daemon for this to work. 866 867 - `pull_activity_timeout` - Defaults to `2m`. If Nomad receives no communication 868 from the Docker engine during an image pull within this timeframe, Nomad will 869 timeout the request that initiated the pull command. (Minimum of `1m`) 870 871 - `pids_limit` - Defaults to unlimited (`0`). An integer value that specifies 872 the pid limit for all the Docker containers running on that Nomad client. You 873 can override this limit by setting [`pids_limit`] in your task config. If 874 this value is greater than `0`, your task `pids_limit` must be less than or 875 equal to the value defined here. 876 877 - `allow_caps` - A list of allowed Linux capabilities. Defaults to 878 879 ```hcl 880 ["audit_write", "chown", "dac_override", "fowner", "fsetid", "kill", "mknod", 881 "net_bind_service", "setfcap", "setgid", "setpcap", "setuid", "sys_chroot"] 882 ``` 883 884 which is the same list of capabilities allowed by [docker by default][docker_caps] 885 (without [`NET_RAW`][no_net_raw]). Allows the operator to control which capabilities can be obtained 886 by tasks using [`cap_add`][cap_add] and [`cap_drop`][cap_drop] options. Supports 887 the value `"all"` as a shortcut for allow-listing all capabilities supported by 888 the operating system. 889 890 !> **Warning:** Allowing more capabilities beyond the default may lead to 891 undesirable consequences, including untrusted tasks being able to compromise the 892 host system. 893 894 - `allow_runtimes` - defaults to `["runc", "nvidia"]` - A list of the allowed 895 docker runtimes a task may use. 896 897 - `auth` stanza: 898 899 - `config`<a id="plugin_auth_file"></a> - Allows an operator to specify a 900 JSON file which is in the dockercfg format containing authentication 901 information for a private registry, from either (in order) `auths`, 902 `credHelpers` or `credsStore`. 903 904 - `helper`<a id="plugin_auth_helper"></a> - Allows an operator to specify a 905 [credsStore](https://docs.docker.com/engine/reference/commandline/login/#credential-helper-protocol) 906 like script on `$PATH` to lookup authentication information from external 907 sources. The script's name must begin with `docker-credential-` and this 908 option should include only the basename of the script, not the path. 909 910 If you set an auth helper, it will be tried for all images, including 911 public images. If you mix private and public images, you will need to 912 include [`auth_soft_fail=true`] in every job using a public image. 913 914 - `tls` stanza: 915 916 - `cert` - Path to the server's certificate file (`.pem`). Specify this 917 along with `key` and `ca` to use a TLS client to connect to the docker 918 daemon. `endpoint` must also be specified or this setting will be ignored. 919 920 - `key` - Path to the client's private key (`.pem`). Specify this along with 921 `cert` and `ca` to use a TLS client to connect to the docker daemon. 922 `endpoint` must also be specified or this setting will be ignored. 923 924 - `ca` - Path to the server's CA file (`.pem`). Specify this along with 925 `cert` and `key` to use a TLS client to connect to the docker daemon. 926 `endpoint` must also be specified or this setting will be ignored. 927 928 - `disable_log_collection` - Defaults to `false`. Setting this to true will 929 disable Nomad logs collection of Docker tasks. If you don't rely on nomad log 930 capabilities and exclusively use host based log aggregation, you may consider 931 this option to disable nomad log collection overhead. 932 933 - `extra_labels` - Extra labels to add to Docker containers. 934 Available options are `job_name`, `job_id`, `task_group_name`, `task_name`, 935 `namespace`, `node_name`, `node_id`. Globs are supported (e.g. `task*`) 936 937 - `logging` stanza: 938 939 - `type` - Defaults to `"json-file"`. Specifies the logging driver docker 940 should use for all containers Nomad starts. Note that for older versions 941 of Docker, only `json-file` file or `journald` will allow Nomad to read 942 the driver's logs via the Docker API, and this will prevent commands such 943 as `nomad alloc logs` from functioning. 944 945 - `config` - Defaults to `{ max-file = "2", max-size = "2m" }`. This option 946 can also be used to pass further 947 [configuration](https://docs.docker.com/config/containers/logging/configure/) 948 to the logging driver. 949 950 - `gc` stanza: 951 952 - `image` - Defaults to `true`. Changing this to `false` will prevent Nomad 953 from removing images from stopped tasks. 954 955 - `image_delay` - A time duration, as [defined 956 here](https://golang.org/pkg/time/#ParseDuration), that defaults to `3m`. 957 The delay controls how long Nomad will wait between an image being unused 958 and deleting it. If a task is received that uses the same image within 959 the delay, the image will be reused. 960 961 - `container` - Defaults to `true`. This option can be used to disable Nomad 962 from removing a container when the task exits. Under a name conflict, 963 Nomad may still remove the dead container. 964 965 - `dangling_containers` stanza for controlling dangling container detection 966 and cleanup: 967 968 - `enabled` - Defaults to `true`. Enables dangling container handling. 969 970 - `dry_run` - Defaults to `false`. Only log dangling containers without 971 cleaning them up. 972 973 - `period` - Defaults to `"5m"`. A time duration that controls interval 974 between Nomad scans for dangling containers. 975 976 - `creation_grace` - Defaults to `"5m"`. Grace period after a container is 977 created during which the GC ignores it. Only used to prevent the GC from 978 removing newly created containers before they are registered with the 979 GC. Should not need adjusting higher but may be adjusted lower to GC 980 more aggressively. 981 982 - `volumes` stanza: 983 984 - `enabled` - Defaults to `false`. Allows tasks to bind host paths 985 (`volumes`) inside their container and use volume drivers 986 (`volume_driver`). Binding relative paths is always allowed and will be 987 resolved relative to the allocation's directory. 988 989 - `selinuxlabel` - Allows the operator to set a SELinux label to the 990 allocation and task local bind-mounts to containers. If used with 991 `docker.volumes.enabled` set to false, the labels will still be applied to 992 the standard binds in the container. 993 994 - `infra_image` - This is the Docker image to use when creating the parent 995 container necessary when sharing network namespaces between tasks. Defaults to 996 `gcr.io/google_containers/pause-<goarch>:3.1`. The image will only be pulled from 997 the container registry if its tag is `latest` or the image doesn't yet exist locally. 998 999 - `infra_image_pull_timeout` - A time duration that controls how long Nomad will 1000 wait before cancelling an in-progress pull of the Docker image as specified in 1001 `infra_image`. Defaults to `"5m"`. 1002 1003 ## Client Configuration 1004 1005 ~> Note: client configuration options will soon be deprecated. Please use 1006 [plugin options][plugin-options] instead. See the [plugin stanza][plugin-stanza] 1007 documentation for more information. 1008 1009 The `docker` driver has the following [client configuration 1010 options](/docs/configuration/client#options): 1011 1012 - `docker.endpoint` - If using a non-standard socket, HTTP or another location, 1013 or if TLS is being used, `docker.endpoint` must be set. If unset, Nomad will 1014 attempt to instantiate a Docker client using the `DOCKER_HOST` environment 1015 variable and then fall back to the default listen address for the given 1016 operating system. Defaults to `unix:///var/run/docker.sock` on Unix platforms 1017 and `npipe:////./pipe/docker_engine` for Windows. 1018 1019 - `docker.auth.config` <a id="auth_file"></a>- Allows an operator to specify a 1020 JSON file which is in the dockercfg format containing authentication 1021 information for a private registry, from either (in order) `auths`, 1022 `credHelpers` or `credsStore`. 1023 1024 - `docker.auth.helper` <a id="auth_helper"></a>- Allows an operator to specify a 1025 [credsStore](https://docs.docker.com/engine/reference/commandline/login/#credential-helper-protocol) 1026 -like script on \$PATH to lookup authentication information from external 1027 sources. The script's name must begin with `docker-credential-` and this 1028 option should include only the basename of the script, not the path. 1029 1030 - `docker.tls.cert` - Path to the server's certificate file (`.pem`). Specify 1031 this along with `docker.tls.key` and `docker.tls.ca` to use a TLS client to 1032 connect to the docker daemon. `docker.endpoint` must also be specified or this 1033 setting will be ignored. 1034 1035 - `docker.tls.key` - Path to the client's private key (`.pem`). Specify this 1036 along with `docker.tls.cert` and `docker.tls.ca` to use a TLS client to 1037 connect to the docker daemon. `docker.endpoint` must also be specified or this 1038 setting will be ignored. 1039 1040 - `docker.tls.ca` - Path to the server's CA file (`.pem`). Specify this along 1041 with `docker.tls.cert` and `docker.tls.key` to use a TLS client to connect to 1042 the docker daemon. `docker.endpoint` must also be specified or this setting 1043 will be ignored. 1044 1045 - `docker.cleanup.image` Defaults to `true`. Changing this to `false` will 1046 prevent Nomad from removing images from stopped tasks. 1047 1048 - `docker.cleanup.image.delay` A time duration, as [defined 1049 here](https://golang.org/pkg/time/#ParseDuration), that defaults to `3m`. The 1050 delay controls how long Nomad will wait between an image being unused and 1051 deleting it. If a tasks is received that uses the same image within the delay, 1052 the image will be reused. 1053 1054 - `docker.volumes.enabled`: Defaults to `false`. Allows tasks to bind host paths 1055 (`volumes`) inside their container and use volume drivers (`volume_driver`). 1056 Binding relative paths is always allowed and will be resolved relative to the 1057 allocation's directory. 1058 1059 - `docker.volumes.selinuxlabel`: Allows the operator to set a SELinux label to 1060 the allocation and task local bind-mounts to containers. If used with 1061 `docker.volumes.enabled` set to false, the labels will still be applied to the 1062 standard binds in the container. 1063 1064 - `docker.privileged.enabled` Defaults to `false`. Changing this to `true` will 1065 allow containers to use `privileged` mode, which gives the containers full 1066 access to the host's devices. Note that you must set a similar setting on the 1067 Docker daemon for this to work. 1068 1069 - `docker.caps.allowlist`: A list of allowed Linux capabilities. Defaults to 1070 `"CHOWN,DAC_OVERRIDE,FSETID,FOWNER,MKNOD,NET_RAW,SETGID,SETUID,SETFCAP, SETPCAP,NET_BIND_SERVICE,SYS_CHROOT,KILL,AUDIT_WRITE"`, which is the list of 1071 capabilities allowed by docker by default, as [defined 1072 here](https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities). 1073 Allows the operator to control which capabilities can be obtained by tasks 1074 using `cap_add` and `cap_drop` options. Supports the value `"ALL"` as a 1075 shortcut for allowlisting all capabilities. 1076 1077 - `docker.cleanup.container`: Defaults to `true`. This option can be used to 1078 disable Nomad from removing a container when the task exits. Under a name 1079 conflict, Nomad may still remove the dead container. 1080 1081 - `docker.nvidia_runtime`: Defaults to `nvidia`. This option allows operators to select the runtime that should be used in order to expose Nvidia GPUs to the container. 1082 1083 Note: When testing or using the `-dev` flag you can use `DOCKER_HOST`, 1084 `DOCKER_TLS_VERIFY`, and `DOCKER_CERT_PATH` to customize Nomad's behavior. If 1085 `docker.endpoint` is set Nomad will **only** read client configuration from the 1086 config file. 1087 1088 An example is given below: 1089 1090 ```hcl 1091 client { 1092 options { 1093 "docker.cleanup.image" = "false" 1094 } 1095 } 1096 ``` 1097 1098 ## Client Attributes 1099 1100 The `docker` driver will set the following client attributes: 1101 1102 - `driver.docker` - This will be set to "1", indicating the driver is 1103 available. 1104 1105 - `driver.docker.bridge_ip` - The IP of the Docker bridge network if one 1106 exists. 1107 1108 - `driver.docker.version` - This will be set to version of the docker server. 1109 1110 Here is an example of using these properties in a job file: 1111 1112 ```hcl 1113 job "docs" { 1114 # Require docker version higher than 1.2. 1115 constraint { 1116 attribute = "${attr.driver.docker.version}" 1117 operator = ">" 1118 version = "1.2" 1119 } 1120 } 1121 ``` 1122 1123 ## Resource Isolation 1124 1125 ### CPU 1126 1127 Nomad limits containers' CPU based on CPU shares. CPU shares allow containers 1128 to burst past their CPU limits. CPU limits will only be imposed when there is 1129 contention for resources. When the host is under load your process may be 1130 throttled to stabilize QoS depending on how many shares it has. You can see how 1131 many CPU shares are available to your process by reading `NOMAD_CPU_LIMIT`. 1132 1000 shares are approximately equal to 1 GHz. 1133 1134 Please keep the implications of CPU shares in mind when you load test workloads 1135 on Nomad. 1136 1137 ### Memory 1138 1139 Nomad limits containers' memory usage based on total virtual memory. This means 1140 that containers scheduled by Nomad cannot use swap. This is to ensure that a 1141 swappy process does not degrade performance for other workloads on the same 1142 host. 1143 1144 Since memory is not an elastic resource, you will need to make sure your 1145 container does not exceed the amount of memory allocated to it, or it will be 1146 terminated or crash when it tries to malloc. A process can inspect its memory 1147 limit by reading `NOMAD_MEMORY_LIMIT`, but will need to track its own memory 1148 usage. Memory limit is expressed in megabytes so 1024 = 1 GB. 1149 1150 ### IO 1151 1152 Nomad's Docker integration does not currently provide QoS around network or 1153 filesystem IO. These will be added in a later release. 1154 1155 ### Security 1156 1157 Docker provides resource isolation by way of 1158 [cgroups and namespaces](https://docs.docker.com/introduction/understanding-docker/#the-underlying-technology). 1159 Containers essentially have a virtual file system all to themselves. If you 1160 need a higher degree of isolation between processes for security or other 1161 reasons, it is recommended to use full virtualization like 1162 [QEMU](/docs/drivers/qemu). 1163 1164 ## Caveats 1165 1166 ### Dangling Containers 1167 1168 Nomad 0.10.2 introduces a detector and a reaper for dangling Docker containers, 1169 containers that Nomad starts yet does not manage or track. Though rare, they 1170 lead to unexpectedly running services, potentially with stale versions. 1171 1172 When Docker daemon becomes unavailable as Nomad starts a task, it is possible 1173 for Docker to successfully start the container but return a 500 error code from 1174 the API call. In such cases, Nomad retries and eventually aims to kill such 1175 containers. However, if the Docker Engine remains unhealthy, subsequent retries 1176 and stop attempts may still fail, and the started container becomes a dangling 1177 container that Nomad no longer manages. 1178 1179 The newly added reaper periodically scans for such containers. It only targets 1180 containers with a `com.hashicorp.nomad.allocation_id` label, or match Nomad's 1181 conventions for naming and bind-mounts (i.e. `/alloc`, `/secrets`, `local`). 1182 Containers that don't match Nomad container patterns are left untouched. 1183 1184 Operators can run the reaper in a dry-run mode, where it only logs dangling 1185 container ids without killing them, or disable it by setting the 1186 `gc.dangling_containers` config stanza. 1187 1188 ### Docker for Windows 1189 1190 Docker for Windows only supports running Windows containers. Because Docker for 1191 Windows is relatively new and rapidly evolving you may want to consult the 1192 [list of relevant issues on GitHub][winissues]. 1193 1194 [faq-win-mac]: /docs/faq#q-how-to-connect-to-my-host-network-when-using-docker-desktop-windows-and-macos 1195 [winissues]: https://github.com/hashicorp/nomad/issues?q=is%3Aopen+is%3Aissue+label%3Atheme%2Fdriver%2Fdocker+label%3Atheme%2Fplatform-windows 1196 [plugin-options]: #plugin-options 1197 [plugin-stanza]: /docs/configuration/plugin 1198 [allocation working directory]: /docs/runtime/environment#task-directories 'Task Directories' 1199 [`auth_soft_fail=true`]: #auth_soft_fail 1200 [cap_add]: /docs/drivers/docker#cap_add 1201 [cap_drop]: /docs/drivers/docker#cap_drop 1202 [no_net_raw]: /docs/upgrade/upgrade-specific#nomad-1-1-0-rc1-1-0-5-0-12-12 1203 [upgrade_guide_extra_hosts]: /docs/upgrade/upgrade-specific#docker-driver 1204 [tini]: https://github.com/krallin/tini 1205 [docker_caps]: https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities 1206 [allow_caps]: /docs/drivers/docker#allow_caps 1207 [Connect]: /docs/job-specification/connect 1208 [`bridge`]: /docs/job-specification/network#bridge 1209 [network stanza]: /docs/job-specification/network#bridge-mode 1210 [`pids_limit`]: /docs/drivers/docker#pids_limit