Nikhil Pandey
Nikhil Pandey

Reputation: 25

Unable to access mongo db through mongoose running in docker container

docker compose file

version: '3'
services:
  web:
    build:
      context: ./reactapp
    ports:
      - "3000:3000"
  server:
    build:
      context: ./expressapp
    ports:
      - "4000:4000"
  mongo:
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"

node js code

function connectToMongoDB() {
    return mongoose.connect('mongodb://127.0.0.1:27017').then(() => {

    }, (error) => {
        console.log(error);
        setTimeout(() => {
            connectToMongoDB()
        }, 10000);
    });
}

Upvotes: 1

Views: 61

Answers (1)

Shoan
Shoan

Reputation: 4078

You have to use 'mongo' as the hostname, not 127.0.0.1

Upvotes: 1

Related Questions