2015-09-20 22:31:33 +00:00
|
|
|
---
|
|
|
|
layout: "docs"
|
|
|
|
page_title: "Drivers: Docker"
|
2015-09-25 02:35:52 +00:00
|
|
|
sidebar_current: "docs-drivers-docker"
|
2015-09-20 22:31:33 +00:00
|
|
|
description: |-
|
|
|
|
The Docker task driver is used to run Docker based tasks.
|
|
|
|
---
|
|
|
|
|
|
|
|
# Docker Driver
|
|
|
|
|
2015-09-25 02:35:52 +00:00
|
|
|
Name: `docker`
|
2015-09-20 22:31:33 +00:00
|
|
|
|
2015-09-25 02:35:52 +00:00
|
|
|
The `docker` driver provides a first-class Docker workflow on Nomad. The Docker
|
2015-09-25 02:33:06 +00:00
|
|
|
driver handles downloading containers, mapping ports, and starting, watching,
|
2015-09-29 21:00:48 +00:00
|
|
|
and cleaning up after containers.
|
2015-09-20 22:31:33 +00:00
|
|
|
|
2015-09-25 02:33:06 +00:00
|
|
|
## Task Configuration
|
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
The `docker` driver is configured via a `config` block:
|
|
|
|
|
|
|
|
```
|
|
|
|
task "webservice" {
|
|
|
|
driver = "docker"
|
|
|
|
config = {
|
|
|
|
image = "redis"
|
2015-11-19 00:39:02 +00:00
|
|
|
labels = {
|
|
|
|
group = "webservice-cache"
|
|
|
|
}
|
2015-11-19 00:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The following options are available for use in the job specification.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
* `image` - The Docker image to run. The image may include a tag or custom URL.
|
|
|
|
By default it will be fetched from Docker Hub.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-09-26 06:28:23 +00:00
|
|
|
* `command` - (Optional) The command to run when starting the container.
|
|
|
|
|
2016-01-11 19:12:09 +00:00
|
|
|
* `args` - (Optional) A list of arguments to the optional `command`. If no
|
|
|
|
`command` is present, `args` are ignored. References to environment variables
|
|
|
|
or any [intepretable Nomad
|
|
|
|
variables](/docs/jobspec/index.html#interpreted_vars) will be interpreted
|
|
|
|
before launching the task. For example:
|
|
|
|
|
|
|
|
```
|
2016-02-05 00:50:20 +00:00
|
|
|
args = ["${nomad.ip}", "${MY_ENV}", "${meta.foo}"]
|
2016-01-11 19:12:09 +00:00
|
|
|
```
|
2015-10-16 00:28:25 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
* `labels` - (Optional) A key/value map of labels to set to the containers on
|
|
|
|
start.
|
|
|
|
|
|
|
|
* `privileged` - (Optional) `true` or `false` (default). Privileged mode gives
|
|
|
|
the container access to devices on the host. Note that this also requires the
|
|
|
|
nomad agent and docker daemon to be configured to allow privileged
|
|
|
|
containers.
|
|
|
|
|
2016-01-10 21:19:33 +00:00
|
|
|
* `ipc_mode` - (Optional) The IPC mode to be used for the container. The default
|
|
|
|
is `none` for a private IPC namespace. Other values are `host` for sharing
|
|
|
|
the host IPC namespace or the name or id of an existing container. Note that
|
|
|
|
it is not possible to refer to Nomad started Docker containers since their
|
|
|
|
names are not known in advance. Note that setting this option also requires the
|
|
|
|
Nomad agent to be configured to allow privileged containers.
|
|
|
|
|
|
|
|
* `pid_mode` - (Optional) `host` or not set (default). Set to `host` to share
|
|
|
|
the PID namespace with the host. Note that this also requires the Nomad agent
|
|
|
|
to be configured to allow privileged containers.
|
|
|
|
|
|
|
|
* `uts_mode` - (Optional) `host` or not set (default). Set to `host` to share
|
|
|
|
the UTS namsepce with the host. Note that this also requires the Nomad agent
|
|
|
|
to be configured to allow privileged containers.
|
2016-01-08 22:34:49 +00:00
|
|
|
|
2015-11-18 01:20:28 +00:00
|
|
|
* `network_mode` - (Optional) The network mode to be used for the container. In
|
|
|
|
order to support userspace networking plugins in Docker 1.9 this accepts any
|
|
|
|
value. The default is `bridge`. Other networking modes may not work without
|
|
|
|
additional configuration on the host (which is outside the scope of Nomad).
|
|
|
|
Valid values pre-docker 1.9 are `default`, `bridge`, `host`, `none`, or
|
2015-11-19 00:30:37 +00:00
|
|
|
`container:name`. See below for more details.
|
2015-10-01 02:15:24 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
* `hostname` - (Optional) The hostname to assign to the container. When
|
|
|
|
launching more than one of a task (using `count`) with this option set, every
|
2015-11-19 01:49:20 +00:00
|
|
|
container the task starts will have the same hostname.
|
|
|
|
|
|
|
|
* `dns_servers` - (Optional) A list of DNS servers for the container to use
|
|
|
|
(e.g. ["8.8.8.8", "8.8.4.4"]). *Docker API v1.10 and above only*
|
2015-11-05 18:47:41 +00:00
|
|
|
|
2015-12-15 21:22:58 +00:00
|
|
|
* `dns_search_domains` - (Optional) A list of DNS search domains for the container
|
2015-11-18 06:05:56 +00:00
|
|
|
to use.
|
2015-11-17 06:44:44 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
* `port_map` - (Optional) A key/value map of port labels (see below).
|
2015-11-18 00:02:42 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
* `auth` - (Optional) Provide authentication for a private registry (see below).
|
|
|
|
|
2015-11-19 00:39:02 +00:00
|
|
|
### Container Name
|
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
Nomad creates a container after pulling an image. Containers are named
|
|
|
|
`{taskName}-{allocId}`. This is necessary in order to place more than one
|
|
|
|
container from the same task on a host (e.g. with count > 1). This also means
|
|
|
|
that each container's name is unique across the cluster.
|
2015-11-19 00:39:02 +00:00
|
|
|
|
|
|
|
This is not configurable.
|
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
### Authentication
|
2015-11-17 13:14:35 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
If you want to pull from a private repo (for example on dockerhub or quay.io),
|
|
|
|
you will need to specify credentials in your job via the `auth` option.
|
2015-11-18 07:32:57 +00:00
|
|
|
|
2015-11-18 09:37:42 +00:00
|
|
|
The `auth` object supports the following keys:
|
|
|
|
|
|
|
|
* `username` - (Optional) The account username.
|
2015-11-19 00:30:37 +00:00
|
|
|
|
2015-11-18 09:37:42 +00:00
|
|
|
* `password` - (Optional) The account password.
|
2015-11-19 00:30:37 +00:00
|
|
|
|
2015-11-18 09:37:42 +00:00
|
|
|
* `email` - (Optional) The account email.
|
2015-11-19 00:30:37 +00:00
|
|
|
|
2015-11-18 09:37:42 +00:00
|
|
|
* `server_address` - (Optional) The server domain/ip without the protocol.
|
2015-11-19 00:30:37 +00:00
|
|
|
Docker Hub is used by default.
|
|
|
|
|
|
|
|
Example:
|
2015-11-05 18:47:41 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
```
|
|
|
|
task "secretservice" {
|
|
|
|
driver = "docker"
|
|
|
|
config {
|
|
|
|
image = "secret/service"
|
|
|
|
auth {
|
|
|
|
username = "dockerhub_user"
|
2015-11-20 07:11:08 +00:00
|
|
|
password = "dockerhub_password"
|
2015-11-19 00:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
**Please note that these credentials are stored in Nomad in plain text.**
|
|
|
|
Secrets management will be added in a later release.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
## Networking
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
Docker supports a variety of networking configurations, including using host
|
|
|
|
interfaces, SDNs, etc. Nomad uses `bridged` networking by default, like Docker.
|
2015-09-25 23:49:26 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
You can specify other networking options, including custom networking plugins
|
|
|
|
in Docker 1.9. **You may need to perform additional configuration on the host
|
|
|
|
in order to make these work.** This additional configuration is outside the
|
|
|
|
scope of Nomad.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
### Allocating Ports
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
You can allocate ports to your task using the port syntax described on the
|
|
|
|
[networking page](/docs/jobspec/networking.html). Here is a recap:
|
2015-09-25 02:33:06 +00:00
|
|
|
|
|
|
|
```
|
2015-11-19 00:30:37 +00:00
|
|
|
task "webservice" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
port "http" {}
|
|
|
|
port "https" {}
|
|
|
|
}
|
2015-09-25 02:33:06 +00:00
|
|
|
```
|
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
### Forwarding and Exposing Ports
|
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
A Docker container typically specifies which port a service will listen on by
|
|
|
|
specifying the `EXPOSE` directive in the `Dockerfile`.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
Because dynamic ports will not match the ports exposed in your Dockerfile,
|
|
|
|
Nomad will automatically expose all of the ports it allocates to your
|
|
|
|
container.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
These ports will be identified via environment variables. For example:
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
```
|
|
|
|
port "http" {}
|
|
|
|
```
|
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
If Nomad allocates port `23332` to your task for `http`, `23332` will be
|
|
|
|
automatically exposed and forwarded to your container, and the driver will set
|
|
|
|
an environment variable `NOMAD_PORT_http` with the value `23332` that you can
|
|
|
|
read inside your container.
|
2015-11-19 00:30:37 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
This provides an easy way to use the `host` networking option for better
|
|
|
|
performance.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
### Using the Port Map
|
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
If you prefer to use the traditional port-mapping method, you can specify the
|
|
|
|
`port_map` option in your job specification. It looks like this:
|
2015-09-25 02:33:06 +00:00
|
|
|
|
|
|
|
```
|
2015-11-19 00:30:37 +00:00
|
|
|
task "redis" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
port "redis" {}
|
|
|
|
|
|
|
|
config {
|
|
|
|
image = "redis"
|
|
|
|
port_map {
|
2015-11-19 05:03:50 +00:00
|
|
|
redis = 6379
|
2015-11-19 00:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 02:33:06 +00:00
|
|
|
```
|
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
If Nomad allocates port `23332` to your task, the Docker driver will
|
|
|
|
automatically setup the port mapping from `23332` on the host to `6379` in your
|
|
|
|
container, so it will just work!
|
2015-11-19 00:30:37 +00:00
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
Note that by default this only works with `bridged` networking mode. It may
|
|
|
|
also work with custom networking plugins which implement the same API for
|
|
|
|
expose and port forwarding.
|
2015-11-19 00:30:37 +00:00
|
|
|
|
2015-11-19 00:39:02 +00:00
|
|
|
### Networking Protocols
|
|
|
|
|
|
|
|
The Docker driver configures ports on both the `tcp` and `udp` protocols.
|
|
|
|
|
|
|
|
This is not configurable.
|
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
### Other Networking Modes
|
|
|
|
|
2015-11-19 00:45:39 +00:00
|
|
|
Some networking modes like `container` or `none` will require coordination
|
|
|
|
outside of Nomad. First-class support for these options may be improved later
|
|
|
|
through Nomad plugins or dynamic job configuration.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
## Host Requirements
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-18 01:20:28 +00:00
|
|
|
Nomad requires Docker to be installed and running on the host alongside the
|
2015-11-19 00:30:37 +00:00
|
|
|
Nomad agent. Nomad was developed against Docker `1.8.2` and `1.9`.
|
2015-09-25 18:29:38 +00:00
|
|
|
|
2015-11-18 01:20:28 +00:00
|
|
|
By default Nomad communicates with the Docker daemon using the daemon's unix
|
|
|
|
socket. Nomad will need to be able to read/write to this socket. If you do not
|
|
|
|
run Nomad as root, make sure you add the Nomad user to the Docker group so
|
2015-09-25 02:33:06 +00:00
|
|
|
Nomad can communicate with the Docker daemon.
|
|
|
|
|
2015-11-18 01:20:28 +00:00
|
|
|
For example, on ubuntu you can use the `usermod` command to add the `vagrant`
|
|
|
|
user to the `docker` group so you can run Nomad without root:
|
2015-09-25 18:29:38 +00:00
|
|
|
|
|
|
|
sudo usermod -G docker -a vagrant
|
|
|
|
|
2015-11-18 01:20:28 +00:00
|
|
|
For the best performance and security features you should use recent versions
|
|
|
|
of the Linux Kernel and Docker daemon.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
## Agent Configuration
|
2015-09-25 06:40:30 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
The `docker` driver has the following host-level configuration options:
|
2015-09-25 06:40:30 +00:00
|
|
|
|
|
|
|
* `docker.endpoint` - Defaults to `unix:///var/run/docker.sock`. You will need
|
2015-11-18 01:20:28 +00:00
|
|
|
to customize this if you use a non-standard socket (http or another
|
|
|
|
location).
|
2015-09-25 06:40:30 +00:00
|
|
|
|
2016-02-06 13:43:30 +00:00
|
|
|
* `docker.auth_config` - Allows an operator to specify a json file which is in
|
|
|
|
the dockercfg format containing authentication information for private registry.
|
|
|
|
|
2015-11-20 23:47:03 +00:00
|
|
|
* `docker.tls.cert` - Path to the server's certificate file (`.pem`). Specify
|
|
|
|
this along with `docker.tls.key` and `docker.tls.ca` to use a TLS client to
|
|
|
|
connect to the docker daemon. `docker.endpoint` must also be specified or
|
|
|
|
this setting will be ignored.
|
|
|
|
|
|
|
|
* `docker.tls.key` - Path to the client's private key (`.pem`). Specify this
|
|
|
|
along with `docker.tls.cert` and `docker.tls.ca` to use a TLS client to
|
|
|
|
connect to the docker daemon. `docker.endpoint` must also be specified or
|
|
|
|
this setting will be ignored.
|
|
|
|
|
|
|
|
* `docker.tls.ca` - Path to the server's CA file (`.pem`). Specify this along
|
|
|
|
with `docker.tls.cert` and `docker.tls.key` to use a TLS client to connect to
|
|
|
|
the docker daemon. `docker.endpoint` must also be specified or this setting
|
|
|
|
will be ignored.
|
|
|
|
|
2015-10-08 07:08:54 +00:00
|
|
|
* `docker.cleanup.container` Defaults to `true`. Changing this to `false` will
|
|
|
|
prevent Nomad from removing containers from stopped tasks.
|
|
|
|
|
|
|
|
* `docker.cleanup.image` Defaults to `true`. Changing this to `false` will
|
|
|
|
prevent Nomad from removing images from stopped tasks.
|
|
|
|
|
2015-11-06 00:40:20 +00:00
|
|
|
* `docker.privileged.enabled` Defaults to `false`. Changing this to `true` will
|
2015-11-19 00:30:37 +00:00
|
|
|
allow containers to use `privileged` mode, which gives the containers full
|
2015-11-19 00:45:39 +00:00
|
|
|
access to the host's devices. Note that you must set a similar setting on the
|
|
|
|
Docker daemon for this to work.
|
2016-01-08 22:34:49 +00:00
|
|
|
`true` will also allow containers to run with ipc_mode, pid_mode and uts_mode
|
|
|
|
set to `host`, which gives access to the hosts ipc, pid and UTS namespaces
|
|
|
|
respectively.
|
2015-11-06 00:40:20 +00:00
|
|
|
|
2015-11-20 23:47:03 +00:00
|
|
|
cert := d.config.Read("docker.tls.cert")
|
|
|
|
key := d.config.Read("docker.tls.key")
|
|
|
|
ca := d.config.Read("docker.tls.ca")
|
|
|
|
|
2015-10-08 07:08:54 +00:00
|
|
|
Note: When testing or using the `-dev` flag you can use `DOCKER_HOST`,
|
2015-11-20 23:47:03 +00:00
|
|
|
`DOCKER_TLS_VERIFY`, and `DOCKER_CERT_PATH` to customize Nomad's behavior. If
|
|
|
|
`docker.endpoint` is set Nomad will **only** read client configuration from the
|
|
|
|
config filie.
|
2015-10-08 07:08:54 +00:00
|
|
|
|
2015-11-19 00:30:37 +00:00
|
|
|
## Agent Attributes
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-09-25 06:40:30 +00:00
|
|
|
The `docker` driver will set the following client attributes:
|
2015-09-25 02:33:06 +00:00
|
|
|
|
2015-11-18 01:20:28 +00:00
|
|
|
* `driver.docker` - This will be set to "1", indicating the driver is
|
|
|
|
available.
|
|
|
|
* `driver.docker.version` - This will be set to version of the docker server
|
2015-09-25 02:33:06 +00:00
|
|
|
|
|
|
|
## Resource Isolation
|
|
|
|
|
|
|
|
### CPU
|
|
|
|
|
2015-11-18 01:20:28 +00:00
|
|
|
Nomad limits containers' CPU based on CPU shares. CPU shares allow containers
|
|
|
|
to burst past their CPU limits. CPU limits will only be imposed when there is
|
2015-09-25 02:33:06 +00:00
|
|
|
contention for resources. When the host is under load your process may be
|
2015-10-17 20:55:55 +00:00
|
|
|
throttled to stabilize QOS depending on how many shares it has. You can see how
|
2015-11-18 01:20:28 +00:00
|
|
|
many CPU shares are available to your process by reading `NOMAD_CPU_LIMIT`.
|
|
|
|
1000 shares are approximately equal to 1Ghz.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
|
|
|
Please keep the implications of CPU shares in mind when you load test workloads
|
|
|
|
on Nomad.
|
|
|
|
|
|
|
|
### Memory
|
|
|
|
|
|
|
|
Nomad limits containers' memory usage based on total virtual memory. This means
|
|
|
|
that containers scheduled by Nomad cannot use swap. This is to ensure that a
|
2015-11-18 01:20:28 +00:00
|
|
|
swappy process does not degrade performance for other workloads on the same
|
|
|
|
host.
|
2015-09-25 02:33:06 +00:00
|
|
|
|
|
|
|
Since memory is not an elastic resource, you will need to make sure your
|
|
|
|
container does not exceed the amount of memory allocated to it, or it will be
|
|
|
|
terminated or crash when it tries to malloc. A process can inspect its memory
|
|
|
|
limit by reading `NOMAD_MEMORY_LIMIT`, but will need to track its own memory
|
|
|
|
usage. Memory limit is expressed in megabytes so 1024 = 1Gb.
|
|
|
|
|
|
|
|
### IO
|
|
|
|
|
|
|
|
Nomad's Docker integration does not currently provide QOS around network or
|
|
|
|
filesystem IO. These will be added in a later release.
|
|
|
|
|
|
|
|
### Security
|
|
|
|
|
|
|
|
Docker provides resource isolation by way of
|
|
|
|
[cgroups and namespaces](https://docs.docker.com/introduction/understanding-docker/#the-underlying-technology).
|
2015-11-18 01:20:28 +00:00
|
|
|
Containers essentially have a virtual file system all to themselves. If you
|
|
|
|
need a higher degree of isolation between processes for security or other
|
|
|
|
reasons, it is recommended to use full virtualization like
|
|
|
|
[QEMU](/docs/drivers/qemu.html).
|