connect two docker containers with socket io












0















I am currently working on dockerizing a tested socket.io app for a simple chat application, using socket.io, and mocha for testing. the server opens up a socket for listening on port 3000 and the test client uses the socket to emit messages or receive emissions.



I am using version 3 of docker compose files.



nodeserver dockerfile:



FROM node:10

WORKDIR /usr/src/appserver

COPY package*.json ./
COPY public public
COPY main.js main.js

RUN npm install
RUN npm install express
RUN npm install socket.io

CMD ["npm", "start"]


test dockerfile:



FROM nodeserver

COPY test test

RUN npm update &&
npm install -g mocha &&
npm install -g socket.io-client

CMD ["npm", "test"]


docker-compose:



version: "3"

services:
nodeserver:
build: .
expose:
- "3000"
image: ws

test:
depends_on:
- nodeserver
links:
- nodeserver
build: ./test
image: test_image


my node server is listening on port 3000, and on connection sends a hi message to all.



let express = require('express');
let app = express();
let http = require('http').createServer(app);
let io = require('socket.io')(http);
http.listen(3000, function ()
{
console.log('listening on *:3000');
});

io.on('connection', function(socket)
{
console.log('a user connected');

io.emit('hi', 'hi');
});


and my mocha test looks like this, which in essence attaches itself as a client and waits for the the hi message to come.



const url = 'ws://nodeserver:3000';


