david benalal
david benalal

Reputation: 386

connect my netcore app to mongodb using docker-compose?

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/

Upvotes: 0

Views: 151

Answers (1)

Uku Loskit
Uku Loskit

Reputation: 42040

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"));

Upvotes: 1

Related Questions