dockerdocker-composemicroservicesmoleculer

Statically configure a microservice to run on a specific machine


I created a 4 micro-services using the Moleculer framework with docker-compose. How do I statically configure each micro-service to run on a specific machine.


Solution

  • You may want to use docker swarm which has a feature allows you to deploy a container on a specific node which called Constraints

    Node: A docker node refers to a member in a swarm mode cluster. Every swarm node must be a docker host, Source: What is the difference between docker host and node?

    Constraints can be treated as node tags, They are key/value pairs associated to particular node.

    Each node by default has the following constraints:

    A service can be deployed as the following:

    docker service create --name backendapp --constraint 'node.hostname == web.example.com'
    

    Note that you can deploy to swarm using docker-compose.yml:

    The deploy command supports compose file version 3.0 and above.

    docker stack deploy --compose-file docker-compose.yml mystack
    

    Also you can set constraints in docker-compose similar to the following example:

    version: '3.3'
    services:
      web:
        image: backendapp-image
        deploy:
          placement:
            constraints:
              - node.hostname == web.example.com
    

    You can get start with docker swarm through here