4

I am trying to run docker containers via nomad. This job spec aims to run a docker container with a java docker image. A volumne from a localhost path is supposed to be mounted on to the docker container. This volume will have some java files. The container should compile the java file as specified by the command.

Expectation: Compilation of java program should be done and a class file should be created both in the container and my local host path.

Job spec file:

job "java" {
  datacenters = ["dc1"]
  type = "service"
  
  update {
    max_parallel = 1
    healthy_deadline = "3m"
    progress_deadline = "10m"
    auto_revert = false
    canary = 0
  }

  migrate {
    max_parallel = 1
    health_check = "checks"
    min_healthy_time = "10s"
    healthy_deadline = "5m"
  }

  group "compile" {
    count = 1
    
    service {
      name = "java-compile"
      tags = ["java", "compile"]
    }
    
    restart {
      attempts = 2
      interval = "30m"
      delay = "15s"
      mode = "fail"
    }

    ephemeral_disk {
      size = 300
    }

    task "java" {
      driver = "docker"

      config {
        image = "adoptopenjdk/openjdk11"
        mount {
          type = "bind"
          target = "/java"
          source = "/home/ninan/nomad/java"
          readonly = false
          bind_options {
            propagation = "rshared"
          }
        }
        work_dir = "/java"
        command = "javac Hello.java"
      }


      resources {
        cpu    = 500 # 500 MHz
        memory = 1024 # 256MB
      }
    }
  }
}

Currently it fails saying volumes are not enabled; cannot mount host path: "/home/ninan/nomad/java"

Attaching the relevant portion of the log:

    2021-08-18T19:09:54.818+0530 [INFO]  client.alloc_runner.task_runner.task_hook.logmon.nomad: opening fifo: alloc_id=f2774019-4135-28f8-9a91-24f6a7c0dbf8 task=java @module=logmon path=/tmp/NomadClient304008624/f2774019-4135-28f8-9a91-24f6a7c0dbf8/alloc/logs/.java.stdout.fifo timestamp=2021-08-18T19:09:54.818+0530
    2021-08-18T19:09:54.819+0530 [INFO]  client.alloc_runner.task_runner.task_hook.logmon.nomad: opening fifo: alloc_id=f2774019-4135-28f8-9a91-24f6a7c0dbf8 task=java @module=logmon path=/tmp/NomadClient304008624/f2774019-4135-28f8-9a91-24f6a7c0dbf8/alloc/logs/.java.stderr.fifo timestamp=2021-08-18T19:09:54.819+0530
    2021-08-18T19:09:55.310+0530 [WARN]  client.alloc_runner.runner_hook.alloc_health_watcher: error looking up Consul registrations for allocation: alloc_id=f2774019-4135-28f8-9a91-24f6a7c0dbf8 error="failed to retrieve services from consul: Get "http://127.0.0.1:8500/v1/agent/services": dial tcp 127.0.0.1:8500: connect: connection refused" alloc_id=f2774019-4135-28f8-9a91-24f6a7c0dbf8
    2021-08-18T19:10:08.094+0530 [ERROR] client.driver_mgr.docker: failed to create container configuration: driver=docker image_name=adoptopenjdk/openjdk11 image_id=sha256:af4af705169ddd5b5c49b64634bc7a09ac000f90ba8e5e49dd9d7cb61c5d2bb8 error="volumes are not enabled; cannot mount host path: "/home/ninan/nomad/java" "/tmp/NomadClient304008624/f2774019-4135-28f8-9a91-24f6a7c0dbf8""
    2021-08-18T19:10:08.094+0530 [ERROR] client.alloc_runner.task_runner: running driver failed: alloc_id=f2774019-4135-28f8-9a91-24f6a7c0dbf8 task=java error="Failed to create container configuration for image "adoptopenjdk/openjdk11" ("sha256:af4af705169ddd5b5c49b64634bc7a09ac000f90ba8e5e49dd9d7cb61c5d2bb8"): volumes are not enabled; cannot mount host path: "/home/ninan/nomad/java" "/tmp/NomadClient304008624/f2774019-4135-28f8-9a91-24f6a7c0dbf8""
    2021-08-18T19:10:08.095+0530 [INFO]  client.alloc_runner.task_runner: not restarting task: alloc_id=f2774019-4135-28f8-9a91-24f6a7c0dbf8 task=java reason="Error was unrecoverable"
    2021-08-18T19:10:08.095+0530 [INFO]  client.gc: marking allocation for GC: alloc_id=f2774019-4135-28f8-9a91-24f6a7c0dbf8

Nomad server is started as -

sudo nomad agent -dev -bind 0.0.0.0 -log-level INFO 

What do I do to enable volume mounting ?

leoOrion
  • 1,833
  • 2
  • 26
  • 52

1 Answers1

5

You should first create the volume path before nomad is starting, then add the following configs in your client.hcl file:

host_volume "database-data" {
    path = "/srv/live/postgres-database/data/"
    read_only = false
}

and then in your jobspecs, in the group stanza define the volume:

volume "database-data" {
     type      = "host"
     read_only = false
     source    = "database-data"
}

and then finally add following in the task stanza use the defined volume:

volume_mount {
      volume      = "database-data"
      destination = "/var/lib/postgresql/data" #<-- in the container
      read_only   = false
}

for more detailed info you can use https://www.nomadproject.io/docs/job-specification/volume

I hope I helped

A Khalili
  • 192
  • 2
  • 10