1

I am having trouble establishing communication between two docker containers via nomad. Containers are in the same task group but still unable to reach each other. Even when using NOMAD_ADDR_ environment variable. Can anyone help in this regard? I tried both host and bridge network mode.

My nomad config is given below. Images are pulled and the Redis container and application container starts, but then app container crashes with Redis Connection Refused error

The second issue is, as you might have guessed is of prettifying the code with proper indentation etc. Just like Javascript or HTML or YAML is automatically formatted in VS code. I am unable to find a code prettifier for the HCL language.

job "app-deployment" {
  datacenters = ["dc1"]

  group "app" {
   network {
    mode = "bridge"
    port "web-ui" { to = 5000 }
    port "redis" { to = 6379 }
  }
   service {
        name = "web-ui"
        port = "web-ui"
        // check {
        //   type     = "http"
        //   path     = "/health"
        //   interval = "2s"
        //   timeout  = "2s"
        // }
    } 

    task "myapp" {
      driver = "docker"
      config {
          image_pull_timeout = "10m"

          image = "https://docker.com"
          ports = ["web-ui"]
        }
       env {
            REDIS_URL="redis://${NOMAD_ADDR_redis}"
            // REDIS_URL="redis://$NOMAD_IP_redis:$NOMAD_PORT_redis"
            NODE_ENV="production"
          }
       }
    task "redis" {
    driver = "docker"
    config {
      image = "redis"
      ports = ["redis"]
      }
    }
    
  }
  
}

Zeeshan
  • 21
  • 4
  • Does this answer your question? [how can I connect two docker containers with nomad](https://stackoverflow.com/questions/68358370/how-can-i-connect-two-docker-containers-with-nomad) – OneCricketeer Dec 21 '21 at 17:37

1 Answers1

0

So I was able to resolve it, basically, when you start nomad agent in dev mode, by default it binds to the loopback interface and that is why you get 127.0.0.1 as IP and node port in NOMAD env variables. 127.0.0.1 resolves to localhost inside container and hence it is unable to reach the Redis server.

To fix the issue, simply run

ip a

Identify the primary network interface for me it was my wifi interface. Then start the nomad like below.

nomad agent -dev -network-interface="en0"
# where en0 is the primary network interface

That way u will still be able to access the nomad UI on localhost:4646 but your containers will get the HOST IP from your network rather then 127.0.0.1

Zeeshan
  • 21
  • 4
  • Ideally, you'd install Consul, then use service name discovery to communicate between containers in the entire cluster rather than only on one host – OneCricketeer Dec 21 '21 at 17:34