ShaSha
ShaSha

Reputation: 689

mongoose couldn't connect to mongodb in docker

mongoose couldn't authenticate with docker mongodb container. Note: mongo is in docker and my API app out of the docker.

docker-compose:

version: "3.7"

services:
  db:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
      MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
      MONGO_INITDB_DB: "${MONGO_DB}"
    ports:
      - 27017:27017
    volumes:
      - mongo_data:/data/db

volumes:
  mongo_data:

.env:

MONGO_USERNAME=root
MONGO_PASSWORD=123456
MONGO_DB=nodeApp

db.js (database connection file):

(async () => {
  try {
    const uri = `mongodb://127.0.0.1:27017/${process.env.MONGO_DB}`;
    await mongoose.connect(uri, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
      user: process.env.MONGO_USERNAME,
      pass: process.env.MONGO_PASSWORD,
    });
    console.log("Database connection completed successfully");
  } catch (error) {
    console.error(error);
  }
})();

and finally this is what I got from console: enter image description here

I can connect to mongo with mongoose with username and password.

Upvotes: 3

Views: 1814

Answers (3)

Canonne Gregory
Canonne Gregory

Reputation: 424

you see that in the inspector docker -> port of your container -> (0.0.0.0:27017)

mongodb://0.0.0.0:27017/

;)

Upvotes: 0

Pradeep Dodiya
Pradeep Dodiya

Reputation: 53

Instead of localhost use your ip on database configuration.

Replace this DBurl=mongodb://localhost:27017/

to this one DBurl=mongodb://X.X.X.X:27017/ ( X.X.X.X means your ip address )on your database configuration file.

On your docker-compose.yml file

  • Inside of services of api add CONNECTIONSTRING.
version: "3.7"

services:
 api:
    build: node-server                                ## as i am using node       
    ports:
      - 3000:3000
    environment:
      - CONNECTIONSTRING=mongodb://X.X.X.X:27017/     ##X.X.X.X means your ip address
    
  db:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
      MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
      MONGO_INITDB_DB: "${MONGO_DB}"
    ports:
      - 27017:27017
    volumes:
      - mongo_data:/data/db

volumes:
  mongo_data:

Upvotes: 0

amup xm
amup xm

Reputation: 21

Docker only supports using default environment variables to be used like what you did in your code.

https://docs.docker.com/compose/env-file/

Docker added support to env filed since version 1.3 (as I remember). The soloution is to use env_file key in your yml file.

  services:
    db:
        image: mongo:latest
        environment:
            MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
            MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
            MONGO_INITDB_DB: "${MONGO_DB}"
        ports:
            - 27017:27017
        volumes:
            - mongo_data:/data/db
        env_file:
            - mongo_variables.env

Upvotes: 2

Related Questions