github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/e2e/volumes/input/volumes.nomad (about)

     1  job "volumes" {
     2    datacenters = ["dc1", "dc2"]
     3  
     4    constraint {
     5      attribute = "${attr.kernel.name}"
     6      value     = "linux"
     7    }
     8  
     9    group "group" {
    10  
    11      volume "data" {
    12        type   = "host"
    13        source = "shared_data"
    14      }
    15  
    16      task "docker_task" {
    17  
    18        driver = "docker"
    19  
    20        config {
    21          image   = "busybox:1"
    22          command = "/bin/sh"
    23          args    = ["/usr/local/bin/myapplication.sh"]
    24  
    25          mounts = [
    26            # this mount binds the task's own NOMAD_TASK_DIR directory as the
    27            # source, letting us map it to a more convenient location; this is a
    28            # frequently-used way to get templates into an arbitrary location in
    29            # the task for Docker
    30            {
    31              type     = "bind"
    32              source   = "local"
    33              target   = "/usr/local/bin"
    34              readonly = true
    35            }
    36          ]
    37        }
    38  
    39        # this is the host volume mount, which we'll write into in our task to
    40        # ensure we have persistent data
    41        volume_mount {
    42          volume      = "data"
    43          destination = "/tmp/foo"
    44        }
    45  
    46        template {
    47          data = <<EOT
    48  #!/bin/sh
    49  echo ${NOMAD_ALLOC_ID} > /tmp/foo/${NOMAD_ALLOC_ID}
    50  sleep 3600
    51  EOT
    52  
    53          # this path is relative to the allocation's task directory:
    54          # /var/nomad/alloc/:alloc_id/:task_name
    55          # but Docker tasks can't see this folder except for the bind-mounted
    56          # directories inside it (./local ./secrets ./tmp)
    57          # so the only reason this works to write our script to execute from
    58          # /usr/local/bin is because of the 'mounts' section above.
    59          destination = "local/myapplication.sh"
    60        }
    61  
    62        resources {
    63          cpu    = 256
    64          memory = 128
    65        }
    66      }
    67  
    68      task "exec_task" {
    69  
    70        driver = "exec"
    71  
    72        config {
    73          command = "/bin/sh"
    74          args    = ["/usr/local/bin/myapplication.sh"]
    75        }
    76  
    77        # host volumes for exec tasks are more limited, so we're only going to read
    78        # data that the other task places there
    79        #
    80        # - we can't write unless the nobody user has permissions to write there
    81        # - we can't template into this location because the host_volume mounts
    82        #   over the template (see https://github.com/hashicorp/nomad/issues/7796)
    83        volume_mount {
    84          volume      = "data"
    85          destination = "/tmp/foo"
    86          read_only   = true
    87        }
    88  
    89        template {
    90          data = <<EOT
    91  #!/bin/sh
    92  sleep 3600
    93  EOT
    94  
    95          # this path is relative to the allocation's task directory:
    96          # /var/nomad/alloc/:alloc_id/:task_name
    97          # which is the same as the root directory for exec tasks.
    98          # we just need to make sure this doesn't collide with the
    99          # chroot: https://www.nomadproject.io/docs/drivers/exec#chroot
   100          destination = "usr/local/bin/myapplication.sh"
   101        }
   102  
   103        resources {
   104          cpu    = 256
   105          memory = 128
   106        }
   107      }
   108    }
   109  }