--- layout: "docs" page_title: "Drivers: Docker" sidebar_current: "docs-drivers-docker" description: |- The Docker task driver is used to run Docker based tasks. --- # Docker Driver Name: `docker` The `docker` driver provides a first-class Docker workflow on Nomad. The Docker driver handles downloading containers, mapping ports, and starting, watching, and cleaning up after containers. ## Task Configuration ```hcl task "webservice" { driver = "docker" config { image = "redis:3.2" labels { group = "webservice-cache" } } } ``` The `docker` driver supports the following configuration in the job spec. Only `image` is required. * `image` - The Docker image to run. The image may include a tag or custom URL and should include `https://` if required. By default it will be fetched from Docker Hub. If the tag is omitted or equal to `latest` the driver will always try to pull the image. If the image to be pulled exists in a registry that requires authentication credentials must be provided to Nomad. Please see the [Authentication section](#authentication). ```hcl config { image = "https://hub.docker.internal/redis:3.2" } ``` * `args` - (Optional) A list of arguments to the optional `command`. If no `command` is specified, the args are passed directly to the container. References to environment variables or any [interpretable Nomad variables](/docs/runtime/interpolation.html) will be interpreted before launching the task. For example: ```hcl config { args = [ "-bind", "${NOMAD_PORT_http}", "${nomad.datacenter}", "${MY_ENV}", "${meta.foo}", ] } ``` * `auth` - (Optional) Provide authentication for a private registry (see below). * `command` - (Optional) The command to run when starting the container. ```hcl config { command = "my-command" } ``` * `dns_search_domains` - (Optional) A list of DNS search domains for the container to use. * `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* * `extra_hosts` - (Optional) A list of hosts, given as host:IP, to be added to `/etc/hosts`. * `force_pull` - (Optional) `true` or `false` (default). Always pull latest image instead of using existing local image. Should be set to `true` if repository tags are mutable. * `hostname` - (Optional) The hostname to assign to the container. When launching more than one of a task (using `count`) with this option set, every container the task starts will have the same hostname. * `interactive` - (Optional) `true` or `false` (default). Keep STDIN open on the container. * `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 Docker containers started by Nomad 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. * `ipv4_address` - (Optional) The IPv4 address to be used for the container when using user defined networks. Requires docker 1.13.0 or greater. * `ipv6_address` - (Optional) The IPv6 address to be used for the container when using user defined networks. Requires docker 1.13.0 or greater. * `labels` - (Optional) A key-value map of labels to set to the containers on start. ```hcl config { labels { foo = "bar" zip = "zap" } } ``` * `load` - (Optional) A list of paths to image archive files. If this key is not specified, Nomad assumes the `image` is hosted on a repository and attempts to pull the image. The `artifact` blocks can be specified to download each of the archive files. The equivalent of `docker load -i path` would be run on each of the archive files. ```hcl artifact { source = "http://path.to/redis.tar" } config { load = ["redis.tar"] image = "redis" } ``` * `logging` - (Optional) A key-value map of Docker logging options. The default value is `syslog`. ```hcl config { logging { type = "fluentd" config { fluentd-address = "localhost:24224" tag = "your_tag" } } } ``` * `network_aliases` - (Optional) A list of network-scoped aliases, provide a way for a container to be discovered by an alternate name by any other container within the scope of a particular network. Network-scoped alias is supported only for containers in user defined networks ```hcl config { network_mode = "user-network" network_aliases = [ "${NOMAD_TASK_NAME}", "${NOMAD_TASK_NAME}-${NOMAD_ALLOC_INDEX}" ] } ``` * `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` for all operating systems but Windows, which defaults to `nat`. 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 `container:name`. * `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. See below for more details. * `port_map` - (Optional) A key-value map of port labels (see below). * `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. * `security_opt` - (Optional) A list of string flags to pass directly to [`--security-opt`](https://docs.docker.com/engine/reference/run/#security-configuration). For example: ```hcl config { security_opt = [ "credentialspec=file://gmsaUser.json", ] } ``` * `shm_size` - (Optional) The size (bytes) of /dev/shm for the container. * `SSL` - (Optional) If this is set to true, Nomad uses SSL to talk to the repository. The default value is `true`. **Deprecated as of 0.5.3** * `tty` - (Optional) `true` or `false` (default). Allocate a pseudo-TTY for the container. * `uts_mode` - (Optional) `host` or not set (default). Set to `host` to share the UTS namespace with the host. Note that this also requires the Nomad agent to be configured to allow privileged containers. * `userns_mode` - (Optional) `host` or not set (default). Set to `host` to use the host's user namespace when user namespace remapping is enabled on the docker daemon. * `volumes` - (Optional) A list of `host_path:container_path` strings to bind host paths to container paths. Mounting host paths outside of the allocation directory can be disabled on clients by setting the `docker.volumes.enabled` option set to false. This will limit volumes to directories that exist inside the allocation directory. ```hcl config { volumes = [ # Use absolute paths to mount arbitrary paths on the host "/path/on/host:/path/in/container", # Use relative paths to rebind paths already in the allocation dir "relative/to/task:/also/in/container" ] } ``` * `volume_driver` - (Optional) The name of the volume driver used to mount volumes. Must be used along with `volumes`. Using a `volume_driver` also allows to use `volumes` with a named volume as well as absolute paths. If `docker.volumes.enabled` is false then volume drivers are disallowed. ```hcl config { volumes = [ # Use named volume created outside nomad. "name-of-the-volume:/path/in/container" ] # Name of the Docker Volume Driver used by the container volume_driver = "flocker" } ``` * `work_dir` - (Optional) The working directory inside the container. ### Container Name 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. This is not configurable. ### Authentication 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 in the task config. * by storing explicit repository credentials or by specifying Docker `credHelpers` in a file and setting the [docker.auth.config](#auth_file) value on the client. * by specifying a [docker.auth.helper](#auth_helper) on the client The `auth` object supports the following keys: * `username` - (Optional) The account username. * `password` - (Optional) The account password. * `email` - (Optional) The account email. * `server_address` - (Optional) The server domain/IP without the protocol. Docker Hub is used by default. Example task-config: ```hcl task "example" { driver = "docker" config { image = "secret/service" auth { username = "dockerhub_user" password = "dockerhub_password" } } } ``` Example docker-config, using two helper scripts in $PATH, "docker-credential-ecr" and "docker-credential-vault": ```json { "auths": { "internal.repo": { "auth": "`echo -n ':' | base64 -w0`" } }, "credHelpers": { ".dkr.ecr..amazonaws.com": "ecr-login" }, "credsStore": "secretservice" } ``` !> **Be Careful!** At this time these credentials are stored in Nomad in plain text. Secrets management will be added in a later release. ## Networking Docker supports a variety of networking configurations, including using host interfaces, SDNs, etc. Nomad uses `bridged` networking by default, like Docker. 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. ### Allocating Ports You can allocate ports to your task using the port syntax described on the [networking page](/docs/job-specification/network.html). Here is a recap: ```hcl task "example" { driver = "docker" resources { network { port "http" {} port "https" {} } } } ``` ### Forwarding and Exposing Ports A Docker container typically specifies which port a service will listen on by specifying the `EXPOSE` directive in the `Dockerfile`. 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. These ports will be identified via environment variables. For example: ```hcl port "http" {} ``` 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. This provides an easy way to use the `host` networking option for better performance. ### Using the Port Map 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: ```hcl task "example" { driver = "docker" config { image = "redis" port_map { redis = 6379 } } resources { network { mbits = 20 port "redis" {} } } } ``` 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! 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. ### Using Docker IPs and Ports *New in Nomad 0.6.* Other than `host` and `none`, Docker network modes create an IP for the container. This IP is set when the container is run and exposed as a `NOMAD_DRIVER_IP_