Search code examples
dockerterraformdocker-swarm

Terraform does not create docker service on worker node


We have a requirement to setup multiple jenkins instances on limited number of host machines.

To achieve this, I've setup Docker Swarm on two servers.

On the "master" node, I use terraform to create and manage docker services.

When I create the services manually, the container automatically gets spawned on worker node. When I create a service with 2 replicas, the containers are created on both nodes as they should be.

However when I use terraform code to create and manage the service, the containers are never created on worker node. Even when I setup 2 replicas, both are created on the master node.

The main.tf file used to create the service is provided here.

resource "docker_service" "jenkins_service" {
  name = var.project_name
  task_spec {
    container_spec {
      image = docker_image.jenkins_image.name
      mounts {
        target = "/var/jenkins_name"
        source = docker_volume.jenkins_volume.name
        type   = "volume"
      }
      mounts {
        source = "/var/run/docker.sock"
        target = "/var/run/docker.sock"
        type   = "bind"
      }
    }
    networks = ["${docker_network.jenkins_network.name}"]
  }

  endpoint_spec {
    ports {
      target_port    = "8080"
      published_port = var.web_interface_port
      publish_mode   = "ingress"
      name           = "WEB_INTERFACE"
    }
    ports {
      target_port    = "50000"
      published_port = var.api_interface_port
      publish_mode   = "ingress"
      name           = "API_INTERFACE"
    }
  }
}

I don't know what I am doing wrong here. Any help will be much appreciated

Regards.


Solution

  • For the benefit of others: I was using custom docker images and the terraform code was essentially calling these images using the format, : thereby forcing the swarm to start containers only on the master node which actually had the image.

    I came across this excellent post which points to the exact problem. My setup was missing a docker registry. I followed the steps in that link and that resolved the problem.

    Thanks, "fernandezcuesta" for taking time to respond to my query.

    Regards Senthil Nathan M