github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/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  The `docker` driver is configured via a `config` block:
    20  
    21  ```
    22  task "webservice" {
    23      driver = "docker"
    24      config = {
    25          image = "redis"
    26          labels = {
    27              group = "webservice-cache"
    28          }
    29      }
    30  }
    31  ```
    32  
    33  The following options are available for use in the job specification.
    34  
    35  * `image` - The Docker image to run. The image may include a tag or custom URL and should include `https://` if required.
    36    By default it will be fetched from Docker Hub.
    37  
    38  * `load` - (Optional) A list of paths to image archive files. If
    39    this key is not specified, Nomad assumes the `image` is hosted on a repository
    40    and attempts to pull the image. The `artifact` blocks can be specified to
    41    download each of the archive files. The equivalent of `docker load -i path`
    42    would be run on each of the archive files.
    43  
    44  * `command` - (Optional) The command to run when starting the container.
    45  
    46  * `args` - (Optional) A list of arguments to the optional `command`. If no
    47    `command` is present, `args` are ignored. References to environment variables
    48    or any [interpretable Nomad variables](/docs/jobspec/interpreted.html) will be
    49    interpreted before launching the task. For example:
    50  
    51    ```
    52    args = ["${nomad.datacenter}", "${MY_ENV}", "${meta.foo}"]
    53    ```
    54  
    55  * `labels` - (Optional) A key/value map of labels to set to the containers on
    56    start.
    57  
    58  * `privileged` - (Optional) `true` or `false` (default). Privileged mode gives
    59    the container access to devices on the host. Note that this also requires the
    60    nomad agent and docker daemon to be configured to allow privileged
    61    containers.
    62  
    63  * `ipc_mode` - (Optional) The IPC mode to be used for the container. The default
    64    is `none` for a private IPC namespace. Other values are `host` for sharing
    65    the host IPC namespace or the name or id of an existing container. Note that
    66    it is not possible to refer to Nomad started Docker containers since their
    67    names are not known in advance. Note that setting this option also requires the
    68    Nomad agent to be configured to allow privileged containers.
    69  
    70  * `pid_mode` - (Optional) `host` or not set (default). Set to `host` to share
    71    the PID namespace with the host. Note that this also requires the Nomad agent
    72    to be configured to allow privileged containers.
    73  
    74  * `uts_mode` - (Optional) `host` or not set (default). Set to `host` to share
    75    the UTS namespace with the host. Note that this also requires the Nomad agent
    76    to be configured to allow privileged containers.
    77  
    78  * `network_mode` - (Optional) The network mode to be used for the container. In
    79    order to support userspace networking plugins in Docker 1.9 this accepts any
    80    value. The default is `bridge` for all operating systems but Windows, which
    81    defaults to `nat`. Other networking modes may not work without additional
    82    configuration on the host (which is outside the scope of Nomad).  Valid values
    83    pre-docker 1.9 are `default`, `bridge`, `host`, `none`, or `container:name`.
    84    See below for more details.
    85  
    86  * `hostname` - (Optional) The hostname to assign to the container. When
    87    launching more than one of a task (using `count`) with this option set, every
    88    container the task starts will have the same hostname.
    89  
    90  * `dns_servers` - (Optional) A list of DNS servers for the container to use
    91    (e.g. ["8.8.8.8", "8.8.4.4"]). *Docker API v1.10 and above only*
    92  
    93  * `dns_search_domains` - (Optional) A list of DNS search domains for the container
    94    to use.
    95  
    96  * `SSL` - (Optional) If this is set to true, Nomad uses SSL to talk to the
    97    repository. The default value is `true`.
    98  
    99  * `port_map` - (Optional) A key/value map of port labels (see below).
   100  
   101  * `auth` - (Optional) Provide authentication for a private registry (see below).
   102  
   103  * `tty` - (Optional) `true` or `false` (default). Allocate a pseudo-TTY for the
   104    container.
   105  
   106  * `interactive` - (Optional) `true` or `false` (default). Keep STDIN open on
   107    the container.
   108    
   109  * `shm_size` - (Optional) The size (bytes) of /dev/shm for the container.
   110  
   111  * `work_dir` - (Optional) The working directory inside the container.
   112  
   113  ### Container Name
   114  
   115  Nomad creates a container after pulling an image. Containers are named
   116  `{taskName}-{allocId}`. This is necessary in order to place more than one
   117  container from the same task on a host (e.g. with count > 1). This also means
   118  that each container's name is unique across the cluster.
   119  
   120  This is not configurable.
   121  
   122  ### Authentication
   123  
   124  If you want to pull from a private repo (for example on dockerhub or quay.io),
   125  you will need to specify credentials in your job via the `auth` option.
   126  
   127  The `auth` object supports the following keys:
   128  
   129  * `username` - (Optional) The account username.
   130  
   131  * `password` - (Optional) The account password.
   132  
   133  * `email` - (Optional) The account email.
   134  
   135  * `server_address` - (Optional) The server domain/IP without the protocol.
   136    Docker Hub is used by default.
   137  
   138  Example:
   139  
   140  ```
   141  task "secretservice" {
   142      driver = "docker"
   143  
   144      config {
   145          image = "secret/service"
   146  
   147          auth {
   148              username = "dockerhub_user"
   149              password = "dockerhub_password"
   150          }
   151      }
   152  }
   153  ```
   154  
   155  **Please note that these credentials are stored in Nomad in plain text.**
   156  Secrets management will be added in a later release.
   157  
   158  ## Networking
   159  
   160  Docker supports a variety of networking configurations, including using host
   161  interfaces, SDNs, etc. Nomad uses `bridged` networking by default, like Docker.
   162  
   163  You can specify other networking options, including custom networking plugins
   164  in Docker 1.9. **You may need to perform additional configuration on the host
   165  in order to make these work.** This additional configuration is outside the
   166  scope of Nomad.
   167  
   168  ### Allocating Ports
   169  
   170  You can allocate ports to your task using the port syntax described on the
   171  [networking page](/docs/jobspec/networking.html). Here is a recap:
   172  
   173  ```
   174  task "webservice" {
   175      driver = "docker"
   176  
   177      resources {
   178          network {
   179              port "http" {}
   180              port "https" {}
   181          }
   182      }
   183  }
   184  ```
   185  
   186  ### Forwarding and Exposing Ports
   187  
   188  A Docker container typically specifies which port a service will listen on by
   189  specifying the `EXPOSE` directive in the `Dockerfile`.
   190  
   191  Because dynamic ports will not match the ports exposed in your Dockerfile,
   192  Nomad will automatically expose all of the ports it allocates to your
   193  container.
   194  
   195  These ports will be identified via environment variables. For example:
   196  
   197  ```
   198  port "http" {}
   199  ```
   200  
   201  If Nomad allocates port `23332` to your task for `http`, `23332` will be
   202  automatically exposed and forwarded to your container, and the driver will set
   203  an environment variable `NOMAD_PORT_http` with the value `23332` that you can
   204  read inside your container.
   205  
   206  This provides an easy way to use the `host` networking option for better
   207  performance.
   208  
   209  ### Using the Port Map
   210  
   211  If you prefer to use the traditional port-mapping method, you can specify the
   212  `port_map` option in your job specification. It looks like this:
   213  
   214  ```
   215  task "redis" {
   216      driver = "docker"
   217  
   218      resources {
   219          network {
   220              mbits = 20
   221              port "redis" {}
   222          }
   223      }
   224  
   225      config {
   226        image = "redis"
   227  
   228        port_map {
   229          redis = 6379
   230        }
   231      }
   232  }
   233  ```
   234  
   235  If Nomad allocates port `23332` to your task, the Docker driver will
   236  automatically setup the port mapping from `23332` on the host to `6379` in your
   237  container, so it will just work!
   238  
   239  Note that by default this only works with `bridged` networking mode. It may
   240  also work with custom networking plugins which implement the same API for
   241  expose and port forwarding.
   242  
   243  ### Networking Protocols
   244  
   245  The Docker driver configures ports on both the `tcp` and `udp` protocols.
   246  
   247  This is not configurable.
   248  
   249  ### Other Networking Modes
   250  
   251  Some networking modes like `container` or `none` will require coordination
   252  outside of Nomad. First-class support for these options may be improved later
   253  through Nomad plugins or dynamic job configuration.
   254  
   255  ## Host Requirements
   256  
   257  Nomad requires Docker to be installed and running on the host alongside the
   258  Nomad agent. Nomad was developed against Docker `1.8.2` and `1.9`.
   259  
   260  By default Nomad communicates with the Docker daemon using the daemon's unix
   261  socket. Nomad will need to be able to read/write to this socket. If you do not
   262  run Nomad as root, make sure you add the Nomad user to the Docker group so
   263  Nomad can communicate with the Docker daemon.
   264  
   265  For example, on Ubuntu you can use the `usermod` command to add the `vagrant`
   266  user to the `docker` group so you can run Nomad without root:
   267  
   268      sudo usermod -G docker -a vagrant
   269  
   270  For the best performance and security features you should use recent versions
   271  of the Linux Kernel and Docker daemon.
   272  
   273  ## Agent Configuration
   274  
   275  The `docker` driver has the following [client configuration
   276  options](/docs/agent/config.html#options):
   277  
   278  * `docker.endpoint` - Defaults to `unix:///var/run/docker.sock`. You will need
   279    to customize this if you use a non-standard socket (http or another
   280    location).
   281  
   282  * `docker.auth.config` - Allows an operator to specify a json file which is in
   283    the dockercfg format containing authentication information for private registry.
   284  
   285  * `docker.tls.cert` - Path to the server's certificate file (`.pem`). Specify
   286    this along with `docker.tls.key` and `docker.tls.ca` to use a TLS client to
   287    connect to the docker daemon. `docker.endpoint` must also be specified or
   288    this setting will be ignored.
   289  
   290  * `docker.tls.key` - Path to the client's private key (`.pem`). Specify this
   291    along with `docker.tls.cert` and `docker.tls.ca` to use a TLS client to
   292    connect to the docker daemon. `docker.endpoint` must also be specified or
   293    this setting will be ignored.
   294  
   295  * `docker.tls.ca` - Path to the server's CA file (`.pem`). Specify this along
   296    with `docker.tls.cert` and `docker.tls.key` to use a TLS client to connect to
   297    the docker daemon. `docker.endpoint` must also be specified or this setting
   298    will be ignored.
   299  
   300  * `docker.cleanup.image` Defaults to `true`. Changing this to `false` will
   301    prevent Nomad from removing images from stopped tasks.
   302  
   303  * `docker.volumes.selinuxlabel`: Allows the operator to set a SELinux
   304    label to the allocation and task local bind-mounts to containers.
   305  
   306  * `docker.privileged.enabled` Defaults to `false`. Changing this to `true` will
   307    allow containers to use `privileged` mode, which gives the containers full
   308    access to the host's devices. Note that you must set a similar setting on the
   309    Docker daemon for this to work.
   310  
   311  Note: When testing or using the `-dev` flag you can use `DOCKER_HOST`,
   312  `DOCKER_TLS_VERIFY`, and `DOCKER_CERT_PATH` to customize Nomad's behavior. If
   313  `docker.endpoint` is set Nomad will **only** read client configuration from the
   314  config file.
   315  
   316  An example is given below: 
   317  
   318  ```
   319      client {
   320          options = {
   321              "docker.cleanup.image" = "false"
   322          }
   323      }
   324  ```
   325  
   326  ## Agent Attributes
   327  
   328  The `docker` driver will set the following client attributes:
   329  
   330  * `driver.docker` - This will be set to "1", indicating the driver is
   331    available.
   332  * `driver.docker.version` - This will be set to version of the docker server
   333  
   334  ## Resource Isolation
   335  
   336  ### CPU
   337  
   338  Nomad limits containers' CPU based on CPU shares. CPU shares allow containers
   339  to burst past their CPU limits. CPU limits will only be imposed when there is
   340  contention for resources. When the host is under load your process may be
   341  throttled to stabilize QOS depending on how many shares it has. You can see how
   342  many CPU shares are available to your process by reading `NOMAD_CPU_LIMIT`.
   343  1000 shares are approximately equal to 1Ghz.
   344  
   345  Please keep the implications of CPU shares in mind when you load test workloads
   346  on Nomad.
   347  
   348  ### Memory
   349  
   350  Nomad limits containers' memory usage based on total virtual memory. This means
   351  that containers scheduled by Nomad cannot use swap. This is to ensure that a
   352  swappy process does not degrade performance for other workloads on the same
   353  host.
   354  
   355  Since memory is not an elastic resource, you will need to make sure your
   356  container does not exceed the amount of memory allocated to it, or it will be
   357  terminated or crash when it tries to malloc. A process can inspect its memory
   358  limit by reading `NOMAD_MEMORY_LIMIT`, but will need to track its own memory
   359  usage. Memory limit is expressed in megabytes so 1024 = 1Gb.
   360  
   361  ### IO
   362  
   363  Nomad's Docker integration does not currently provide QOS around network or
   364  filesystem IO. These will be added in a later release.
   365  
   366  ### Security
   367  
   368  Docker provides resource isolation by way of
   369  [cgroups and namespaces](https://docs.docker.com/introduction/understanding-docker/#the-underlying-technology).
   370  Containers essentially have a virtual file system all to themselves. If you
   371  need a higher degree of isolation between processes for security or other
   372  reasons, it is recommended to use full virtualization like
   373  [QEMU](/docs/drivers/qemu.html).