Skip to content

Jobs

A job is how software is deployed on Nomad, it is conceptually similar to a Kubernetes Deployment.

Minimal Job

The below job deploys a Docker image

minimal.nomad.hcl
job "minimal" {
  group "minimal" {
    task "minimal" {
      driver = "docker"
      config {
        image = "redis:alpine"
      }
    }
  }
}

Networking

Expose a port

networking.nomad.hcl
job "redis" {
  group "redis" {
    network {
      port "redis-port" {
        # Port on the host
        static = 6379
        # Port on the container
        to     = 6379
      }
    }

    task "redis" {
      driver = "docker"
      config {
        image = "redis:alpine"
        ports = ["redis-port"]
      }
    }
  }
}

Networking between tasks in the same group

networking.nomad.hcl
job "networking" {
  group "networking" {
    network {
      port "redis" {
        to = 6379
      }
    } 

    task "redis" {
      driver = "docker"   
      config {
        image = "redis:alpine"
        ports =["redis"]
      }
    }

    task "app" {
      driver = "docker"
      config {
        image = "ghcr.io/paperless-ngx/paperless-ngx:latest"
      }
      env {
        # Contains both the host and port
        PAPERLESS_REDIS = "redis://${NOMAD_ADDR_redis}"
      }
    }
  }
}

Configuration

Application arguments

args.nomad
job "args" {
  group "args" {
    task "args" {
      driver = "docker"
      config {
        image = "my-docker-image:latest"

        args = [
          "-listen",
          "4000"
        ]
      }
    }
  }
}

Custom container entrypoint

Sometimes its useful to set a custom entrypoint for a container, for example to get a shell when trying to debug why a container is crashing.

entrypoint.nomad.hcl
job "entrypoint" {
  group "entrypoint" {
    task "entrypoint" {
      driver = "docker"
      config {
        image = "my-docker-image:latest"
        entrypoint = ["/bin/sh", "-c", "while true; do sleep 500; done"]
      }
    }
  }
}

Static environment variables

variables.nomad.hcl
job "variables" {
  group "variables" {
    task "variables" {
      driver = "docker"
      config {
        image = "my-docker-image:latest"
      }

      env {
        DB_USER = "test"
      }
    }
  }
}

Loading environment variables from .env

.env
MY_VARIABLE=test
variables.nomad.hcl
job "variables" {
  group "variables" {
    task "variables" {
      driver = "docker"
      config {
        image = "my-docker-image:latest"
      }

      template {
        data        = file("./.env")
        destination = "secrets/file.env"
        env         = true
      }
    }
  }
}

Input Variables

Input variables allow customizing a job without having to alter the job's source code.

variables.nomad.hcl
variable "my_variable" {
  type = string
  default = "my-default-value"
}

job "variables" {
  group "variables" {
    task "variables" {
      driver = "docker"
      config {
        image = var.my_variable
      }
    }
  }
}

The variable can be set by using the -var flag when running the job.

nomad job run -var="my_variable=redis" variables.nomad.hcl

For more examples see Variables and Values.

Volumes

Mounting static file

config.nomad.hcl
job "config" {
  group "config" {
    task "config" {
      driver = "docker"
      config {
        image = "my-docker-image:latest"

        volumes = [
          "local/config.yaml:/etc/my-app/config.yaml"
        ]
      }

      template {
        data        = file("./config.yaml")
        destination = "local/config.yaml"
      }
    }
  }
}

Mounting relative directory

This is only really useful for local development environments.

volumes.nomad.hcl
locals {
  # This path is relative from where you run the command
  path = abspath("./data")
}

job "volumes" {
  group "volumes" {
    task "volumes" {
      driver = "docker"
      volumes {
        image = "my-docker-image:latest"

        volumes = [
          "${local.path}:/usr/src/my-app/data",
        ]
      }
    }
  }
}

Last update: August 12, 2023
Created: June 3, 2023