Home > Software engineering >  How to connect to a MongoDb of a docker container
How to connect to a MongoDb of a docker container

Time:03-24

I've created the following docker-compose.yml:

version: "3"
services:
  mongo:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
    ports:
      - '27017:27017'

I then start my containers:

docker-compose up

then I try to connect into MongoDb Compass(also tried through c# code), with the following:

  • mongodb://admin:admin@localhost:27017/?authSource=admin
  • mongodb://admin:admin@localhost:27017
  • mongodb://admin:[email protected]:27017

But I always get a "Authentication failed" message:

enter image description here

I really don't understand what is going on. What am I missing.

Sorry for the dumb question...

CodePudding user response:

The behavior you're seeing suggests that there is already another mongodb instance running on your system (with different authentication credentials). Stop the Docker container and check to see if there is still a mongodb service listening on port 27017.

  • Related