Correct way to connect to upstream that uses dynamic ports

Hi @lens0021 :wave:

I think that what you are missing is setting the address_mode of your service to alloc. By default, Nomad will advertise the host port associated with the allocation (the random port), but for Connect, since everything lives in the same bridge network, you want to advertise the actual port your allocation is listening on.

Iā€™m not sure if you already have it, but you will also need to assign the port to your Docker task.

So the changes that I think you need to make are these:

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

  group "caddy" {
    task "caddy" {
      driver = "docker"

      config {
        image = "caddy"
+       ports = ["http"]
      }
    }

    network {
      mode = "bridge"

      # Below http port is not referenced now.
      port "http" {
        to = 80
      }
    }

    service {
      name         = "caddy"
+     port         = "http"
+     address_mode = "alloc"

      connect {
        sidecar_service {}
      }
    }
  }

Give it a try and let me know how it goes :slightly_smiling_face:

1 Like