title: Communication between containers in docker swarm
body: <p>I would like to communicate between master and worker nodes via WebSocket connection in docker swarm mode.</p>
  
  <p>A master node should have been reached from the worker node. 
  The connection fails.</p>
  
  <p>Also, I would like to connect via http to the master node from my host machine. The connection fails as well.</p>
  
  <p>Here is my <strong>docker-compose.yml</strong></p>
  
  <pre><code>version: '3'
  services:
    master:
      image: master
      build:
        context: .
        dockerfile: ./docker/master/Dockerfile
      env_file:
        - ./config.env
      command: ['node', './src/master/']
      ports:
        - 8080:8080
      networks:
        - webnet
      deploy:
        replicas: 1
        resources:
          limits:
            cpus: "0.2"
            memory: 200M
        restart_policy:
          condition: none
  
    worker:
      image: worker
      build:
        context: .
        dockerfile: ./docker/worker/Dockerfile
      env_file:
        - ./config.env
      command: ['node', './src/worker/']
      deploy:
        replicas: 10
        resources:
          limits:
            cpus: "0.1"
            memory: 100M
        restart_policy:
          condition: none
      networks:
        - webnet
      depends_on:
        - master
  
  networks:
    webnet:
  </code></pre>
  
  <p>I create the swarm with:</p>
  
  <pre class="lang-sh prettyprint-override"><code>docker swarm init
  docker-compose build --no-cache
  docker stack deploy -c docker-compose.yml ${stack_name}
  </code></pre>
  
  <p>I try to reach the master node with:</p>
  
  <pre class="lang-sh prettyprint-override"><code>curl http://localhost:8080/result
  </code></pre>
  
  <p>and get</p>
  
  <pre class="lang-sh prettyprint-override"><code>curl: (7) Failed to connect to localhost port 8080: Connection refused
  </code></pre>
  
  Some more details about <strong>master</strong> node:
  
  <p><em>Env:</em></p>
  
  <pre><code>MASTER_PORT=3000
  MASTER_HOST=localhost
  MASTER_HTTP_PORT=8080
  </code></pre>
  
  <p><em>Code starting websocket server:</em></p>
  
  <pre><code>const wss = new WebSocket.Server({ host: config.masterHost, port: config.masterPort }, (err) =&gt; {
    if (err != null) {
      throw err
    }
    console.log(`Web Socket server started on address ws://${config.masterHost}:${config.masterPort}`)
  });
  </code></pre>
  
  <p><em>Code starting http server:</em></p>
  
  <pre><code>server.listen(config.masterHttpPort, config.masterHost, (err) =&gt; {
      if (err != null) {
        throw err
      }
      console.log(`Http server started on address http://${config.masterHost}:${config.masterHttpPort}`);
    });
  </code></pre>
  
  Some more details about <strong>worker</strong> node:
  
  <p><em>Env</em></p>
  
  <pre><code>MASTER_SERVICE_HOST=master
  MASTER_PORT=3000
  </code></pre>
  
  <p><em>Code connecting websocket client to master node:</em></p>
  
  <pre><code>const masterUri = `ws://${config.masterServiceHost}:${config.masterPort}`;
  console.log(`Connecting to ${masterUri}`);
  const ws = new WebSocket(masterUri, {perMessageDeflate: false});
  </code></pre>
  
  <h3>I'm really confused because when I'm  running the app with docker-compose (not in swarm mode) everything works perfectly.</h3>
  
  <p>But in swarm mode, I neither could reach</p>
  
  <p><strong>the master node from host machine via http</strong></p>
  
  <p>nor could I reach</p>
  
  <p><strong>the master node from worker node via websocket connection</strong></p>
  
  <p>I suspect me configuring the docker networking incorrectly.</p>
  
  <p>Logs from <strong>master</strong> node:</p>
  
  <pre><code>Process started with config: {
    "grouperFile": "./src/operations/group.js",
    "initialDelay": 10,
    "mapperFile": "./src/operations/map.js",
    "masterHost": "localhost",
    "masterHttpPort": 8080,
    "masterPort": 3000,
    "masterServiceHost": "master",
    "slaveReplicationFactor": 1
  }
  Web Socket server started on address ws://localhost:3000
  Http server started on address http://localhost:8080
  </code></pre>
  
  <p>Also, some logs from <strong>worker</strong> node:</p>
  
  <pre><code>Process started with config: {
    "grouperFile": "./src/operations/group.js",
    "initialDelay": 10,
    "mapperFile": "./src/operations/map.js",
    "masterHost": "master",
    "masterHttpPort": 8080,
    "masterPort": 3000,
    "slaveReplicationFactor": 1
  }
  Connecting to ws://master:3000
  events.js:174
        throw er; // Unhandled 'error' event
        ^
  
  Error: getaddrinfo EAI_AGAIN master master:3000
      at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
  Emitted 'error' event at:
      at ClientRequest.req.on (/app/node_modules/ws/lib/websocket.js:554:10)
      at ClientRequest.emit (events.js:189:13)
      at Socket.socketErrorListener (_http_client.js:392:9)
      at Socket.emit (events.js:189:13)
      at emitErrorNT (internal/streams/destroy.js:82:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
      at process._tickCallback (internal/process/next_tick.js:63:19)
  </code></pre>