Nomad service address resolves to localhost

I am trying to use the nomad service discovery to connect services in a bridge network the template block output resolves .Address as 127.0.0.1 instead of the container IP.

I have the following configs when trying this out.

job "test" {
  datacenters = ["dc1"]
  type = "service"
        group "test" {
                count = 1

                network {
                        mode = "bridge"
                        port "http" { }
                }


                task "nginx" {
                        driver = "docker"
                        config {
                                image = "nginx"
                                ports = ["http"]
                        }
                  service {
                        name = "test"
                        port = "http"
        provider = "nomad"

                  }
      env {
        NGINX_PORT = "${NOMAD_PORT_http}"
      }
                }
        }
}
job "busybox" {
  datacenters = ["dc1"]
  type = "service"
        group "busybox" {
                count = 1

                network {
                        mode = "bridge"
                }


                task "sleep" {
                        driver = "docker"
      template {
        data = <<EOH

{{$allocID := env "NOMAD_ALLOC_ID" -}}
{{range nomadService 1 $allocID "test"}}
TEST="{{.Address}}:{{.Port}}"
{{ end}}
EOH
        destination = "/tmp/env.local"
      }
                        config {
                                image = "busybox"
                                command = "/bin/sh"
                                args = [
                                        "-c",
                                        "sleep 500"
                                ]
                        }
                }
        }
}

When checking the resulting file it looks like this:

TEST="127.0.0.1:24258"

Hi @Tebro, if you want to use the container IP you need to have a group-level service with address_mode = "alloc" (or "driver" depending on what you want) set. e.g.

job "test" {
  datacenters = ["dc1"]
  type        = "service"
  group "test" {
    count = 1

    network {
      mode = "bridge"
      port "http" {}
    }

    service {
      name         = "test"
      port         = "http"
      address_mode = "alloc"
      provider     = "nomad"
    }

    task "nginx" {
      driver = "docker"
      config {
        image = "nginx"
        ports = ["http"]
      }
      env {
        NGINX_PORT = "${NOMAD_PORT_http}"
      }
    }
  }
}
➜ nomad service info test
Job ID  Address              Tags  Node ID   Alloc ID
test    172.26.65.104:27079  []    f32cdae6  add55a28

And then using a simpler little helper for viewing the template output

job "busybox" {
  type        = "sysbatch"

  group "busybox" {

    task "cat" {
      driver = "docker"
      template {
        data        = <<EOH
{{$allocID := env "NOMAD_ALLOC_ID" -}}
{{range nomadService 1 $allocID "test"}}
TEST="{{.Address}}:{{.Port}}"
{{ end}}
EOH
        destination = "local/output.txt"
      }
      config {
        image   = "busybox"
        command = "/bin/sh"
        args = [
          "-c",
          "cat local/output.txt"
        ]
      }
    }
  }
}
➜ nomad alloc logs c4

TEST="172.26.65.104:27079"