Oscar
Oscar

Reputation: 13960

Can't connect to MongoDB running from docker-compose

I'm unable to connect to containerized mongodb instance, authentication fails from application or from client (Robo 3T)

This is my docker compose file:

    version: '3.4'

services:
  db:
    container_name : mongo
    image: mongo
    volumes:
        - c:/data/db:/data/db
        - c:/data/configdb:/data/configdb
    ports:
          - '27017:27017'
    restart: always
    environment:
      AUTH: "yes"
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: dBPassword01
    command: mongod
    networks:
        clusternetwork:
            ipv4_address: 172.16.0.2

  calculatorservice:
    image: ${DOCKER_REGISTRY-}calculatorservice
    build:
      context: .
      dockerfile: CalculatorService/Dockerfile
    depends_on:
            - db
    networks:
        clusternetwork:
            ipv4_address: 172.16.0.3

networks:
  clusternetwork:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.16.0.0/24

In my application setting, I have:

"MongoDbSettings": {
    "ConnectionString": "mongodb://dbroot:[email protected]:27017",
    "DatabaseName": "calculator-service"
  }

But it fails with the following error:

MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
 ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed..
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)

Using client (Robo 3T) I have the same authentication issue:

enter image description here

What am I missing here?

Upvotes: 1

Views: 903

Answers (3)

Jordy Cuan
Jordy Cuan

Reputation: 487

I was facing the same problem but having well configured my root username. The thing was I had robo 3t on windows and mongo container running inside wsl.

The solution was getting my wsl inet ip (ifconfig in ubuntu or wsl hostname -I in windows) and then connecting to this IP rather than localhost

Upvotes: 0

andrew.fox
andrew.fox

Reputation: 7933

I had a similar problem with my Docker. It turned out that I was connecting to a localhost instance, instead of the docker one, because the ports were the same. I received Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1 because the user didn't match, but connection was successful. (Now it sounds funny)

Upvotes: 0

OhadR
OhadR

Reputation: 8839

In your config:

MONGO_INITDB_ROOT_USERNAME: root

so why you try to connect with username dbroot? i would try to use the same username, root...

Upvotes: 1

Related Questions