github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/website/source/docs/drivers/docker.html.md (about)

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