github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/drivers/docker.mdx (about)

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