Search code examples
node.jsdockerdocker-composedocker-swarm

Scaling node.js express app using docker swarm


I am trying to scale my express server that runs in a docker swarm. when run my stack and there are 2 replicas of the express server on the same machine everything works fine, but as soon as i add another server to the swarm and my express app runs a container on each server then the issue begins.

The issue that I experience is:

  1. POST REQUEST FROM UI
  2. OPTIONS preflight is performed - gets 204 status code from my backend
  3. the request hangs and the POST is not being sent.

What I have tried:

  1. tried to see if it is something that has to do with CORS, but i don't think so because i have managed to make it work on one server which is also in a different origin from the frontend.

  2. tried running the express application only on the worker node (the new node i added to the cluster), and it managed to get the post request to it.

  3. tried running the application on the master node only - it worked.

both servers are behind a load balancer i created (don't know if that is an issue or not when it comes to express) both servers run on EC2 instances on aws base image - node:8-alpine express version : "express": "^4.17.1",


Solution

  • After a while i found out that the issue was that i didn't run swarm on the public IP of both of the ec2 instances.

    make sure that when you initialise swarm that you listen on the public ip of the server.

    docker swarm init --advertise-addr <public ip>

    same thing for joining the swarm.