describe("Chat Server", function()
{
it("Should broadcast hi!", function(done)
{
let client1 = io.connect(url, options);

client1.on('connect', function()
{
client1.on('hi', function(msg)
{
msg.should.equal("hi");
client1.disconnect();
done();
});
});
});
}


running docker-compose, runs the nodeserver and the test client fails with timeout, which tells me the client cannot see the swarm network.



now running the dockers separately, that is exposing nodeservers to host and trying to connect to my localhost instead works perfectly and the test passes. This tells me that my socket and the way I communicate with nodeserver should be correct, which would basically mean that I should be having a problem with setting up my swarm's network. Can somebody tell me what I am doing wrong here?










share|improve this question





























    0















    I am currently working on dockerizing a tested socket.io app for a simple chat application, using socket.io, and mocha for testing. the server opens up a socket for listening on port 3000 and the test client uses the socket to emit messages or receive emissions.



    I am using version 3 of docker compose files.



    nodeserver dockerfile:



    FROM node:10

    WORKDIR /usr/src/appserver

    COPY package*.json ./
    COPY public public
    COPY main.js main.js

    RUN npm install
    RUN npm install express
    RUN npm install socket.io

    CMD ["npm", "start"]


    test dockerfile:



    FROM nodeserver

    COPY test test

    RUN npm update &&
    npm install -g mocha &&
    npm install -g socket.io-client

    CMD ["npm", "test"]


    docker-compose:



    version: "3"

    services:
    nodeserver:
    build: .
    expose:
    - "3000"
    image: ws

    test:
    depends_on:
    - nodeserver
    links:
    - nodeserver
    build: ./test
    image: test_image


    my node server is listening on port 3000, and on connection sends a hi message to all.



    let express = require('express');
    let app = express();
    let http = require('http').createServer(app);
    let io = require('socket.io')(http);
    http.listen(3000, function ()
    {
    console.log('listening on *:3000');
    });

    io.on('connection', function(socket)
    {
    console.log('a user connected');

    io.emit('hi', 'hi');
    });


    and my mocha test looks like this, which in essence attaches itself as a client and waits for the the hi message to come.



    const url = 'ws://nodeserver:3000';


    describe("Chat Server", function()
    {
    it("Should broadcast hi!", function(done)
    {
    let client1 = io.connect(url, options);

    client1.on('connect', function()
    {
    client1.on('hi', function(msg)
    {
    msg.should.equal("hi");
    client1.disconnect();
    done();
    });
    });
    });
    }


    running docker-compose, runs the nodeserver and the test client fails with timeout, which tells me the client cannot see the swarm network.



    now running the dockers separately, that is exposing nodeservers to host and trying to connect to my localhost instead works perfectly and the test passes. This tells me that my socket and the way I communicate with nodeserver should be correct, which would basically mean that I should be having a problem with setting up my swarm's network. Can somebody tell me what I am doing wrong here?










    share|improve this question



























      0












      0








      0


      1






      I am currently working on dockerizing a tested socket.io app for a simple chat application, using socket.io, and mocha for testing. the server opens up a socket for listening on port 3000 and the test client uses the socket to emit messages or receive emissions.



      I am using version 3 of docker compose files.



      nodeserver dockerfile:



      FROM node:10

      WORKDIR /usr/src/appserver

      COPY package*.json ./
      COPY public public
      COPY main.js main.js

      RUN npm install
      RUN npm install express
      RUN npm install socket.io

      CMD ["npm", "start"]


      test dockerfile:



      FROM nodeserver

      COPY test test

      RUN npm update &&
      npm install -g mocha &&
      npm install -g socket.io-client

      CMD ["npm", "test"]


      docker-compose:



      version: "3"

      services:
      nodeserver:
      build: .
      expose:
      - "3000"
      image: ws

      test:
      depends_on:
      - nodeserver
      links:
      - nodeserver
      build: ./test
      image: test_image


      my node server is listening on port 3000, and on connection sends a hi message to all.



      let express = require('express');
      let app = express();
      let http = require('http').createServer(app);
      let io = require('socket.io')(http);
      http.listen(3000, function ()
      {
      console.log('listening on *:3000');
      });

      io.on('connection', function(socket)
      {
      console.log('a user connected');

      io.emit('hi', 'hi');
      });


      and my mocha test looks like this, which in essence attaches itself as a client and waits for the the hi message to come.



      const url = 'ws://nodeserver:3000';


      describe("Chat Server", function()
      {
      it("Should broadcast hi!", function(done)
      {
      let client1 = io.connect(url, options);

      client1.on('connect', function()
      {
      client1.on('hi', function(msg)
      {
      msg.should.equal("hi");
      client1.disconnect();
      done();
      });
      });
      });
      }


      running docker-compose, runs the nodeserver and the test client fails with timeout, which tells me the client cannot see the swarm network.



      now running the dockers separately, that is exposing nodeservers to host and trying to connect to my localhost instead works perfectly and the test passes. This tells me that my socket and the way I communicate with nodeserver should be correct, which would basically mean that I should be having a problem with setting up my swarm's network. Can somebody tell me what I am doing wrong here?










      share|improve this question
















      I am currently working on dockerizing a tested socket.io app for a simple chat application, using socket.io, and mocha for testing. the server opens up a socket for listening on port 3000 and the test client uses the socket to emit messages or receive emissions.



      I am using version 3 of docker compose files.



      nodeserver dockerfile:



      FROM node:10

      WORKDIR /usr/src/appserver

      COPY package*.json ./
      COPY public public
      COPY main.js main.js

      RUN npm install
      RUN npm install express
      RUN npm install socket.io

      CMD ["npm", "start"]


      test dockerfile:



      FROM nodeserver

      COPY test test

      RUN npm update &&
      npm install -g mocha &&
      npm install -g socket.io-client

      CMD ["npm", "test"]


      docker-compose:



      version: "3"

      services:
      nodeserver:
      build: .
      expose:
      - "3000"
      image: ws

      test:
      depends_on:
      - nodeserver
      links:
      - nodeserver
      build: ./test
      image: test_image


      my node server is listening on port 3000, and on connection sends a hi message to all.



      let express = require('express');
      let app = express();
      let http = require('http').createServer(app);
      let io = require('socket.io')(http);
      http.listen(3000, function ()
      {
      console.log('listening on *:3000');
      });

      io.on('connection', function(socket)
      {
      console.log('a user connected');

      io.emit('hi', 'hi');
      });


      and my mocha test looks like this, which in essence attaches itself as a client and waits for the the hi message to come.



      const url = 'ws://nodeserver:3000';


      describe("Chat Server", function()
      {
      it("Should broadcast hi!", function(done)
      {
      let client1 = io.connect(url, options);

      client1.on('connect', function()
      {
      client1.on('hi', function(msg)
      {
      msg.should.equal("hi");
      client1.disconnect();
      done();
      });
      });
      });
      }


      running docker-compose, runs the nodeserver and the test client fails with timeout, which tells me the client cannot see the swarm network.



      now running the dockers separately, that is exposing nodeservers to host and trying to connect to my localhost instead works perfectly and the test passes. This tells me that my socket and the way I communicate with nodeserver should be correct, which would basically mean that I should be having a problem with setting up my swarm's network. Can somebody tell me what I am doing wrong here?







      node.js docker socket.io docker-compose mocha






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 15:13









      Siyu

      2,8141927




      2,8141927










      asked Nov 18 '18 at 11:44









      ArminArmin

      40210




      40210
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I think your configuration looks good, it's a matter of readiness of your nodeserver. Even with depends_on, there is no guarantee that nodeserver is ready when test starts. (also links is useless and deprecated).



          To verify my hypothesis, try the following sequence:



          docker-compose up -d nodeserver



          wait a few seconds



          docker-compose up -d test






          share|improve this answer
























          • hey @Siyu, still have the same problem, even if I wait for a couple of minutes

            – Armin
            Nov 18 '18 at 15:16











          • @Armin how did you run the separately?

            – Siyu
            Nov 18 '18 at 15:19











          • I exposed the server with -p 3000:3000 on my host and ran it directly on a container, then ran the tests without container directly with mocha in localhost:3000

            – Armin
            Nov 18 '18 at 15:22











          • btw running two containers without compose and binding ports to local host wouldnt work, because the second container complains that the port is already allocated

            – Armin
            Nov 18 '18 at 17:23



















          0














          ok so apparently I was running the test on top of nodeserver was not a good idea. I think the keyword here is



          depends_on:
          - nodeserver


          in the "test" service part of compose. This forces test service to build the server again and run the command



          CMD ["npm", "start"]


          from "nodeserver" service, and that screws things up, after removing the "depends_on", all my tests pass and two containers can connect.



          the complete bare bone project can be found here



          https://github.com/tetrohed/couch






          share|improve this answer


























          • Not working without the depends_on either. I'm failing to understand how a client would be able to connect to 'ws://nodeserver:3000'. A server would have this alias available on the network, but the client is run on the host right? And Docker doesn't seem to create aliasses on the host machine, or I would be able to ping nodeserver...

            – JosFabre
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360489%2fconnect-two-docker-containers-with-socket-io%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I think your configuration looks good, it's a matter of readiness of your nodeserver. Even with depends_on, there is no guarantee that nodeserver is ready when test starts. (also links is useless and deprecated).



          To verify my hypothesis, try the following sequence:



          docker-compose up -d nodeserver



          wait a few seconds



          docker-compose up -d test






          share|improve this answer
























          • hey @Siyu, still have the same problem, even if I wait for a couple of minutes

            – Armin
            Nov 18 '18 at 15:16











          • @Armin how did you run the separately?

            – Siyu
            Nov 18 '18 at 15:19











          • I exposed the server with -p 3000:3000 on my host and ran it directly on a container, then ran the tests without container directly with mocha in localhost:3000

            – Armin
            Nov 18 '18 at 15:22











          • btw running two containers without compose and binding ports to local host wouldnt work, because the second container complains that the port is already allocated

            – Armin
            Nov 18 '18 at 17:23
















          0














          I think your configuration looks good, it's a matter of readiness of your nodeserver. Even with depends_on, there is no guarantee that nodeserver is ready when test starts. (also links is useless and deprecated).



          To verify my hypothesis, try the following sequence:



          docker-compose up -d nodeserver



          wait a few seconds



          docker-compose up -d test






          share|improve this answer
























          • hey @Siyu, still have the same problem, even if I wait for a couple of minutes

            – Armin
            Nov 18 '18 at 15:16











          • @Armin how did you run the separately?

            – Siyu
            Nov 18 '18 at 15:19











          • I exposed the server with -p 3000:3000 on my host and ran it directly on a container, then ran the tests without container directly with mocha in localhost:3000

            – Armin
            Nov 18 '18 at 15:22











          • btw running two containers without compose and binding ports to local host wouldnt work, because the second container complains that the port is already allocated

            – Armin
            Nov 18 '18 at 17:23














          0












          0








          0







          I think your configuration looks good, it's a matter of readiness of your nodeserver. Even with depends_on, there is no guarantee that nodeserver is ready when test starts. (also links is useless and deprecated).



          To verify my hypothesis, try the following sequence:



          docker-compose up -d nodeserver



          wait a few seconds



          docker-compose up -d test






          share|improve this answer













          I think your configuration looks good, it's a matter of readiness of your nodeserver. Even with depends_on, there is no guarantee that nodeserver is ready when test starts. (also links is useless and deprecated).



          To verify my hypothesis, try the following sequence:



          docker-compose up -d nodeserver



          wait a few seconds



          docker-compose up -d test







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 13:27









          SiyuSiyu

          2,8141927




          2,8141927













          • hey @Siyu, still have the same problem, even if I wait for a couple of minutes

            – Armin
            Nov 18 '18 at 15:16











          • @Armin how did you run the separately?

            – Siyu
            Nov 18 '18 at 15:19











          • I exposed the server with -p 3000:3000 on my host and ran it directly on a container, then ran the tests without container directly with mocha in localhost:3000

            – Armin
            Nov 18 '18 at 15:22











          • btw running two containers without compose and binding ports to local host wouldnt work, because the second container complains that the port is already allocated

            – Armin
            Nov 18 '18 at 17:23



















          • hey @Siyu, still have the same problem, even if I wait for a couple of minutes

            – Armin
            Nov 18 '18 at 15:16











          • @Armin how did you run the separately?

            – Siyu
            Nov 18 '18 at 15:19











          • I exposed the server with -p 3000:3000 on my host and ran it directly on a container, then ran the tests without container directly with mocha in localhost:3000

            – Armin
            Nov 18 '18 at 15:22











          • btw running two containers without compose and binding ports to local host wouldnt work, because the second container complains that the port is already allocated

            – Armin
            Nov 18 '18 at 17:23

















          hey @Siyu, still have the same problem, even if I wait for a couple of minutes

          – Armin
          Nov 18 '18 at 15:16





          hey @Siyu, still have the same problem, even if I wait for a couple of minutes

          – Armin
          Nov 18 '18 at 15:16













          @Armin how did you run the separately?

          – Siyu
          Nov 18 '18 at 15:19





          @Armin how did you run the separately?

          – Siyu
          Nov 18 '18 at 15:19













          I exposed the server with -p 3000:3000 on my host and ran it directly on a container, then ran the tests without container directly with mocha in localhost:3000

          – Armin
          Nov 18 '18 at 15:22





          I exposed the server with -p 3000:3000 on my host and ran it directly on a container, then ran the tests without container directly with mocha in localhost:3000

          – Armin
          Nov 18 '18 at 15:22













          btw running two containers without compose and binding ports to local host wouldnt work, because the second container complains that the port is already allocated

          – Armin
          Nov 18 '18 at 17:23





          btw running two containers without compose and binding ports to local host wouldnt work, because the second container complains that the port is already allocated

          – Armin
          Nov 18 '18 at 17:23













          0














          ok so apparently I was running the test on top of nodeserver was not a good idea. I think the keyword here is



          depends_on:
          - nodeserver


          in the "test" service part of compose. This forces test service to build the server again and run the command



          CMD ["npm", "start"]


          from "nodeserver" service, and that screws things up, after removing the "depends_on", all my tests pass and two containers can connect.



          the complete bare bone project can be found here



          https://github.com/tetrohed/couch






          share|improve this answer


























          • Not working without the depends_on either. I'm failing to understand how a client would be able to connect to 'ws://nodeserver:3000'. A server would have this alias available on the network, but the client is run on the host right? And Docker doesn't seem to create aliasses on the host machine, or I would be able to ping nodeserver...

            – JosFabre
            2 days ago
















          0














          ok so apparently I was running the test on top of nodeserver was not a good idea. I think the keyword here is



          depends_on:
          - nodeserver


          in the "test" service part of compose. This forces test service to build the server again and run the command



          CMD ["npm", "start"]


          from "nodeserver" service, and that screws things up, after removing the "depends_on", all my tests pass and two containers can connect.



          the complete bare bone project can be found here



          https://github.com/tetrohed/couch






          share|improve this answer


























          • Not working without the depends_on either. I'm failing to understand how a client would be able to connect to 'ws://nodeserver:3000'. A server would have this alias available on the network, but the client is run on the host right? And Docker doesn't seem to create aliasses on the host machine, or I would be able to ping nodeserver...

            – JosFabre
            2 days ago














          0












          0








          0







          ok so apparently I was running the test on top of nodeserver was not a good idea. I think the keyword here is



          depends_on:
          - nodeserver


          in the "test" service part of compose. This forces test service to build the server again and run the command



          CMD ["npm", "start"]


          from "nodeserver" service, and that screws things up, after removing the "depends_on", all my tests pass and two containers can connect.



          the complete bare bone project can be found here



          https://github.com/tetrohed/couch






          share|improve this answer















          ok so apparently I was running the test on top of nodeserver was not a good idea. I think the keyword here is



          depends_on:
          - nodeserver


          in the "test" service part of compose. This forces test service to build the server again and run the command



          CMD ["npm", "start"]


          from "nodeserver" service, and that screws things up, after removing the "depends_on", all my tests pass and two containers can connect.



          the complete bare bone project can be found here



          https://github.com/tetrohed/couch







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 18 '18 at 19:49

























          answered Nov 18 '18 at 18:58









          ArminArmin

          40210




          40210













          • Not working without the depends_on either. I'm failing to understand how a client would be able to connect to 'ws://nodeserver:3000'. A server would have this alias available on the network, but the client is run on the host right? And Docker doesn't seem to create aliasses on the host machine, or I would be able to ping nodeserver...

            – JosFabre
            2 days ago



















          • Not working without the depends_on either. I'm failing to understand how a client would be able to connect to 'ws://nodeserver:3000'. A server would have this alias available on the network, but the client is run on the host right? And Docker doesn't seem to create aliasses on the host machine, or I would be able to ping nodeserver...

            – JosFabre
            2 days ago

















          Not working without the depends_on either. I'm failing to understand how a client would be able to connect to 'ws://nodeserver:3000'. A server would have this alias available on the network, but the client is run on the host right? And Docker doesn't seem to create aliasses on the host machine, or I would be able to ping nodeserver...

          – JosFabre
          2 days ago





          Not working without the depends_on either. I'm failing to understand how a client would be able to connect to 'ws://nodeserver:3000'. A server would have this alias available on the network, but the client is run on the host right? And Docker doesn't seem to create aliasses on the host machine, or I would be able to ping nodeserver...

          – JosFabre
          2 days ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360489%2fconnect-two-docker-containers-with-socket-io%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini