How to expose Nomad HTTP API within a Docker task without using host networking

Hello. Is there a way for a task (Docker driver) to communicate with the Nomad HTTP API on host port 4646 without having to use host networking? Currently with this job file I can communicate with the Nomad HTTP API via localhost:4646 but I’d like to avoid having to use network_mode = "host".

job "test" {
  datacenters = ["dc1"]

  group "test" {
    count = 1

    task "test" {
      driver = "docker"

      config {
        image = "random/image"
        network_mode = "host"
      }
    }
  }
}

If you’re using bridged network, then you can try accessing host resources via 172.17.0.1 (default).

You can set a dedicated env variable pointing to the nomad agent interface:

job "test" {
  datacenters = ["dc1"]

  group "test" {
    count = 1
    network {
      mode = "bridge"
    }
    task "test" {
      driver = "docker"
      env {
          AGENT_ADDRESS = ${attr.unique.network.ip-address}
      }
      config {
              ...
              args = [ "use" , "$AGENT_ADDRESS:4646", "in", "some", "way" ]
      }
    }
  }
}

I’m not sure which default variables are exposed but you can check them via nomad exec -t <allocation-id> env.
This should work as long as you take some sort of linux as base image.

Another option could be to register the nomad-ui in consul and use the default service discovery options for consul services.

Thank you, I used this method and it worked!

Is there also a way to do this with Consul Connect? I have Nomad registered as a service within Consul. However, I’m not able to communicate with Nomad still with this config

job "test" {
  datacenters = ["dc1"]

  group "test" {
    service {
      name = "test-service"

      connect {
        sidecar_service {
          proxy {
            upstreams {
              destination_name = "nomad"
              local_bind_port = 4646
            }
          }
        }
      }
    }

    task "test" {
      driver = "docker"

      env {
        # Does not resolve
        NOMAD_ADDR = "http://localhost:4646"
      }

      config {
        image = "random/image"
      }
    }
  }
}