Node app fails to connect to MongoDB, but only in Docker









up vote
1
down vote

favorite












I have two apps:



  • MongoDB (started from the Bitnami MongoDB Docker image)

  • My custom node app

The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up, the backend can not connect to MongoDB.



This is an excerpt of the startup sequence:



mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
[...]
backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'


This is my docker-compose.yml:



version: '2'
services:

mongodb:
image: bitnami/mongodb:latest
expose:
- 27017
environment:
- ALLOW_EMPTY_PASSWORD=yes

backend:
build: ./backend
environment:
API_HOST: http://localhost:3000/
APP_SERVER_PORT: 3000
expose:
- 3000
volumes:
- ./backend:/app/backend
links:
- mongodb
depends_on:
- mongodb


This is my node call to the DB:



mongoose.connect('mongodb://localhost:27017/groceryList', 
useNewUrlParser: true
);


I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:



  • It is not that MongoDB is not ready when my node app tries to
    connect. I wrapped my connection call into an auto reconnection
    function as described here and the error repeats endlessly. It is not just about the "first
    connect".

  • I can publish Port 27017 of the MongoDB container and
    happily connect with Robo3T. The DB is definitely working.

  • When I connect to mongodb://mongo:27017/groceryList instead, the same applies, only with the ENOTFOUND flag instead of ECONNREFUSED.

What am I missing?



  • Docker 18.06.1-ce

  • docker-compose 1.22.0

  • Mongoose 5.3.6

  • MongoDB 4.0.3

  • Node 11.1.0

  • macOS 10.14.1









