Nomad Port Binding

job "mysql-db" {
  datacenters = ["dc1"]
  type = "service"

  group "mysql" {
    count = 2

    network {
      port "db" {
        static = 3306
      }
    }

    volume "mysql_data" {
      type      = "host"
      read_only = false
      source    = "mysql_vol"
    }

    task "mysql" {
      driver = "docker"
      
      env = {
        MYSQL_ROOT_PASSWORD = "password"
        MYSQL_DATABASE      = "db"
        MYSQL_USER          = "user"
        MYSQL_PASSWORD      = "password"
      }

      config {
        image = "mysql:8.0"
        ports = ["db"]

        volumes = [
          "local/mysql_data:/var/lib/mysql"
        ]
      }

      resources {
        cpu    = 500
        memory = 1024
      }
    }
  }
}

If i change the mysql port from 3306 to another port, i can’t reach the mysql server until i change it back to the default 3306. I can’t also use dynamic ports because the container still becomes un-reacheable, how can i bind ports instead. From the default container port to, the a host port ?

Hi, First at all, welcome to our community!

You can use map port:

network {
      port "db" {
          to = 3306
      }
    }

But, the allocate port to advertise will be dynamic! (if you use consul, its not problem for you).

1 Like

Alright thanks, this works !

1 Like