connect my netcore app to mongodb using docker-compose?











up vote
0
down vote

favorite












I can't seem to connect my app container to the mongodb container



here is the mongo class inside my app



public BaseDao(string dbName)
{
m_mongoClient = new MongoClient("mongodb://localhost:27017");
m_mongoDb = m_mongoClient.GetDatabase(dbName);
}


here is my docker-compose



version: '3.4'

services:
app:
image: vinnie_app:latest
depends_on:
- 'mongodb'
mongodb:
image: mongo:latest
container_name: "mongodb"
ports:
- 27017:27017


and my docker-compose.override



version: '3.4'

services:
app:
build: ./app
environment:
- ASPNETCORE_ENVIRONMENT=Staging
- NODE_ENV=staging
- ASPNETCORE_URLS=http://*:50000
- MONGODB_URI='mongodb://mongo:27017/'
ports:
- 50000:50000


I get a 500 error when I try to fetch stuff from the database from the app container. What's the error and how do I connect my app container to the mongodb?? I run the containerized app at http://192.168.99.100:50000/










share|improve this question


























    up vote
    0
    down vote

    favorite












    I can't seem to connect my app container to the mongodb container



    here is the mongo class inside my app



    public BaseDao(string dbName)
    {
    m_mongoClient = new MongoClient("mongodb://localhost:27017");
    m_mongoDb = m_mongoClient.GetDatabase(dbName);
    }


    here is my docker-compose



    version: '3.4'

    services:
    app:
    image: vinnie_app:latest
    depends_on:
    - 'mongodb'
    mongodb:
    image: mongo:latest
    container_name: "mongodb"
    ports:
    - 27017:27017


    and my docker-compose.override



    version: '3.4'

    services:
    app:
    build: ./app
    environment:
    - ASPNETCORE_ENVIRONMENT=Staging
    - NODE_ENV=staging
    - ASPNETCORE_URLS=http://*:50000
    - MONGODB_URI='mongodb://mongo:27017/'
    ports:
    - 50000:50000


    I get a 500 error when I try to fetch stuff from the database from the app container. What's the error and how do I connect my app container to the mongodb?? I run the containerized app at http://192.168.99.100:50000/










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I can't seem to connect my app container to the mongodb container



      here is the mongo class inside my app



      public BaseDao(string dbName)
      {
      m_mongoClient = new MongoClient("mongodb://localhost:27017");
      m_mongoDb = m_mongoClient.GetDatabase(dbName);
      }


      here is my docker-compose



      version: '3.4'

      services:
      app:
      image: vinnie_app:latest
      depends_on:
      - 'mongodb'
      mongodb:
      image: mongo:latest
      container_name: "mongodb"
      ports:
      - 27017:27017


      and my docker-compose.override



      version: '3.4'

      services:
      app:
      build: ./app
      environment:
      - ASPNETCORE_ENVIRONMENT=Staging
      - NODE_ENV=staging
      - ASPNETCORE_URLS=http://*:50000
      - MONGODB_URI='mongodb://mongo:27017/'
      ports:
      - 50000:50000


      I get a 500 error when I try to fetch stuff from the database from the app container. What's the error and how do I connect my app container to the mongodb?? I run the containerized app at http://192.168.99.100:50000/










      share|improve this question













      I can't seem to connect my app container to the mongodb container



      here is the mongo class inside my app



      public BaseDao(string dbName)
      {
      m_mongoClient = new MongoClient("mongodb://localhost:27017");
      m_mongoDb = m_mongoClient.GetDatabase(dbName);
      }


      here is my docker-compose



      version: '3.4'

      services:
      app:
      image: vinnie_app:latest
      depends_on:
      - 'mongodb'
      mongodb:
      image: mongo:latest
      container_name: "mongodb"
      ports:
      - 27017:27017


      and my docker-compose.override



      version: '3.4'

      services:
      app:
      build: ./app
      environment:
      - ASPNETCORE_ENVIRONMENT=Staging
      - NODE_ENV=staging
      - ASPNETCORE_URLS=http://*:50000
      - MONGODB_URI='mongodb://mongo:27017/'
      ports:
      - 50000:50000


      I get a 500 error when I try to fetch stuff from the database from the app container. What's the error and how do I connect my app container to the mongodb?? I run the containerized app at http://192.168.99.100:50000/







      mongodb docker docker-compose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 15:16









      david benalal

      557




      557
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          m_mongoClient = new MongoClient("mongodb://localhost:27017");
          this line should instead read the URI from MONGODB_URI environment variable.



          m_mongoClient = new MongoClient(System.getenv("MONGODB_URI"));






          share|improve this answer





















          • what if I want to run the app from VS and not docker, it won't work anymore right? I want to be able to run the app from both VS and docker
            – david benalal
            Nov 19 at 15:51






          • 1




            m_mongoClient = new MongoClient(System.getenv("MONGODB_URI") || "mongodb://localhost:27017");
            – Siyu
            Nov 19 at 16:02










          • String m_url = System.getEnv("MONGODB_URI"); if (m_url == null) {m_url = mongodb://localhost:27017"} `
            – Uku Loskit
            Nov 19 at 16:02






          • 1




            @Siyu beat me to a more concise form :)
            – Uku Loskit
            Nov 19 at 16:03










          • System.getenv does not compile. Also Siyu i can't use ||
            – david benalal
            Nov 19 at 17:24













          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%2f53377621%2fconnect-my-netcore-app-to-mongodb-using-docker-compose%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













          m_mongoClient = new MongoClient("mongodb://localhost:27017");
          this line should instead read the URI from MONGODB_URI environment variable.



          m_mongoClient = new MongoClient(System.getenv("MONGODB_URI"));






          share|improve this answer





















          • what if I want to run the app from VS and not docker, it won't work anymore right? I want to be able to run the app from both VS and docker
            – david benalal
            Nov 19 at 15:51






          • 1




            m_mongoClient = new MongoClient(System.getenv("MONGODB_URI") || "mongodb://localhost:27017");
            – Siyu
            Nov 19 at 16:02










          • String m_url = System.getEnv("MONGODB_URI"); if (m_url == null) {m_url = mongodb://localhost:27017"} `
            – Uku Loskit
            Nov 19 at 16:02






          • 1




            @Siyu beat me to a more concise form :)
            – Uku Loskit
            Nov 19 at 16:03










          • System.getenv does not compile. Also Siyu i can't use ||
            – david benalal
            Nov 19 at 17:24

















          up vote
          1
          down vote













          m_mongoClient = new MongoClient("mongodb://localhost:27017");
          this line should instead read the URI from MONGODB_URI environment variable.



          m_mongoClient = new MongoClient(System.getenv("MONGODB_URI"));






          share|improve this answer





















          • what if I want to run the app from VS and not docker, it won't work anymore right? I want to be able to run the app from both VS and docker
            – david benalal
            Nov 19 at 15:51






          • 1




            m_mongoClient = new MongoClient(System.getenv("MONGODB_URI") || "mongodb://localhost:27017");
            – Siyu
            Nov 19 at 16:02










          • String m_url = System.getEnv("MONGODB_URI"); if (m_url == null) {m_url = mongodb://localhost:27017"} `
            – Uku Loskit
            Nov 19 at 16:02






          • 1




            @Siyu beat me to a more concise form :)
            – Uku Loskit
            Nov 19 at 16:03










          • System.getenv does not compile. Also Siyu i can't use ||
            – david benalal
            Nov 19 at 17:24















          up vote
          1
          down vote










          up vote
          1
          down vote









          m_mongoClient = new MongoClient("mongodb://localhost:27017");
          this line should instead read the URI from MONGODB_URI environment variable.



          m_mongoClient = new MongoClient(System.getenv("MONGODB_URI"));






          share|improve this answer












          m_mongoClient = new MongoClient("mongodb://localhost:27017");
          this line should instead read the URI from MONGODB_URI environment variable.



          m_mongoClient = new MongoClient(System.getenv("MONGODB_URI"));







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 15:47









          Uku Loskit

          29.9k86779




          29.9k86779












          • what if I want to run the app from VS and not docker, it won't work anymore right? I want to be able to run the app from both VS and docker
            – david benalal
            Nov 19 at 15:51






          • 1




            m_mongoClient = new MongoClient(System.getenv("MONGODB_URI") || "mongodb://localhost:27017");
            – Siyu
            Nov 19 at 16:02










          • String m_url = System.getEnv("MONGODB_URI"); if (m_url == null) {m_url = mongodb://localhost:27017"} `
            – Uku Loskit
            Nov 19 at 16:02






          • 1




            @Siyu beat me to a more concise form :)
            – Uku Loskit
            Nov 19 at 16:03










          • System.getenv does not compile. Also Siyu i can't use ||
            – david benalal
            Nov 19 at 17:24




















          • what if I want to run the app from VS and not docker, it won't work anymore right? I want to be able to run the app from both VS and docker
            – david benalal
            Nov 19 at 15:51






          • 1




            m_mongoClient = new MongoClient(System.getenv("MONGODB_URI") || "mongodb://localhost:27017");
            – Siyu
            Nov 19 at 16:02










          • String m_url = System.getEnv("MONGODB_URI"); if (m_url == null) {m_url = mongodb://localhost:27017"} `
            – Uku Loskit
            Nov 19 at 16:02






          • 1




            @Siyu beat me to a more concise form :)
            – Uku Loskit
            Nov 19 at 16:03










          • System.getenv does not compile. Also Siyu i can't use ||
            – david benalal
            Nov 19 at 17:24


















          what if I want to run the app from VS and not docker, it won't work anymore right? I want to be able to run the app from both VS and docker
          – david benalal
          Nov 19 at 15:51




          what if I want to run the app from VS and not docker, it won't work anymore right? I want to be able to run the app from both VS and docker
          – david benalal
          Nov 19 at 15:51




          1




          1




          m_mongoClient = new MongoClient(System.getenv("MONGODB_URI") || "mongodb://localhost:27017");
          – Siyu
          Nov 19 at 16:02




          m_mongoClient = new MongoClient(System.getenv("MONGODB_URI") || "mongodb://localhost:27017");
          – Siyu
          Nov 19 at 16:02












          String m_url = System.getEnv("MONGODB_URI"); if (m_url == null) {m_url = mongodb://localhost:27017"} `
          – Uku Loskit
          Nov 19 at 16:02




          String m_url = System.getEnv("MONGODB_URI"); if (m_url == null) {m_url = mongodb://localhost:27017"} `
          – Uku Loskit
          Nov 19 at 16:02




          1




          1




          @Siyu beat me to a more concise form :)
          – Uku Loskit
          Nov 19 at 16:03




          @Siyu beat me to a more concise form :)
          – Uku Loskit
          Nov 19 at 16:03












          System.getenv does not compile. Also Siyu i can't use ||
          – david benalal
          Nov 19 at 17:24






          System.getenv does not compile. Also Siyu i can't use ||
          – david benalal
          Nov 19 at 17:24




















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53377621%2fconnect-my-netcore-app-to-mongodb-using-docker-compose%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?