share|improve this question

























    up vote
    1
    down vote

    favorite












    I have two apps:



    • MongoDB (started from the Bitnami MongoDB Docker image)

    • My custom node app

    The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up, the backend can not connect to MongoDB.



    This is an excerpt of the startup sequence:



    mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
    [...]
    backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'


    This is my docker-compose.yml:



    version: '2'
    services:

    mongodb:
    image: bitnami/mongodb:latest
    expose:
    - 27017
    environment:
    - ALLOW_EMPTY_PASSWORD=yes

    backend:
    build: ./backend
    environment:
    API_HOST: http://localhost:3000/
    APP_SERVER_PORT: 3000
    expose:
    - 3000
    volumes:
    - ./backend:/app/backend
    links:
    - mongodb
    depends_on:
    - mongodb


    This is my node call to the DB:



    mongoose.connect('mongodb://localhost:27017/groceryList', 
    useNewUrlParser: true
    );


    I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:



    • It is not that MongoDB is not ready when my node app tries to
      connect. I wrapped my connection call into an auto reconnection
      function as described here and the error repeats endlessly. It is not just about the "first
      connect".

    • I can publish Port 27017 of the MongoDB container and
      happily connect with Robo3T. The DB is definitely working.

    • When I connect to mongodb://mongo:27017/groceryList instead, the same applies, only with the ENOTFOUND flag instead of ECONNREFUSED.

    What am I missing?



    • Docker 18.06.1-ce

    • docker-compose 1.22.0

    • Mongoose 5.3.6

    • MongoDB 4.0.3

    • Node 11.1.0

    • macOS 10.14.1









    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have two apps:



      • MongoDB (started from the Bitnami MongoDB Docker image)

      • My custom node app

      The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up, the backend can not connect to MongoDB.



      This is an excerpt of the startup sequence:



      mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
      [...]
      backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'


      This is my docker-compose.yml:



      version: '2'
      services:

      mongodb:
      image: bitnami/mongodb:latest
      expose:
      - 27017
      environment:
      - ALLOW_EMPTY_PASSWORD=yes

      backend:
      build: ./backend
      environment:
      API_HOST: http://localhost:3000/
      APP_SERVER_PORT: 3000
      expose:
      - 3000
      volumes:
      - ./backend:/app/backend
      links:
      - mongodb
      depends_on:
      - mongodb


      This is my node call to the DB:



      mongoose.connect('mongodb://localhost:27017/groceryList', 
      useNewUrlParser: true
      );


      I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:



      • It is not that MongoDB is not ready when my node app tries to
        connect. I wrapped my connection call into an auto reconnection
        function as described here and the error repeats endlessly. It is not just about the "first
        connect".

      • I can publish Port 27017 of the MongoDB container and
        happily connect with Robo3T. The DB is definitely working.

      • When I connect to mongodb://mongo:27017/groceryList instead, the same applies, only with the ENOTFOUND flag instead of ECONNREFUSED.

      What am I missing?



      • Docker 18.06.1-ce

      • docker-compose 1.22.0

      • Mongoose 5.3.6

      • MongoDB 4.0.3

      • Node 11.1.0

      • macOS 10.14.1









      share|improve this question













      I have two apps:



      • MongoDB (started from the Bitnami MongoDB Docker image)

      • My custom node app

      The two apps interact flawlessly when my node app is run natively. Now I've put it inside a Docker container and when I start both together with docker compose up, the backend can not connect to MongoDB.



      This is an excerpt of the startup sequence:



      mongodb_1 | 2018-11-10T22:22:52.481+0000 I NETWORK [initandlisten] waiting for connections on port 27017
      [...]
      backend_1 | 2018-11-10T22:23:48.119Z 'MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'


      This is my docker-compose.yml:



      version: '2'
      services:

      mongodb:
      image: bitnami/mongodb:latest
      expose:
      - 27017
      environment:
      - ALLOW_EMPTY_PASSWORD=yes

      backend:
      build: ./backend
      environment:
      API_HOST: http://localhost:3000/
      APP_SERVER_PORT: 3000
      expose:
      - 3000
      volumes:
      - ./backend:/app/backend
      links:
      - mongodb
      depends_on:
      - mongodb


      This is my node call to the DB:



      mongoose.connect('mongodb://localhost:27017/groceryList', 
      useNewUrlParser: true
      );


      I skimmed about 15 Stackoverflow questions asking the same and I am not getting the cause:



      • It is not that MongoDB is not ready when my node app tries to
        connect. I wrapped my connection call into an auto reconnection
        function as described here and the error repeats endlessly. It is not just about the "first
        connect".

      • I can publish Port 27017 of the MongoDB container and
        happily connect with Robo3T. The DB is definitely working.

      • When I connect to mongodb://mongo:27017/groceryList instead, the same applies, only with the ENOTFOUND flag instead of ECONNREFUSED.

      What am I missing?



      • Docker 18.06.1-ce

      • docker-compose 1.22.0

      • Mongoose 5.3.6

      • MongoDB 4.0.3

      • Node 11.1.0

      • macOS 10.14.1






      node.js mongodb docker mongoose docker-compose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 23:37









      303

      264




      264






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your mongodb service is named mongodb not mongo.



          Try



          mongoose.connect('mongodb://mongodb:27017/groceryList', 
          useNewUrlParser: true
          );


          The generic form is 'mongodb://mongoServiceName:27017/dbname', this uses docker's automatic dns resolution for containers within the same network.



          And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.






          share|improve this answer




















            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',
            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%2f53244489%2fnode-app-fails-to-connect-to-mongodb-but-only-in-docker%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Your mongodb service is named mongodb not mongo.



            Try



            mongoose.connect('mongodb://mongodb:27017/groceryList', 
            useNewUrlParser: true
            );


            The generic form is 'mongodb://mongoServiceName:27017/dbname', this uses docker's automatic dns resolution for containers within the same network.



            And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.






            share|improve this answer
























              up vote
              1
              down vote



              accepted










              Your mongodb service is named mongodb not mongo.



              Try



              mongoose.connect('mongodb://mongodb:27017/groceryList', 
              useNewUrlParser: true
              );


              The generic form is 'mongodb://mongoServiceName:27017/dbname', this uses docker's automatic dns resolution for containers within the same network.



              And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.






              share|improve this answer






















                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Your mongodb service is named mongodb not mongo.



                Try



                mongoose.connect('mongodb://mongodb:27017/groceryList', 
                useNewUrlParser: true
                );


                The generic form is 'mongodb://mongoServiceName:27017/dbname', this uses docker's automatic dns resolution for containers within the same network.



                And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.






                share|improve this answer












                Your mongodb service is named mongodb not mongo.



                Try



                mongoose.connect('mongodb://mongodb:27017/groceryList', 
                useNewUrlParser: true
                );


                The generic form is 'mongodb://mongoServiceName:27017/dbname', this uses docker's automatic dns resolution for containers within the same network.



                And as you may already know from other questions/answers, within a container, the url is relative to itself, therefore since there not mongodb running inside the backend container, it can't connect to it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 23:53









                Siyu

                1,009517




                1,009517



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244489%2fnode-app-fails-to-connect-to-mongodb-but-only-in-docker%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







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線