[Nomad] How to mount the host path to docker task

Recently I came across a problem where I need to share the host path to Nomad docker task. I have to say that the official document about this part does not explain very clear. I spent quite long time to find the solution.

Note that this is not about mounting the host volume to Nomad docker task. Just mount pure folder in host filesystem to Nomad docker task.

It is equivalent to the docker command like

docker run -d \
  -it \
  --name devtest \
  -v "$(pwd)"/target:/app \
  nginx:latest

The most important thing that the Nomad official document did not point out is that the Nomad agent configuration(.hcl) has to add docker plugin first. The below file is Nomad client agent configuration. See line 35-41.

 1# Increase log verbosity
 2log_level = "DEBUG"
 3
 4# Setup data dir
 5data_dir = "/tmp/client1"
 6
 7# Enable the client
 8client {
 9  enabled = true
10
11  # For demo assume you are talking to server1. For production,
12  # this should be like "nomad.service.consul:4647" and a system
13  # like Consul used for service discovery.
14  servers = ["127.0.0.1:4647"]
15}
16
17# Modify our port to avoid a collision with server1
18ports {
19  http = 5656
20}
21
22# Require TLS
23tls {
24  http = true
25  rpc  = true
26
27  ca_file   = "nomad-ca.pem"
28  cert_file = "client.pem"
29  key_file  = "client-key.pem"
30
31  verify_server_hostname = true
32  verify_https_client    = true
33}
34
35plugin "docker" {
36  config {
37    volumes {
38      enabled = true
39    }
40  }
41}

After running Nomad client agent with the updated .hcl file. Next we need to update the job spec.

 1# TARGET_PATH will be passed in by executing the "nomad job run -var TARGET_PATH=${pwd}/target xxx.hcl"
 2variable TARGET_PATH {}
 3
 4job "nginx" {
 5
 6  datacenters = [var.DC]
 7
 8  group "nginx" {
 9
10    task "nginx" {
11      driver = "docker"
12
13      config {
14        image = "nginx:latest"
15        force_pull = true
16        
17        mount {
18          type = "bind"
19          target = "/app"
20          source = var.TARGET_PATH
21          readonly = false
22          bind_options {
23            propagation = "rshared"
24          }
25        }
26      }
27    }
28  }
29}

With the above configuration, the host path will be mount to Nomad docker task successfully.

You can find the official document about volume_mount.


If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !