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