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