J4N
J4N

Reputation: 20771

How to connect to a MongoDb of a docker container

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:

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...

Upvotes: 0

Views: 1556

Answers (2)

Dawid Frankiewicz
Dawid Frankiewicz

Reputation: 130

I think it might be better to set ports to

ports:
   - '27012:27017'

So you don't need to delete anything from your system. Now you can connect as ussual, just change port to 27012 instead of 27017

Also this way you can run multiple monogdb's using more ports wthout any issues. In my case I still needed local mongodb running and because of that port 27017 was automatically connecting to it.

Upvotes: 0

larsks
larsks

Reputation: 312690

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.

Upvotes: 1

Related Questions