Simple nomad config for a Nginx and php-fpm app

Hello!

I have webapp (php-fpm and nginx) that I am trying to run with Nomad, I need to share a app folder in the php container into the Nginx, one, how can I do that? both the Php and Nginx tasks are in the same Job group.

And the error I am facing is that the Nginx job fails because it is not mounting the files.

Any ideas whats the proper way to archive what I intended?

This is my current config:

job "mywebapp" {
  datacenters = [
    "aws_us_east_2"]
  type = "service"
  update {
    max_parallel = 1
    min_healthy_time = "10s"
    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 "app" {
    count = 1
    restart {
      attempts = 2
      interval = "30m"
      delay = "15s"
      mode = "fail"
    }
    ephemeral_disk {
      size = 300
    }
    task "nginx" {
      driver = "docker"
      config {
        image = "nginx:latest"
        hostname = "nginx.service.consul"
        port_map {
          nginx_port = 8080
        }
        dns_servers = [
          "127.0.0.1:8600"]
        dns_search_domains = [
          "consul"]
        volumes = [
          "app_dir:/var/www/mywebapp",
        ]
      }
      resources {
        cpu = 500
        memory = 256
        network {
          mbits = 10
          port "nginx_port" {
            static = 8080
          }
        }
      }
      service {
        name = "nginx"
        tags = [
          "global",
          "app"]
        port = "nginx_port"

        check {
          name = "alive"
          type = "tcp"
          interval = "10s"
          timeout = "2s"
        }
      }
      template {
        source        = "/home/user/app/nomad/templates/nginx/default.conf"
        destination = "/etc/nginx/conf.d/default.conf"
      }
    }
    task "mywebapp_task" {
      driver = "docker"
      config {
        image = "mywebapp"
        hostname = "mywebapp.service.consul"
        port_map {
          phpfpm = 9000
        }
        dns_servers = ["${attr.unique.network.ip-address}"]
        dns_search_domains = [
          "consul"]
        volumes = [
          "app_dir:/var/www/mywebapp",
        ]
      }
      resources {
        cpu = 1000
        memory = 1024
        network {
          mbits = 10
          port "phpfpm" {}
        }
      }
      service {
        name = "mywebapp"
        tags = [
          "global",
          "app"]
        port = "phpfpm"

        check {
          name = "alive"
          type = "tcp"
          interval = "10s"
          timeout = "2s"
        }
      }
    }
  }
}

1 Like

I know this is old, but I had a similar problem with files not being mapped as expected.

You need to use an “artifact” stanza to retrieve the config file, as you can’t map the file directly from your machine. There’s a good example of how to get this working here:

https://stackoverflow.com/questions/42965004/how-would-you-use-hashicorps-nomad-template-stanza-to-generate-an-nginx-confi?answertab=active#tab-top

Hope that helps someone

1 Like

Hi @jcalonso,

Since you are trying to share a file across tasks you can use the alloc/ dir as a place to store common files.

But as @jstowey mentioned, you won’t be able to load that file from the host machine (the reason being that there’s no way to guarantee that the file will still exist if the allocation gets reschedule). You could have a pre-start task that will render the template in the alloc/ dir for others to consume.

If you do need access to the hosts’ file system, you will need to use a host volume, but then your task will only be able to run in the clients that have the volume registered.